Chapter 11 - Class hierarchies and interfaces. The main goals of this chapter are to: Understand class hierarchies and polymorphism, learn about abstract classes, learn the syntax for calling superclass’s constructors and methods, understand interfaces. | Class Hierarchies and Interfaces Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. 11- Class hierarchies is the heart of OOP. Objectives: Understand class hierarchies and polymorphism Learn about abstract classes Learn the syntax for calling superclass’s constructors and methods Understand interfaces 11- Also finish the Dance Studio project, started in Chapter 3. Inheritance Inheritance represents the IS-A relationship between objects: an object of a subclass IS-A(n) object of the superclass. Superclass (Base class) Subclass (Derived class) Subclass extends Superclass 11- A subclass inherits all the fields and methods of its superclass, but not constructors. Class Hierarchies Using inheritance, a programmer can define a hierarchy of classes. Biped Walker Hopper Dancer ToedInWalker CharlieChaplin 11- The class Biped is a generic class | Class Hierarchies and Interfaces Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. 11- Class hierarchies is the heart of OOP. Objectives: Understand class hierarchies and polymorphism Learn about abstract classes Learn the syntax for calling superclass’s constructors and methods Understand interfaces 11- Also finish the Dance Studio project, started in Chapter 3. Inheritance Inheritance represents the IS-A relationship between objects: an object of a subclass IS-A(n) object of the superclass. Superclass (Base class) Subclass (Derived class) Subclass extends Superclass 11- A subclass inherits all the fields and methods of its superclass, but not constructors. Class Hierarchies Using inheritance, a programmer can define a hierarchy of classes. Biped Walker Hopper Dancer ToedInWalker CharlieChaplin 11- The class Biped is a generic class that represents any kind of “creature” that moves on two legs. (The term biped comes from Latin: bi means “two,” and ped means “foot.”) Class Hierarchies (cont’d) Help reduce duplication of code by factoring out common code from similar classes into a common superclass. Biped Constructor Accessors turnLeft turnRight turnAround draw Walker Constructor firstStep nextStep stop distanceTraveled Hopper Constructor firstStep nextStep stop distanceTraveled 11- If two classes share a lot of code, it makes sense to define a common superclass and factor out common code into it. Class Hierarchies (cont’d) Help reduce duplication of code by letting you write more general methods in client classes. public void moveAcross (Walker creature, int distance) { (); while (() < distance) (); (); } public void moveAcross (Hopper creature, int distance) { (); while (() < distance) .