Presentation is loading. Please wait.

Presentation is loading. Please wait.

One class is an extension of another.

Similar presentations


Presentation on theme: "One class is an extension of another."— Presentation transcript:

1 One class is an extension of another.
11/13/2018 4:38 PM 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. Refer to page 213 in the text. © 2007 Lawrenceville Press

2 Chapter 9 The Puck Class Hierarchy
11/13/2018 4:38 PM Chapter 9 The Puck Class Hierarchy Refer to page 213 in the text. The Puck class inherits the Disk class which inherits the Circle class. © 2007 Lawrenceville Press

3 Chapter 9 Subclass Terminology
11/13/2018 4:38 PM 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. Refer to page 214 in the text. © 2007 Lawrenceville Press

4 call to superclass constructor
11/13/2018 4:38 PM Chapter 9 A Subclass superclass subclass public class Disk extends Circle { private double thickness; public Disk(double r, double t) { super(r); thickness = t; } public String toString() { } call to superclass constructor Note: This slide contains animations. Press the space bar or click the mouse button to display each animation. This slide contains four (4) animations. Refer to pages 214 and 215 in the text. A subclass, or derived class, declaration includes the subclass name. <press space bar> Following the subclass name is the keyword extends. After the keyword extends is the superclass, or base class, name. <press space bar> The constructor in a subclass often makes a call to the superclass constructor to initialize variables inherited from the base class. <press space bar> Superclass methods, such as toString(), can be redefined, or overridden, in the subclass. <press space bar> override of superclass method © 2007 Lawrenceville Press

5 11/13/2018 4:38 PM 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); Refer to page 217 in the text. © 2007 Lawrenceville Press

6 Chapter 9 The Music Application
11/13/2018 4:38 PM Chapter 9 The Music Application Refer to page 217 in the text. © 2007 Lawrenceville Press

7 Chapter 9 Abstract Classes
11/13/2018 4:38 PM Chapter 9 Abstract Classes Models an abstract concept Cannot be instantiated Declared with the keyword abstract Intended to be inherited Refer to page 222 in the text. © 2007 Lawrenceville Press

8 Chapter 9 Abstract Methods
11/13/2018 4:38 PM 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 Refer to page 222 in the text. © 2007 Lawrenceville Press

9 Chapter 9 An Abstract Class
11/13/2018 4:38 PM Chapter 9 An Abstract Class abstract class abstract class Instrument { abstract String makeSound(); } abstract method Note: This slide contains animations. Press the space bar or click the mouse button to display each animation. This slide contains two (2) animations. Refer to page 222 in the text. An abstract class is declared with the keyword abstract. <press space bar> Abstract classes are not used to instantiate objects. They model an abstract concept and are intended to be inherited. The derived class based on the abstract class then implements the specifics. Abstract classes often include abstract methods. <press space bar> An abstract method is also declared with the keyword abstract. It contains only a declaration and no body. The implementation of the abstract method is in a subclass. © 2007 Lawrenceville Press

10 A class with method declarations that have no implementations
11/13/2018 4:38 PM 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 Refer to pages 225 and 226 in the text. © 2007 Lawrenceville Press


Download ppt "One class is an extension of another."

Similar presentations


Ads by Google