Chapter 11 Inheritance and Polymorphism

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.
Chapter 14 Abstract Classes and Interfaces 1. Objectives To design and use abstract classes (§14.2). To process a calendar using the Calendar and GregorianCalendar.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
This keyword.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
1 Introduction to Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding.
UML Basics & Access Modifier
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.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
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,
Object Oriented programming Instructor: Dr. Essam H. Houssein.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Abstract Classes Course Lecture Slides 7 June 2010 “None of the abstract.
1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different.
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 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
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.
Inheritance.
Chapter 11 Inheritance and Polymorphism
Modern Programming Tools And Techniques-I
Chapter 15 Abstract Classes and Interfaces
Sixth Lecture ArrayList Abstract Class and Interface
Inheritance ITI1121 Nour El Kadri.
Lecture 4: Object Composition, Inheritance and Polymorphism
Overriding Method.
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
Road Map Inheritance Class hierarchy Overriding methods Constructors
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.
Chapter 13 Abstract Classes and Interfaces
CS Week 13 Jim Williams, PhD.
CS 302 Week 10 Jim Williams.
Chapter 9 Inheritance and Polymorphism
Inheritance Chapter 5.
Chapter 19 Generics Dr. Clincy - Lecture.
Java Programming Language
Chapter 9 Inheritance and Polymorphism
Java – Inheritance.
Chapter 11 Inheritance and Polymorphism
Chapter 14 Abstract Classes and Interfaces
Chapter 8 Class Inheritance and Interfaces
CS 200 More Classes Jim Williams, PhD.
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Encapsulation and Polymorphism
Chapter 11 Inheritance and Polymorphism
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Chapter 11 Inheritance and Polymorphism Dr. Clincy ------ Lecture

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Overview Lab 3 Dr. Clincy ------ Lecture