Last Time
Interfaces So far we know that an interface is a contract. It specifies methods by listing their headers inside of the interface body.
public interface Loser
{
public void sellBackHouses();
public void mortageDeeds();
public boolean isConkingOut();
}
You sign the contract with the impements
keyword.
public class Doofus implements Loser
{
public void sellBackHouses()
{
}
public void mortageDeeds()
{
}
public boolean isConkingOut()
{
return true;
}
}
In this case, both Loser
and Doofus
are types, and Doofus
is a subtype of Loser
.
We also saw the extends
keyword in the context
of interfaces.
public interface Shape
{
public double perimeter();
public double area();
public double diameter();
}
public interface Polygon extends Shape
{
public int numSides();
}
In this case, Polygon
is a subtype of Shape
.
Triangle.java
Herron's Formula If a triangle has side lengths \(a\), \(b\), and \(c\), then its semiperimeter is defined as $$ s = {a + b + c\over 2},$$ and its area is $$ A = \sqrt{s(s-a)(s-b)(s-c)}.$$ Moreover, if the radicand is negative, then a triangle having sides of the specified lengths does not exist.
Inheritance and Subclassing
You have been subclassing all along! Every
Java class, whether standard or user-created, extends Object
.