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. | Class 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 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 work on the GridWorld Dance project. 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 the constructors. Class Hierarchies Using inheritance, a programmer can define a hierarchy of classes. 11- The class Actor is a generic class that represents any kind of “actor” in . | Class 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 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 work on the GridWorld Dance project. 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 the constructors. Class Hierarchies Using inheritance, a programmer can define a hierarchy of classes. 11- The class Actor is a generic class that represents any kind of “actor” in GridWorld. Class Hierarchies (cont’d) Help reduce duplication 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) 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 add (Location loc, Bug occupant) { (getGrid(), loc); } public void add (Location loc, Flower occupant) { (getGrid(), loc); } public void add (Location loc, Actor occupant) { (getGrid(), loc); } Works for a Bug or a Flower any Actor 11- So inheritance helps to eliminate duplicating code in two ways: (1) by factoring out common code and (2) by writing more .