Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Advertisements

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 Inheritance Reserved word protected Reserved word super
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics (inheritance review + Java generics)
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
TCP1201 OOPDS Lecture 4 1. Learning Objectives  To understand upcasting & downcasting  To understand static polymorphism and dynamic polymorphism 
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Inheritance in the Java programming language J. W. Rider.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
1 COMP313A Programming Languages Object Oriented Progamming Languages (3)
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Object Oriented Software Development
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Object-Oriented Programming Chapter Chapter
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
ISBN Object-Oriented Programming Chapter Chapter
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
SOLID Design Principles
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
1 Lecture 23: Dynamic Binding (Section ) CSCI 431 Programming Languages Fall 2002 A compilation of material developed by Felix Hernandez-Campos.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
ISBN Chapter 12 Support for Object-Oriented Programming.
Chapter 12: Support for Object- Oriented Programming Lecture # 18.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism in Methods
Inheritance and Polymorphism
Interfaces and Inheritance
Object-Oriented Programming: Polymorphism
Continuing Chapter 11 Inheritance and Polymorphism
Object Oriented Programming
Advanced Java Topics Chapter 9
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Polymorphism CT1513.
Polymorphism Polymorphism - Greek for “many forms”
Chapter 9 Carrano Chapter 10 Small Java
Chapter 8: Class Relationships
Advanced Inheritance Concepts
Lecture 10 Concepts of Programming Languages
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
Liskov Substitution Principle (LSP)
Presentation transcript:

Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not the other way around Examples: reference/pointer can hold an object of a subclass subclass can be passed to an function requiring superclass reference/pointer

Inheritance: is-a Relationships Inheritance should only be used when an is-a relationship exists between the base and the derived class e.g. A Ball is a Sphere, so a Ball class could be derived from a Sphere class, however A Color is not a Ball (!!) so a Color class should not be derived from a Ball class (or vice versa) In programming terms a derived class should be type-compatible with all of its ancestor classes That is, an instance of a derived class can be used instead of an instance of the base class

When to use inheritance? An important principle in software design for writing code that is easy to maintain and re-use is the Open-Closed principle: Software entities like classes, modules and functions should be open for extension but closed for modifications In terms of classes, this means that creating a subclass should not necessitate changing the superclass Any subclass should comply with the (Liskov) Substitution Principle otherwise it may violate the Open-Closed Principle Reference:

(Liskov) Substitution Principle “The supertype’s behavior must be supported by the subtypes: subtype objects can be substituted for supertype objects without affecting the behavior of the using code.” i.e., functions that use pointers/references to base class objects must be able to use objects of derived classes without knowing it. Example: Rectangle and Square classes

When to use inheritance? The key is that an is-a relationship should apply to the behavior of objects, not to the real-world entities that the objects are modeling A square is a rectangle but The behavior of a Square object is not consistent with the behavior of a Rectangle object! The test, therefore, is that a subclass’ public behavior should conform to the expected behavior of its base class This is important because a client program may depend (or expect) that this conformity exists

Polymorphism Polymorphism means many forms A Subclass definition can include a redefinition of a superclass method The subclass method then overrides the superclass method If the calling object is a subclass object, the subclass’ version of the method is used This is useful if the subclass is required to perform the same action but in a different way Note that overriding is not the same as overloading An overloaded method is one where there is another method with the same name, but a different parameter list

Dynamic Binding The correct version of an overridden method is decided at execution time, not at compilation time, based on the type to which a pointer refers This is known as dynamic binding or late binding This allows a superclass reference to be used to refer to a subclass object while still eliciting the appropriate behavior which method is chosen is based on the object type rather than the pointer/reference type this allows the old code (code of the superclass) to call new code (code of the subclass)!

Java and Dynamic Binding In Java, all object variables are references to objects and all methods have dynamic binding A base class reference variable that refers to a subclass object will use the subclass methods But does not have access to subclass methods that do not exist in the base class Containers that store a base class can be used to store subclass objects When retrieving objects from such a container the subclass specific methods can be accessed by first casting the object to a subclass reference Note that all Java classes are subclasses of Object

C++ and Dynamic Binding Unlike Java, C++ object variables are not references/pointers unless explicitly declared so, therefore a C++ object variable uses static memory Dynamic binding can happen only for pointers and references to objects (which method to call will be decided base on the type of object not on type of pointer or reference). However, dynamic binding is not automatically used as in Java! Unless the method is declared virtual the static binding is used (i.e., depends on type of pointer or reference)