1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.

Slides:



Advertisements
Similar presentations
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Object-Oriented Programming: Polymorphism Outline 10.1 Introduction 10.2 Relationships Among.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
Road Map Introduction to object oriented programming. Classes
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
1 Evan Korth New York University abstract classes and casting Professor Evan Korth New York University.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Computer Science I Inheritance Professor Evan Korth New York University.
Object-Oriented Programming: Polymorphism
Object Oriented Programming: Inheritance Chapter 9.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Lecture 9 Polymorphism Richard Gesick.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Java Inheritance.
1 Object-Oriented Programming: Polymorphism 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Superclass Methods.
What is inheritance? It is the ability to create a new class from an existing class.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Object-Oriented Programming: Polymorphism Chapter 10.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Object Oriented Programming: Inheritance Chapter 9.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance and Polymorphism
Object-Oriented Programming: Inheritance
Week 4 Object-Oriented Programming (1): Inheritance
abstract classes and casting objects
Road Map Inheritance Class hierarchy Overriding methods Constructors
Lecture 23 Polymorphism Richard Gesick.
Lecture 22 Inheritance Richard Gesick.
Inheritance, Polymorphism, and Interfaces. Oh My
Object Oriented Programming: Inheritance
Java Inheritance.
Polymorphism and Type Casting
Fundaments of Game Design
Object-Oriented Programming: Inheritance
Chapter 8 Class Inheritance and Interfaces
Presentation transcript:

1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University

2 Evan Korth New York University Road map Object class Polymorphism Superclass / subclass references and casting Reading –Liang 5: 8.5, 8.6 –Liang 6: 9.5 – 9.7, –Liang 7: 10.6 – 10.8

3 Evan Korth New York University review Define superclass. Define inheritance. Why do you need the keyword super ? –What are the two uses of super ? If you want to call a superclass constructor (with non zero parameters) in your subclass' constructor where must you call it? What is the difference between overloading and overriding?

4 Evan Korth New York University Review (cont) Given the following class: public class Bicycle extends Vehicle When you call one of Bicycle's constructors, which constructors are called and in which order do they do their work? When a client program invokes an object's methods, how does it use the keyword super? When a client program invokes an object's methods, what is the difference in syntax between invoking a method declared in the object vs. one that is directly inherited? How about indirectly inherited?

 2003 Prentice Hall, Inc. All rights reserved protected members and accessing a superclass member protected access –Intermediate level of protection between public and private –protected members accessible to superclass members subclass members Class members in the same package Subclass access superclass member –Keyword super and a dot (.)

 2003 Prentice Hall, Inc. All rights reserved Relationship between Superclasses and Subclasses (Cont.) Using protected instance variables –Advantages subclasses can modify values directly Slight increase in performance –Avoid set/get function call overhead –Disadvantages No validity checking –subclass can assign illegal value Implementation dependent –subclass methods more likely dependent on superclass implementation –superclass implementation changes may result in subclass modifications Fragile (brittle) software

7 Evan Korth New York University Visibility modifiers for data and methods private – only seen in class (UML -) default (no visibility modifier) – private access plus classes in the package (UML none) protected – default access plus subclasses and superclasses (UML #) public – protected access plus everything else (UML +) Weakest access privileges Strongest access privileges

8 Evan Korth New York University Overload vs override (review) Overloading a method refers to having two methods which share the same name but have different signatures. Overriding a method refers to having a new implementation of a method with the same signature in a subclass.

9 Evan Korth New York University Object class In Java, all classes are derived from other classes except the Object class which is the top of Java’s class hierarchy. Therefore, if a new class does not explicitly extend another class, it implicitly extends the Object class. Several of the methods provided in the Object class are provided with the intention that they will be overridden.

10 Evan Korth New York University Object class: equals method public boolean equals (Object object) Object’s equals() method returns true if the Objects are the same Object (ie the two variables refer to the same position in memory) Since you can already check for that condition with the == operator, you are meant to override the equals() method with one that will check to see if the two objects (explicit and implicit) have the same fields (or some other definition of equality).

11 Evan Korth New York University Object class: toString method public String toString() Object’s toString() method returns the name of the class of the object plus sign and a number representing the object. You should override toString() to return a String that more closely represents the object. Whenever you print an object, the result of method toString() is what will be printed.

 2003 Prentice Hall, Inc. All rights reserved. 12 Introduction to Polymorphism Polymorphism –“Program in the general” –Treat objects in same class hierarchy as if all are superclass objects –Abstract class (next session) Common functionality Cannot instantiate –Makes programs extensible New classes added easily, can still be processed

 2003 Prentice Hall, Inc. All rights reserved. 13 Relationships Among Objects in an Inheritance Hierarchy In our examples –Use superclass GeometricObject (will change to an abstract class soon) Defines common interface (functionality) Rectangle, Circle and Cylinder inherit from GeometricObject –Class Employee is a natural example Previously –Circle inherited from GeometricObject –Manipulated Rectangle and Circle objects using references to invoke methods Now –Using superclass references with subclass-type variables –Subclass method calls via superclass-type variables Key concept –subclass object can be treated as superclass object “is-a” relationship –superclass object cannot be treated as subclass object No “is-a” relationship

Superclass / Subclass Relations Try to reference a superclass object with a subclass variable: –For example: –Circle c = new GeometricObject(); –Is a GeometricObject a Circle?

Superclass / Subclass Relations Try to reference a superclass object with a subclass variable: –For example: –Circle c = new GeometricObject(); –Is a GeometricObject a Circle? No, a superclass object is not a necessarily subclass object: –Our example: –A GeometricObject is a GeometricObject –A GeometricObject is not a Circle (i.e. a Circle is more specialized GeometricObject) –Does not have an “is-a” relationship.

Superclass / Subclass Relations Try to reference a subclass object with a superclass variable: –For example: –GeometricObject g = new Circle(); –Is a Circle a GeometricObject?

Superclass / Subclass Relations Try to reference a subclass object with a superclass variable: –For example: –GeometricObject g = new Circle(); –Is a Circle a GeometricObject? Yes, a subclass object is a superclass object: –Our example: –A Circle is a GeometricObject (i.e. a Circle is more specialized GeometricObject) –A Circle is a Circle –Does have an “is-a” relationship.

18 Evan Korth New York University Limitation of using superclass reference You cannot call a subclass’ method using a superclass reference if the method has not been inherited from the superclass. For example: Object o = new Circle(6); o.getArea() Causes a compile error

 2003 Prentice Hall, Inc. All rights reserved Polymorphism Examples Examples –Suppose Rectangle derives from Quadrilateral Rectangle more specific than Quadrilateral Any operation on Quadrilateral can be done on Rectangle (i.e., perimeter, area) Suppose designing video game –Superclass SpaceObject Subclasses Martian, SpaceShip, LaserBeam Contains method draw –To refresh screen Send draw message to each object Same message has “many forms” of results

 2003 Prentice Hall, Inc. All rights reserved Polymorphism Examples Video game example, continued –Easy to add class Mercurian Extends SpaceObject Provides its own implementation of draw –Programmer does not need to change code Calls draw regardless of object’s type Mercurian objects “plug right in”