Chapter 5 Class Hierarchies and Interfaces
Chapter 5 Class Hierarchies and Interfaces
El Dick
Inheritance
A subclass inherits all the fields and methods of
its superclass, but not constructors
Superclass
CLASS (Base class)
Subclass extends
HIERARCHIES AND Superclass
INTERFACES Subclass
(Derived class)
Chapter 5 M. El Dick
ToeWalker CharlieChaplin
M. El Dick M. El Dick
Advantages of Class Hierarchies Factoring out Common Code
Reduce duplication of code by : Biped
Factoring out common code from similar classes into a Constructor
common superclass turnLeft
turnRight
Writing more general methods turnAround
Walker Hopper
Constructor Constructor
firstStep() firstStep()
nextStep() nextStep()
stop() stop()
distanceTraveled() distanceTraveled()
M. El Dick M. El Dick
M. El Dick M. El Dick
Parameter can be a Walker, a
Hopper, etc. any subclass of Biped. Abstract Class
Some methods in a class can be declared abstract and left with
public void moveAcross (Biped creature, int distance) only signatures defined
{
creature.firstStep();
A class with one or more public abstract class Biped
while (creature.distanceTraveled () < distance)
abstract methods must be {
creature.nextStep();
declared abstract ...
creature.stop();
public abstract void firstStep();
}
public abstract void nextStep(); Abstract
public abstract void stop(); methods
Automatically call Walker’s methods if creature ...
is Walker, Hopper’s for Hopper, etc. public void moveAcross(..) { ... }
}
M. El Dick
Closer to the root of the hierarchy ... JTextComponent AbstractButton JPanel ...
M. El Dick M. El Dick
Abstract Classes (cont’d) Class Object
Cannot be instantiated (i.e., cannot create objects) Every class by default extends library class Object (from java.lang)
M. El Dick M. El Dick
M. El Dick M. El Dick
Final Interfaces
Final method:
cannot be overridden by subclasses
DanceFloor Aerobics
Final class: Interface
cannot be subclassed DanceGroup Waltz
Band Cha-Cha-Cha
Dancer Salsa
M. El Dick M. El Dick
Interfaces Interfaces
Like an abstract class, but no fields or constructors, and A concrete class that implements an interface must
only abstract methods supply all the methods of that interface
public class Waltz implements Dance
public interface Dance
{
{
...
DanceStep getStep (int i);
// Methods:
int getTempo ();
public DanceStep getStep (int i) { ... }
int getBeat (int i);
public int getTempo () { return 750; }
}
public int getBeat (int i) { ... }
...
“publicabstract” is not written because all the methods }
are public abstract
M. El Dick M. El Dick
M. El Dick M. El Dick
Classes Interfaces Classes Interfaces
Similarities Similarities
Provide secondary data Provide secondary data A concrete subclass of an A concrete class that implements
type to objects of its type to objects of classes abstract class must define all an interface must define all the
subclasses that implement it the inherited abstract methods specified by the
methods interface
M. El Dick M. El Dick
A class can extend only Has all its methods All declared methods
A class can implement
one class defined if concrete, one are abstract
several interfaces
or more abstract
Can have fields methods if abstract
Cannot have fields
(except, possibly, public
static final constants) May belong to a small
Is part of a hierarchy of hierarchy of
Have no constructors classes with Object at the interfaces, but this is
Define its own top not as common
constructors (or get
default constructor)
M. El Dick
Exercise 1
Exercises Find the errors in the code below
public abstract class A {
public class B extends A {
public double bar() {
System.out.println("B");
public abstract void bar() ; return 0.0;
System.out.println( "B" ); }
} public void bar() {}
}
public class C extends A {
public C(){
super( "C“ );
}
public void display() {
System.out.println( "C" );
M. El Dick }} M. El Dick
M. El Dick M. El Dick