A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.

Slides:



Advertisements
Similar presentations
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 4 : Polymorphism King Fahd University of Petroleum & Minerals College of Computer.
Advertisements

CE203 - Application Programming Autumn 2013CE203 Part 31 Part 3.
Big Ideas behind Inheritance. Can you think of some possible examples of inheritance hierarchies?
Web Application Development Slides Credit Umair Javed LUMS.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
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.
Inheritance and Polymorphism CS180 Fall Definitions Inheritance – object oriented way to form new classes from pre-existing ones –Superclass The.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Polymorphism What is Polymorphism? Taking Advantage of Polymorphism
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
Chapter 10: Inheritance and Polymorphism
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
More about inheritance Exploring polymorphism 3.0.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
CS 61B Data Structures and Programming Methodology July David Sun.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)
Programming With Java ICS Chapter 8 Polymorphism.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Inheritance and Access Control CS 162 (Summer 2009)
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Georgia Institute of Technology More on Creating Classes part 3 Barb Ericson Georgia Institute of Technology Nov 2005.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Inheritance ndex.html ndex.htmland “Java.
Inheritance and Polymorphism
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Design issues for Object-Oriented Languages
Modern Programming Tools And Techniques-I
Object-oriented Programming in Java
Polymorphism, Abstract Classes & Interfaces
Inheritance and Polymorphism
Web Design & Development Lecture 5
Overloading and Constructors
Java Programming Language
More about inheritance
Polymorphism, Abstract Classes & Interfaces
Java Inheritance.
Polymorphism.
Inheritance in Java CS 3331 Fall 2009.
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Advanced Programming in Java
Presentation transcript:

A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A subclass may not redefine a public method as private A subclass may not override static methods of the superclass A subclass should define its own constructors A subclass cannot directly access the private members of its superclass. It must use accessor or mutators Rules for subclasses

Constructors Constructors are never inherited. If no constructor for child is created it will call the default from the parent. If not default is in the parent you will get a compile error.

Inheritance & Polymorphism

Inheritance Person Student Employee GradStudentUndergrad The arrow points to its superclass. The arrow designates the is-a relationship. The Employee is a person The Student is a Person No! A Person may not be a student.

Testing for Inheritance First Test Is – a Ask is this a type of this You can’t cast something to something it is not a type of BasketballPlayer is a CollegeBBPlayer (no) CollegeBBPlayer is a BasketballPlayer (yes) BasketballPlayer CollegeBBPlayer ProBBPlayer

Testing for Inheritance Second test is the instanceof Instanceof checks to see if the object is a type of another object public void processPlayers(BasketballPlayer b) { if(b instanceof ProBBPlayer) System.out.println(b.getDescription()); if(b instanceof CollegeBBPlayer) System.out.println(b.getDescription()); }

7 Polymorphism Polymorphism: to assign multiple meanings to the same method name getDescription(); toString(); These methods can refer to objects of their own class or to objects of the classes inherited from their class Decision is made at run-time called late or dynamic binding

Declaring subclass objects When an object is created, the reference can refer not only to an object of the superclass, but also to objects of any of its subclasses. BasketballPlayer b = new BasketballPlayer(); BasketballPlayer b2 = new CollegeBBPlayer(); BasketballPlayer b3 = new ProBBPlayer(); This is known as downcasting. It is implicit. There is no need to perform a cast. (Similar to no need to cast an int to a double)

Method is in all classes BasketballPlayer b1 = new BasketballPlayer("Basketball Player", 0); BasketballPlayer c1 = new CollegeBBPlayer("College player", 0, "Ball"); BasketballPlayer p1 = new ProBBPlayer("Pro Player", 0, 0); What printed? Static or early binding: Compiler checked to see if a getDescription() method was in the BasketballPlayer class. If so it compiled. Dynamic or late binding: runtime checked to see the actual type of object it was and found the method in that class. public String getDescription() { return “College Player”; } public String getDescription() { return "Basketball player"; }

Static or Early Binding & Late or Dynamic Binding Static or Early Binding is at Compile time public String getDescription() { return "Basketball player"; } BasketballPlayer b1 = new BasketballPlayer("Basketball Player", 0); BasketballPlayer c1 = new CollegeBBPlayer("College player", 0, "Ball"); BasketballPlayer p1 = new ProBBPlayer("Pro Player", 0, 0); Compile time checks to make sure the method exist and can be called from the class creating the object (Left hand side) Late or Dynamic Binding At Run-Time the decision is made which instance method to call: It looks at the actual type of object that was created. b1.getDescription(); c1.getDescription(); p1.getDescription(); Each object is of the type Basketball at compile time but the subclass at runtime.

Casting Casting does not change the object referenced; it creates an anonymous reference to that object BasketballPlayer b = new BasketballPlayer(); BasketballPlayer b2 = new CollegeBBPlayer(); BasketballPlayer b3 = new ProBBPlayer(); b is a BasketballPlayer at runtime b2 is still a CollegeBBPlayer at runtime b3 is still a ProBBPlayer at runtime

Polymorphism used for method arguments. This method is only in the BasetballPlayer class. public void showDescription(BasketballPlayer b) { System.out.println(b.getDescription()); } BasketballPlayer b1 = new BasketballPlayer("Basketball Player", 0); BasketballPlayer c1 = new CollegeBBPlayer("College player", 0, "Ball"); BasketballPlayer p1 = new ProBBPlayer("Pro Player", 0, 0); Question: What prints for each? Late or Dynamic binding finds the actual type at runtime and finds the getDescription method in that class.

Explicit Casting (upcasting) Cast a reference from the subclass upward to the parent a cast is required. BasekballPlayer b2 = new CollegeBBPlayer(); You would not be able to call any methods that were only in the CollegeBBPlayer class unless you cast the object. b2.getMajor(); // will not compile ((CollegeBBPlayer) b2).getMajor();

Rules for Polymorphic Method call BasketballPlayer c1 = new CollegeBBPlayer("College player", 0, "Ball"); c1.getMajor() // will not compile ((CollegeBBPlayer)c1).getMajor(); //explicit cast At compile time the method must be found in the class BasketballPlayer. If it is not you must do an explicit cast to its actual type. At runtime the actual type is determined and the method is selected from the subclass.

Class Cast Exception error BasketballPlayer b1 = new BasketballPlayer(); BasketballPlayer p1 = new ProBBPlayer(); b1 = (CollegeBBPlayer)p1; java.lang.ClassCastException: ProBBPlayer cannot be cast to CollegeBBPlayer