Professional Documents
Culture Documents
A & AB
Object-Oriented Programming and Data Structures
Maria Litvin Gary Litvin
11
A
Chap
Apt
Chapter
Objectives:
Understand class hierarchies and
polymorphism
Learn about abstract classes Learn the syntax for calling superclasss
constructors and methods
Understand interfaces
11-2
Inheritance
Superclass (Base class)
Subclass extends Superclass
Class Hierarchies
Using inheritance, a programmer can define a
hierarchy of classes.
Biped Walker ToedInWalker Hopper Dancer
CharlieChaplin
11-4
(Walker
{
creature.firstStep(); while (creature.distanceTraveled() < distance) creature.nextStep(); public void moveAcross creature.stop(); } (Biped creature, int distance) { public void moveAcross creature.firstStep(); while (creature.distanceTraveled() < distance) (Hopper creature, int distance) creature.nextStep(); { creature.stop(); creature.firstStep(); while (creature.distanceTraveled() < distance) }
creature.nextStep(); creature.stop();
} 11-6
Polymorphism
Ensures that the correct method is called for
an object of a specific type, even when that object is disguised as a reference to a more generic type, that is, the type of the objects superclass or some ancestor higher up the inheritance line.
11-7
Polymorphism (contd)
The actual parameter passed to this method can be a Walker, a Hopper, etc. any subclass of Biped. public void moveAcross (Biped creature, int distance) { creature.firstStep(); while (creature.distanceTraveled () < distance) creature.nextStep(); creature.stop(); } Correct methods will be called automatically for any specific type of creature: Walkers methods for Walker, Hoppers for Hopper, etc.
11-8
Abstract Classes
Some of the methods in a class can be
declared abstract and left with only signatures defined
Abstract methods
11-9
...
Component
...
Button ...
TextComponent
Container
JComponent
Window
...
JTextComponent
AbstractButton
JPanel
...
...
JButton
...
A fragment of Java library GUI class hierarchy (abstract classes are boxed)
11-11
11-12
Class Object
In Java every class by default extends a
library class Object (from java.lang)
11-13
Biped Walker
public class Walker extends Biped { // Constructor public Walker(int x, int y, Image leftPic, Image rightPic) { Calls Bipeds super(x, y, leftPic, rightPic); ... constructor } The number / types of parameters passed to } super must match parameters of one of the
If present, must be the first statement superclasss constructors.
11-14
11-15
Biped
super(...)
Walker
11-16
Walker CharlieChaplin
public class CharlieChaplin extends Walker { ... public void nextStep () { turnFeetIn(); Calls Walkers super.nextStep(); nextStep turnFeetOut(); } ... super.someMethod refers to someMethod in } the nearest class, up the inheritance line, where someMethod is defined.
11-17
Interfaces
DanceFloor
DanceGroup ControlPanel Band Dancer
Interface
Aerobics
Waltz Rumba Cha-Cha-Cha Salsa
Dance
11-19
Interfaces (contd)
An interface in Java is like an abstract class,
but it does not have any fields or constructors, and all its methods are abstract.
public interface Dance { DanceStep getStep (int i); int getTempo (); int getBeat (int i); }
Interfaces (contd)
We must officially state that a class
implements an interface.
Interfaces (contd)
A class can implement several interfaces. Like an abstract class, an interface supplies a
secondary data type to objects of a class that implements that interface.
Interfaces (contd)
public interface Edible { String getFoodGroup(); int getCaloriesPerServing(); } public class Pancake implements Edible { ... }
public class Breakfast Polymorphism: { the correct private int myTotalCalories = 0; method is called ... for any specific public void eat (Edible obj, int servings) type of Edible, { e.g., a Pancake myTotalCalories += obj.getCaloriesPerServing () * servings; } ... }
11-23
Classes
Similarities
Interfaces
An interface provides a
secondary data type to objects of classes that implement that interface.
A superclass provides a
secondary data type to objects of its subclasses.
An abstract class
cannot be instantiated.
An interface cannot be
instantiated.
11-24
Classes
Similarities
Interfaces
A concrete class that
implements an interface must define all the methods specified by the interface.
A concrete subclass of
an abstract class must define all the inherited abstract methods.
Classes
Differences
Interfaces
A class can implement
any number of interfaces.
An interface cannot
have fields (except, possibly, some public static final constants).
An interface has no
constructors.
11-26
Classes
Differences
Interfaces
All methods declared in
an interface are abstract.
An interface may
belong to a small hierarchy of interfaces, but this is not as common.
11-27
Dance Studio
interface
Band
interface
Biped
Dancer
Dance
Foot
DanceStep
AbstractDance
CoordinateSystem
depends on extends
implements has
11-28
Review
Describe two ways for eliminating duplicate
code using class hierarchies.
Review (contd)
Can an abstract class be instantiated? Can someMethod1 have a call
super.someMethod2 ( )?
?
11-30
Review (contd)
What is the main difference between an
abstract class and an interface?
11-31