abstract
abstract class Animal { }
Animal a = new Animal("Centipede");
public abstract void talk();
abstract class Animal { protected String breed; public Animal(String breed) { this.breed = breed; } public String getBreed() { return breed; } public abstract void talk(); }
talk
method from
the superclass:
abstract class Animal { protected String breed; public Animal(String breed) { this.breed = breed; } public String getBreed() { return breed; } }
SingAlong.java
will give a compile-time errorAnimal a = new Cow(); a.talk();
a
is of type Animal
and complains that this has no talk
methodCow
's
talk
method that will be executed
a
ends up containing will definitely have a talk
method
Animal
had a talk
method, then
whatever a
ends up containing will have one (if only by inheritance)
talk
from Animal
is not possibleabstract
onemove
method for updating their position
Shape
(current and future) will have a getArea
method
Shape
, which will be
inherited, e.g.
public double getArea() { return 0.0; }This isn't satisfactory
Shape
Shape
, an abstract classclass abstract Shape { protected int x; protected int y; public Shape(int x, int y) { this.x = x; this.y = y; } public void move(int xChange, int yChange) { x = x + xChange; y = y + yChange; } public abstract double getArea(); }
class Circle extends Shape { private int radius; public Circle(int x, int y, int radius) { super(x, y); this.radius = radius; } public double getArea() { return 3.142 * radius * radius; } }
public
interface:
abstract
methods, concrete subclasses
must override the abstract
methods