Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 11 Inheritance and Polymorphism

Similar presentations


Presentation on theme: "Chapter 11 Inheritance and Polymorphism"— Presentation transcript:

1 Chapter 11 Inheritance and Polymorphism
Dr. Clincy Lecture

2 Inheritance Suppose you needed to define a potential new class (Class A) that had many common and overlapping features with an existing class (Class B). What is the best way to design this class so to avoid redundancy? ANSWER: Define Class A from Class B This is called inheritance OO languages are designed to easily handle inheritance Terms: Class A is called the subclass (specialized-class/child-class/extended-class/derived-class) and Class B is called the Superclass (general-class/parent-class/base-class) Terminology: Class A is extended from Class B Dr. Clincy Lecture

3 Inheritance Suppose you needed to define a potential new class (Class A) that generalizes many common features amongst several other classes (Class B, Class C and Class D). Class A would be the Superclass and Classes B, C and D would be the Subclasses Classes B, C and D would inherit the methods and data from Class A NOTE1: a subclass is NOT a subset of a superclass, it may include more attributes and methods NOTE2: private data field are NOT accessible to the subclass. The subclass cannot directly access them NOTE3: the superclass of all Java classes is the java.lang.Object class Dr. Clincy Lecture

4 Example Superclass New methods Subclasses
In addition to their existing methods and data – they inherit GeometricObject’s methods and data Dr. Clincy Lecture

5 Example – How is it implemented ?
public class GeometricObject { . Subclass Superclass public class Circle extends GeometricObject { . public class Rectangle extends GeometricObject { . The keyword extends tells the compiler that the subclass extends the superclass, thus, the subclass inherits the superclass’ methods Dr. Clincy Lecture

6 GeometricObject Recall: The this keyword is needed to reference a data field hidden by a method or to invoke an overloaded constructor public class GeometricObject1 { private String color = "white"; private boolean filled; private java.util.Date dateCreated; /** Construct a default geometric object */ public GeometricObject1() { dateCreated = new java.util.Date(); } /** Construct a geometric object with the specified color * and filled value */ public GeometricObject1(String Color, boolean filled) { this.color = color; this.filled = filled; /** Return color */ public String getColor() { return color; /** Set a new color */ public void setColor(String color) { /** Return filled. Since filled is boolean, its get method is named isFilled */ public boolean isFilled() { return filled; /** Set a new filled */ public void setFilled(boolean filled) { /** Get dateCreated */ public java.util.Date getDateCreated() { return dateCreated; /** Return a string representation of this object */ public String toString() { return "created on " + dateCreated + "\ncolor: " + color + " and filled: " + filled; Dr. Clincy Lecture

7 Circle Dr. Clincy ------ Lecture
public class Circle extends GeometricObject1 { private double radius; public Circle() { } public Circle(double radius) { this.radius = radius; public Circle(double radius, String color, boolean filled) { setColor(color); setFilled(filled); /** Return radius */ public double getRadius() { return radius; /** Set a new radius */ public void setRadius(double radius) { /** Return area */ public double getArea() { return radius * radius * Math.PI; /** Return diameter */ public double getDiameter() { return 2 * radius; /** Return perimeter */ public double getPerimeter() { return 2 * radius * Math.PI; /* Print the circle info */ public void printCircle() { System.out.println(toString() + "The circle is created " + getDateCreated() + " and the radius is " + radius); Dr. Clincy Lecture

8 Rectangle Dr. Clincy ------ Lecture
public class Rectangle extends GeometricObject1 { private double width; private double height; public Rectangle() { } public Rectangle(double width, double height) { this.width = width; this.height = height; public Rectangle(double width, double height, String color, boolean filled) { setColor(color); setFilled(filled); /** Return width */ public double getWidth() { return width; /** Set a new width */ public void setWidth(double width) { /** Return height */ public double getHeight() { return height; /** Set a new height */ public void setHeight(double height) { /** Return area */ public double getArea() { return width * height; /** Return perimeter */ public double getPerimeter() { return 2 * (width + height); Dr. Clincy Lecture

9 Are superclass’s Constructor Inherited?
No. They are not inherited. They are invoked explicitly or implicitly. Explicitly using the super keyword. A constructor is used to construct an instance of a class. Unlike properties and methods, a superclass's constructors are not inherited in the subclass. They can only be invoked from the subclasses' constructors, using the keyword super. If the keyword super is not explicitly used, the superclass's no-arg constructor is automatically invoked. Dr. Clincy Lecture

10 Superclass’s Constructor Is Always Invoked
A constructor may invoke an overloaded constructor or its superclass’s constructor. If none of them is invoked explicitly, the compiler puts super() as the first statement in the constructor. For example, Dr. Clincy Lecture

11 Using the Keyword super
The keyword super refers to the superclass of the class in which super appears. This keyword can be used in two ways: To call a superclass constructor To call a superclass method Dr. Clincy Lecture

12 CAUTION You must use the keyword super to call the superclass constructor. Invoking a superclass constructor’s name in a subclass causes a syntax error. Java requires that the statement that uses the keyword super appear first in the constructor. Dr. Clincy Lecture

13 Constructor Chaining 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) { new Faculty(); } public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); class Employee extends Person { public Employee() { 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); class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); Dr. Clincy Lecture

14 1. Start from the main method
Trace Execution public class Faculty extends Employee { public static void main(String[] args) { new Faculty(); } public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); class Employee extends Person { public Employee() { 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); class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); 1. Start from the main method Dr. Clincy Lecture

15 2. Invoke Faculty constructor
Trace Execution public class Faculty extends Employee { public static void main(String[] args) { new Faculty(); } public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); class Employee extends Person { public Employee() { 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); class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); 2. Invoke Faculty constructor Dr. Clincy Lecture

16 3. Invoke Employee’s no-arg constructor
Trace Execution public class Faculty extends Employee { public static void main(String[] args) { new Faculty(); } public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); class Employee extends Person { public Employee() { 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); class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); 3. Invoke Employee’s no-arg constructor Dr. Clincy Lecture

17 4. Invoke Employee(String) constructor
Trace Execution public class Faculty extends Employee { public static void main(String[] args) { new Faculty(); } public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); class Employee extends Person { public Employee() { 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); class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); 4. Invoke Employee(String) constructor Dr. Clincy Lecture

18 5. Invoke Person() constructor
Trace Execution public class Faculty extends Employee { public static void main(String[] args) { new Faculty(); } public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); class Employee extends Person { public Employee() { 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); class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); 5. Invoke Person() constructor Dr. Clincy Lecture

19 Trace Execution 6. Execute println
public class Faculty extends Employee { public static void main(String[] args) { new Faculty(); } public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); class Employee extends Person { public Employee() { 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); class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); 6. Execute println Dr. Clincy Lecture

20 Trace Execution 7. Execute println
public class Faculty extends Employee { public static void main(String[] args) { new Faculty(); } public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); class Employee extends Person { public Employee() { 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); class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); 7. Execute println Dr. Clincy Lecture

21 Trace Execution 8. Execute println
public class Faculty extends Employee { public static void main(String[] args) { new Faculty(); } public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); class Employee extends Person { public Employee() { 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); class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); 8. Execute println Dr. Clincy Lecture

22 Trace Execution 9. Execute println
public class Faculty extends Employee { public static void main(String[] args) { new Faculty(); } public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); class Employee extends Person { public Employee() { 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); class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); 9. Execute println Dr. Clincy Lecture

23 Defining a Subclass A subclass inherits from a superclass. You can also: Add new properties Add new methods Override the methods of the superclass To override a method from a superclass, the method defined in the subclass must use the same exact signature as in its superclass A subclass inherits methods from a superclass. Sometimes it is necessary for the subclass to modify the implementation of a method defined in the superclass. This is referred to as method overriding. Dr. Clincy Lecture

24 NOTES An instance method can be overridden only if it is accessible.
Thus a private method cannot be overridden, because it is not accessible outside its own class. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated. Like an instance method, a static method can be inherited. However, a static method cannot be overridden. If a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden. Dr. Clincy Lecture

25 Overriding Vs. Overloading
To override means to provide a new implementation for a method in a subclass To overload means to define multiple methods with the same name but different signatures Dr. Clincy Lecture

26 Overview Lab 3 Dr. Clincy Lecture


Download ppt "Chapter 11 Inheritance and Polymorphism"

Similar presentations


Ads by Google