Chapter 11 - Class hierarchies and interfaces. In this chapter, the learning objectives are: Understand class hierarchies and polymorphism, learn about abstract classes, learn the syntax for calling superclass’s constructors and methods, understand interfaces.
Trang 1Class Hierarchies and Interfaces
Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing All
rights reserved.
Java Methods
Object-Oriented Programming
and Data Structures
Maria Litvin ● Gary Litvin
2nd AP edition with GridWorld
C h a p
e
t r
Trang 2Objectives:
polymorphism
constructors and methods
Trang 3Inheritance
between objects: an object of a subclass IS-A(n) object of the superclass
Superclass (Base class)
Subclass (Derived class)
Subclass extends
Superclass
Trang 5Class Hierarchies (cont’d)
of code by factoring out
common code from
similar classes into a
common superclass
Actor Constructor Accessors
putSelfInGrid removeSelfFromGrid setColor
moveTo act
Bug Constructor
canMove move turn act (redefined)
Flower Constructor
act (redefined)
Trang 6Class Hierarchies (cont’d)
write more general methods in client classes
public void add
(Location loc, Bug occupant)
{
occupant.putSelfInGrid(getGrid(), loc);
}
public void add
(Location loc, Flower occupant)
{
occupant.putSelfInGrid(getGrid(), loc);
}
public void add
(Location loc, Actor occupant)
{ occupant.putSelfInGrid(getGrid(), loc); }
Works for a Bug or a Flower any Actor
Trang 7Abstract Classes
defined
public abstract class Actor
Trang 8Abstract Classes (cont’d)
superclasses for more specific classes
for the compiler to do additional error
checking
hierarchy; they describe more abstract
objects
Trang 9JTextComponent AbstractButton JPanel
JTextArea JTextField JMenuItem JButton
A fragment of Java library GUI class hierarchy (abstract classes are boxed)
Trang 10Abstract Classes (cont’d)
create objects of) abstract classes
they can be called from constructors of
subclasses
concrete
Trang 11• Object is a concrete class
public class Object
{
public String toString { }
public boolean equals (Object other) { }
public int hashCode() { }
// a few other methods
}
Methods redefined (overridden)
as necessary
Trang 12Calls Bug’s
constructor
If present, must be
the first statement
The number / types of parameters passed to
super must match parameters of one of the
superclass’s constructors.
Trang 13Invoking Superclass’s
Constructors (cont’d)
always called, but you don’t have to have an
superclass’s no-args constructor is called by default
Must be defined then If not defined syntax error:
Trang 14constructor, and so on, all the way up to
Object’s constructor
Actor Flower
Trang 15Calls Flower’s act
super.someMethod refers to someMethod in
the nearest class, up the inheritance line, where
someMethod is defined.
Trang 16Calling Superclass’s
Methods (cont’d)
• super-dot calls are often used in
subclasses of library classes:
public class Canvas extends JPanel
Trang 17Polymorphism
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 object’s superclass or some ancestor higher up the inheritance line
polymorphism is just there no need to do anything special
Trang 18The correct act method is
called automatically for any specific type of actor.
Trang 19Interface
Trang 20Interfaces (cont’d)
but it does not have any fields or constructors, and all its methods are abstract
public interface Dance
{ String getName ();
String getSteps (int m);
int[] getBeat ();
}
Trang 21Interfaces (cont’d)
implements an interface
must supply all the methods of that interface
public class Waltz implements Dance
{
.
public String getName( ) { return "Waltz"; }
public String getSteps(int m) { }
public int[] getBeat( ) { }
.
}
Trang 22Interfaces (cont’d)
secondary data type to objects of a class that implements that interface
an interface type
disguised as interface types
Dance d = new Waltz( );
Trang 23method is called for any specific
type of Edible (e.g., a Pancake)
public class Pancake implements Edible
{
}
Trang 24instantiated.
Similarities
Trang 25Classes Interfaces
an abstract class must
define all the inherited
abstract methods.
another class A
subclass can add
methods and override
some of its superclass’s
methods.
implements an interface must define all the
methods specified by the interface
another interface (called
its superinterface) by
adding declarations of abstract methods.
Similarities
Trang 26Classes Interfaces
one class
constructors (or gets a
default constructor)
any number of interfaces
have fields (except, possibly, some public static final constants).
constructors
Differences
Trang 27Classes Interfaces
its methods defined An
abstract class usually
has one or more
abstract methods.
a hierarchy of classes
an interface are abstract
to a small hierarchy of interfaces, but this is not very common
Differences
Trang 28RightShoe
LeftSandal RightSandal
DancingBug
DancingCrab
Maracas
EasySound
GridWorld’s framework
Trang 29Review
code using class hierarchies
rather than an empty method?
Is superclass’s constructor called?
Trang 30Review (cont’d)
paintComponent(g);
instead of
super.paintComponent(g);
?
Trang 31Review (cont’d)
abstract class and an interface?
interface type What type of value can be
assigned to that variable?
abstract classes?