Chapter 11 Inheritance and Polymorphism Part 1

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Inheritance and.
Advertisements

OOP: Inheritance By: Lamiaa Said.
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
Lecture 10: Inheritance Subclasses and superclasses The inheritance chain Access control The Object cosmic superclass The super keyword Overriding methods.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
1 Introduction to Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Intro to OOP with Java, C. Thomas Wu
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
1 Chapter 4 Inheritance and Polymorphism. 2 Objectives u To develop a subclass from a superclass through inheritance. u To invoke the superclass’s constructors.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 11 Inheritance and Polymorphism.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
COME 339 WEEK 1. Example: The Course Class 2 TestCourceRunCourse.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
1 Chapter 2 Inheritance and Polymorphism. 2 Objectives u To develop a subclass from a superclass through inheritance. u To invoke the superclass’s constructors.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
Lecture 4: Object Composition, Inheritance and Polymorphism Michael Hsu CSULA.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
EKT472: Object Oriented Programming Overloading and Overriding Method.
Inheritance.
Modern Programming Tools And Techniques-I
Class Inheritance Part I
Chapter 15 Abstract Classes and Interfaces
Lecture 4: Object Composition, Inheritance and Polymorphism
Overriding Method.
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
Chapter 5 Hierarchies IS-A associations superclasses subclasses
Lecture 10: Inheritance Subclasses and superclasses
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
Week 8 Lecture -3 Inheritance and Polymorphism
Continuing Chapter 11 Inheritance and Polymorphism
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
CSC 143 Inheritance.
Chapter 9 Inheritance and Polymorphism
Inheritance Basics Programming with Inheritance
Chapter 9 Object-Oriented Programming: Inheritance
Java Programming Language
Chapter 9: Polymorphism and Inheritance
Chapter 9 Inheritance and Polymorphism
Inheritance.
Computer Programming with JAVA
Java – Inheritance.
Chapter 11 Inheritance and Polymorphism
Java Programming, Second Edition
Java Inheritance.
Chapter 14 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 2
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
Chapter 11 Inheritance and Polymorphism
Presentation transcript:

Chapter 11 Inheritance and Polymorphism Part 1

Inheritance We use a class to model objects of the same type Some classes have some common properties & behaviors, which can be generalized in a class that can be shared by other classes We can define a new specialized class (subclass) that extends the existing generalized class (superclass). This is called class inheritance The subclasses inherit the properties & methods (except constructors) from the superclass class Fruit { // code } class Apple extends Fruit {

Superclasses & Subclasses SimpleGeometricObject CircleFromSimpleGeometricObject RectangleFromSimpleGeometricObject TestCircleRectangle Run

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 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

Are Superclass’s Constructor Inherited? No. They are invoked explicitly or implicitly. They can be invoked from the subclasses' constructors, using the keyword super If the keyword super is not explicitly used, the superclass's accessible no-arg constructor is automatically invoked

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,

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"); (1) Person's no-arg constructor is invoked (2) Invoke Employee’s overloaded constructor (3) Employee's no-arg constructor is invoked (4) Faculty's no-arg constructor is invoked

Trace Execution 1. Start from main() animation 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 main()

2. Invoke Faculty constructor animation 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

3. Invoke Employee’s no-arg constructor animation 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

4. Invoke Employee(String) constructor animation 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

5. Invoke Person() constructor animation 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

Trace Execution 6. Execute println animation 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

Trace Execution 7. Execute println animation 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

Trace Execution 8. Execute println animation 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

Trace Execution 9. Execute println animation 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

Example of the Impact of a Superclass without a no-arg constructor Find out the error in the following program: class Fruit { public Fruit(String name) { System.out.println("Fruit's constructor is invoked"); } public class Apple extends Fruit { A compile-time error will be generated indicating the need for a no-arg constructor in the superclass

Properties & Methods of Subclasses A subclass inherits properties and methods (except constructors) from a superclass You can also: Add new properties to the subclass Add new methods to the subclass Override (i.e. redefine) the implementation of the methods defined in the superclass. To override a method, the method must be defined in the subclass using the same signature and the same return type as in its superclass

Calling Superclass Methods public class Circle extends GeometricObject { // Other code /** Use toString() defined in GeometricObject */ public void printCircle() { return super.toString() } The super keyword in this case is optional because the subclass has inherited toString() from the superclass public class Circle extends GeometricObject { // Other code /** Override toString() defined in GeometricObject */ @Override public String toString() { return super.toString() + "\nradius is " + radius; } The super keyword in this case is necessary because out of the two versions of toString(), we are referring to superclass version here

Overriding Methods in the Superclass 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

Overriding vs. Overloading Same name/arguments, different body Same name, different arguments/body

The Object Class and its Methods Every class in Java is descended from the java.lang.Object class If no inheritance is specified when a class is defined, the superclass of the class is Object

toString() in Object toString() returns a String representation of the object The default implementation of toString() returns a String consisting of a class name of which the object is an instance, the at sign @, and a number representing this object Loan loan = new Loan(); System.out.println(loan.toString()); displays Loan@15037e5 This message is not very helpful or informative. Usually you should override toString() so that it returns an informative String representation of the object