Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.

Similar presentations


Presentation on theme: "© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing."— Presentation transcript:

1 © 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing class.  Classes that are derived from existing classes demonstrate an "is-a" relationship. A class "is a" type of another class.  There can be multiple levels of inheritance.

2 © 2007 Lawrenceville Press Slide 2 Chapter 9 The Puck Class Hierarchy

3 © 2007 Lawrenceville Press Slide 3 Chapter 9 Subclass Terminology  A class that inherits another class is called the subclass.  The class that is used to create the subclass is called the superclass.  The subclass is also called the derived class.  The superclass is also called the base class.

4 © 2007 Lawrenceville Press Slide 4 Chapter 9 A Subclass public class Disk extends Circle { private double thickness; public Disk(double r, double t) { super(r); thickness = t; }... public String toString() {... } subclass call to superclass constructor override of superclass method superclass

5 © 2007 Lawrenceville Press Slide 5 Chapter 9 Polymorphism  OOP property in which objects have the ability to assume different types  Based on inheritance  A superclass object can reference an subclass object: Circle wafer; Disk cookie = new Disk(2, 0.5); wafer = cookie;//Circle can refer to Disk /* displays: The disk has radius 2.0 and thickness 0.5 */ System.out.println(wafer);

6 © 2007 Lawrenceville Press Slide 6 Chapter 9 The Music Application

7 © 2007 Lawrenceville Press Slide 7 Chapter 9 Abstract Classes  Models an abstract concept  Cannot be instantiated  Declared with the keyword abstract  Intended to be inherited

8 © 2007 Lawrenceville Press Slide 8 Chapter 9 Abstract Methods  Member of an abstract class  Declared with the keyword abstract  Contains a method declaration but no body  Implemented in a subclass

9 © 2007 Lawrenceville Press Slide 9 Chapter 9 An Abstract Class abstract class Instrument {... abstract String makeSound(); } abstract class abstract method

10 © 2007 Lawrenceville Press Slide 10 Chapter 9 Interfaces  A class with method declarations that have no implementations  Cannot be inherited  Must be implemented in a class using the keyword implements  Adds behavior to a class, but does not provide a hierarchy for the class  A class can implement multiple interfaces


Download ppt "© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing."

Similar presentations


Ads by Google