motivations chapter 11 inheritance and polymorphism
play

Motivations Chapter 11: Inheritance and Polymorphism Suppose you - PDF document

Motivations Chapter 11: Inheritance and Polymorphism Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the best way to design these classes so to avoid redundancy? The


  1. Motivations Chapter 11: Inheritance and Polymorphism Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the best way to design these classes so to avoid redundancy? The answer CS2: Data Structures and Algorithms is to use inheritance. Colorado State University Original slides by Daniel Liang Modified slides by Chris Wilcox Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 1 2 rights reserved. rights reserved. Objectives Superclasses and Subclasses To define a subclass from a superclass through inheritance (§11.2). ◆ GeometricObject To invoke the superclass’s constructors and methods using the super keyword -color: String The color of the object (default: white). ◆ -filled: boolean Indicates whether the object is filled with a color (default: false). (§11.3). -dateCreated: java.util.Date The date when the object was created. +GeometricObject() Creates a GeometricObject. To override instance methods in the subclass (§11.4). ◆ +GeometricObject(color: String, Creates a GeometricObject with the specified color and filled filled: boolean) values. To distinguish differences between overriding and overloading (§11.5). +getColor(): String Returns the color. ◆ +setColor(color: String): void Sets a new color. To explore the toString() method in the Object class (§11.6). ◆ +isFilled(): boolean Returns the filled property. +setFilled(filled: boolean): void Sets a new filled property. To discover polymorphism and dynamic binding (§§11.7–11.8). ◆ +getDateCreated(): java.util.Date Returns the dateCreated. +toString(): String Returns a string representation of this object. To describe casting and explain why explicit downcasting is necessary (§11.9). ◆ To explore the equals method in the Object class (§11.10). ◆ Rectangle Circle To store, retrieve, and manipulate objects in an ArrayList (§11.11). -radius: double -width: double ◆ -height: double +Circle() To enable data and methods in a superclass accessible from subclasses using the ◆ +Circle(radius: double) +Rectangle() GeometricObject +Rectangle(width: double, height: double) protected visibility modifier (§11.13). +Circle(radius: double, color: String, filled: boolean) +Rectangle(width: double, height: double +getRadius(): double color: String, filled: boolean) To prevent class extending and method overriding using the final modifier CircleFromSimpleGeometricObject ◆ +getWidth(): double +setRadius(radius: double): void (§11.14). +getArea(): double +setWidth(width: double): void RectangleFromSimpleGeometricObject +getPerimeter(): double +getHeight(): double +setHeight(height: double): void +getDiameter(): double +printCircle(): void +getArea(): double TestCircleRectangle Run +getPerimeter(): double Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 3 4 rights reserved. rights reserved.

  2. Are superclass’s Constructor Superclass’s Constructor Is Always Invoked Inherited? A constructor may invoke an overloaded constructor or its superclass’s constructor. If none of them is invoked No. They are not inherited. explicitly, the compiler puts super() as the first statement They are invoked explicitly or implicitly. in the constructor. For example, Explicitly using the super keyword. public A() { public A() { A constructor is used to construct an instance of a class. is equivalent to super(); } Unlike properties and methods, a superclass's } constructors are not inherited in the subclass. They can only be invoked from the subclasses' constructors, using public A(double d) { public A(double d) { the keyword super. If the keyword super is not explicitly is equivalent to // some statements super(); } // some statements used, the superclass's no-arg constructor is } automatically invoked. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 5 6 rights reserved. rights reserved. Using the Keyword super CAUTION The keyword super refers to the superclass You must use the keyword super to call the of the class in which super appears. This keyword can be used in two ways: superclass constructor. Invoking a superclass constructor’s name in a subclass ✦ To call a superclass constructor causes a syntax error. Java requires that the ✦ To call a superclass method statement that uses the keyword super appear first in the constructor. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 7 8 rights reserved. rights reserved.

  3. Constructor Chaining animation Trace Execution Constructing an instance of a class invokes all the superclasses’ constructors along the inheritance chain. This is known as constructor chaining . public class Faculty extends Employee { public static void main(String[] args) { public class Faculty extends Employee { 1. Start from the new Faculty(); public static void main(String[] args) { main method } new Faculty(); } public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); public Faculty() { } System.out.println("(4) Faculty's no-arg constructor is invoked"); } } } class Employee extends Person { public Employee() { class Employee extends Person { this("(2) Invoke Employee’s overloaded constructor"); public Employee() { System.out.println("(3) Employee's no-arg constructor is invoked"); this("(2) Invoke Employee’s overloaded constructor"); } System.out.println("(3) Employee's no-arg constructor is invoked"); } public Employee(String s) { System.out.println(s); public Employee(String s) { } System.out.println(s); } } } class Person { public Person() { class Person { System.out.println("(1) Person's no-arg constructor is invoked"); public Person() { } System.out.println("(1) Person's no-arg constructor is invoked"); } } } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 9 10 rights reserved. rights reserved. animation animation Trace Execution Trace Execution public class Faculty extends Employee { public class Faculty extends Employee { public static void main(String[] args) { public static void main(String[] args) { 2. Invoke Faculty new Faculty(); new Faculty(); constructor } } public Faculty() { public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); System.out.println("(4) Faculty's no-arg constructor is invoked"); } } 3. Invoke Employee’s no- } } arg constructor class Employee extends Person { class Employee extends Person { public Employee() { public Employee() { this("(2) Invoke Employee’s overloaded constructor"); this("(2) Invoke Employee’s overloaded constructor"); System.out.println("(3) Employee's no-arg constructor is invoked"); System.out.println("(3) Employee's no-arg constructor is invoked"); } } public Employee(String s) { public Employee(String s) { System.out.println(s); System.out.println(s); } } } } class Person { class Person { public Person() { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); System.out.println("(1) Person's no-arg constructor is invoked"); } } } } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 11 12 rights reserved. rights reserved.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend