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.
Java Methods Object-Oriented Programming and Data Structures 2nd AP edition with GridWorld Maria Litvin ● Gary Litvin C h a p t e r Class Hierarchies and Interfaces Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing All rights reserved Objectives: • Understand class hierarchies and polymorphism • Learn about abstract classes • Learn the syntax for calling superclass’s constructors and methods • Understand interfaces 112 Inheritance Superclass (Base class) Subclass extends Superclass Subclass (Derived class) • Inheritance represents the IS-A relationship between objects: an object of a subclass IS-A(n) object of the superclass 113 Class Hierarchies • Using inheritance, a programmer can define a hierarchy of classes Actor Flower WiltingFlower Rock BoxBug Bug UTurnBug 114 Class Hierarchies (cont’d) • Help reduce duplication of code by factoring out common code from similar classes into a common superclass Bug Constructor canMove move turn act (redefined) Actor Constructor Accessors putSelfInGrid removeSelfFromGrid setColor moveTo act Flower Constructor act (redefined) 115 Class Hierarchies (cont’d) • Help reduce duplication of code by letting you 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); } Works for a Bug or a Flower any Actor public void add (Location loc, Actor occupant) { occupant.putSelfInGrid(getGrid(), loc); } 116 Abstract Classes • Some of the methods in a class can be declared abstract and left with only signatures defined • A class with one or more abstract methods must be declared abstract public abstract class Actor { public abstract void act(); } Abstract method 117 Abstract Classes (cont’d) • Abstract classes serve as common superclasses for more specific classes • An abstract method provides an opportunity for the compiler to additional error checking • Abstract classes are closer to the root of the hierarchy; they describe more abstract objects 118 Abstract Classes (cont’d) Object Component Button TextComponent JTextComponent Container JComponent AbstractButton JTextArea JTextField JMenuItem Window JPanel JButton A fragment of Java library GUI class hierarchy (abstract classes are boxed) 119 Abstract Classes (cont’d) • Java does not allow us to instantiate (that is, create objects of) abstract classes • Still, an abstract class can have constructors they can be called from constructors of subclasses • A class with no abstract methods is called concrete 1110 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 object’s superclass or some ancestor higher up the inheritance line • Once you define a common superclass, polymorphism is just there anything special no need to 1117 Polymorphism (cont’d) public void step() { for (Actor a : actors) { // only act if another actor hasn't removed a if (a.getGrid() == gr) a.act(); The correct act method is } called automatically for any } specific type of actor 1118 Interfaces YMCA DancingBug DancingCrab Dance Waltz Conga Maracas Interface 1119 Interfaces (cont’d) • 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 { String getName (); String getSteps (int m); int[] getBeat (); } • “public abstract” is not written because all the methods are public abstract 1120 Interfaces (cont’d) • We must “officially” state that a class implements an interface • A concrete class that 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( ) { } } 1121 Interfaces (cont’d) • 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 • You can declare variables and parameters of an interface type Dance d = new Waltz( ); • Polymorphism fully applies to objects disguised as interface types 1122 Interfaces (cont’d) 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; } } 1123 Classes Interfaces Similarities • A superclass provides a secondary data type to objects of its subclasses • An abstract class cannot be instantiated • An interface provides a secondary data type to objects of classes that implement that interface • An interface cannot be instantiated 1124 Classes Interfaces Similarities • A concrete subclass of an abstract class must define all the inherited abstract methods • A class can extend another class A subclass can add methods and override some of its superclass’s methods • A concrete class that implements an interface must define all the methods specified by the interface • An interface can extend another interface (called its superinterface) by adding declarations of abstract methods 1125 Classes Interfaces Differences • A class can extend only one class • A class can implement any number of interfaces • A class can have fields • An interface cannot • A class defines its own • An interface has no constructors (or gets a default constructor) have fields (except, possibly, some public static final constants) constructors 1126 Classes Interfaces Differences • A concrete class has all its methods defined An abstract class usually has one or more abstract methods • Every class is a part of a hierarchy of classes with Object at the top • All methods declared in an interface are abstract • An interface may belong to a small hierarchy of interfaces, but this is not very common 1127 DanceNumber GridWorld Dance YMCANumber WaltzNumber CongaNumber YMCA Waltz Conga GridWorld’s framework «interface» Dance info.gridworld actor.Actor «Abstract» Dancer LeftShoe RightShoe LeftSandal RightSandal DancingBug DancingCrab Maracas depends on A extends implements B EasySound 1128 Review • Describe two ways for eliminating duplicate code using class hierarchies • What is an abstract class? • Why is it better to use an abstract method rather than an empty method? • Define concrete class • What happens when a constructor of a subclass does not have a super statement? Is superclass’s constructor called? 1129 Review (cont’d) • Can an abstract class be instantiated? • Can someMethod1 have a call super.someMethod2 ( )? • What happens if, by mistake, a programmer puts in his paintComponent method a call paintComponent(g); instead of super.paintComponent(g); ? 1130 Review (cont’d) • What is the main difference between an abstract class and an interface? • Can a class implement several interfaces? • Suppose you declare a variable of an interface type What type of value can be assigned to that variable? • What is the main advantage of interfaces over abstract classes? 1131 ...Objectives: • Understand class hierarchies and polymorphism • Learn about abstract classes • Learn the syntax for calling superclass’s constructors and methods • Understand interfaces 11? ?2 Inheritance... Subclass (Derived class) • Inheritance represents the IS-A relationship between objects: an object of a subclass IS-A(n) object of the superclass 11? ?3 Class Hierarchies • Using inheritance, a programmer... JMenuItem Window JPanel JButton A fragment of Java library GUI class hierarchy (abstract classes are boxed) 11? ?9 Abstract Classes (cont’d) • Java does not allow us to instantiate (that is,