Inheritance and Polymorphism CS351 – Programming Paradigms.

Slides:



Advertisements
Similar presentations
OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance.
Advertisements

INHERITANCE BASICS Reusability is achieved by INHERITANCE
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
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. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 24: Dynamic Binding COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
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.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
CS 403 – Programming Languages Class 25 November 28, 2000.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Multiple Inheritance Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
Inheritance Extending Class Functionality. Polymorphism Review Earlier in the course, we examined the topic of polymorphism. Many times in coding, we.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Inheritance in the Java programming language J. W. Rider.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 12 Support for Object oriented Programming.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Chapter 10 Inheritance and Polymorphism
Programming in Java CSCI-2220 Object Oriented Programming.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object-Oriented Programming Chapter Chapter
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Virtual FunctionstMyn1 Virtual Functions A virtual function is declared in a base class by using the keyword virtual. A function that you declare as virtual.
COMP Inheritance Basics Yi Hong June 09, 2015.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Inheritance Chapter 7. Outline Inheritance Basics Programming with Inheritance Dynamic Binding and Polymorphism.
CSCI-383 Object-Oriented Programming & Design Lecture 24.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
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)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
1 Lecture 23: Dynamic Binding (Section ) CSCI 431 Programming Languages Fall 2002 A compilation of material developed by Felix Hernandez-Campos.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Chapter 12: Support for Object- Oriented Programming Lecture # 18.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Design issues for Object-Oriented Languages
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
7. Inheritance and Polymorphism
Polymorphism, Abstract Classes & Interfaces
Inheritance and Polymorphism
Polymorphism.
Types of Programming Languages
Object Oriented Analysis and Design
Java Programming Language
Polymorphism, Abstract Classes & Interfaces
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Polymorphism Polymorphism
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Inheritance and Polymorphism
Lecture 10 Concepts of Programming Languages
C++ Object Oriented 1.
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Presentation transcript:

Inheritance and Polymorphism CS351 – Programming Paradigms

Dynamic Method Binding Inheritance in OOP opens up many interesting properties. One of the main advantages of inheritance is that some derived class D has all of the members of its base class B. Once D is not hiding any of the public members of B, then it is possible for an object of D to represent B in any context where a B could be used. This feature is known as subtype polymorphism. Lets illustrate an example:

An Example of Dynamic Binding class person { … void person :: print() { // prints details for the person } }; class student : public person { … void student :: print () { //adds more specific information } }; class lecturer : public person { … void lecturer :: print () { //adds more details } }; … student *s = new student (); lecturer *t = new lecturer (); s -> print (); t -> print (); This is a polymorphic call person *x = s; person *y = t; x -> print () ; y -> print () ;

Example cont… person *x = s; person *y = t; x -> print () ; y -> print () ; Does the choice of the method to be called depend on the types of x and y ? If so then this is known as static binding. Does the choice of the method to be called depend on the classes of the objects s and t to which those variables refer ? If so then is known as dynamic binding. Dynamic binding is central to OOP and ensures that even if we are using the base class to refer to a child class, the correct methods will always be called.

Virtual Methods C++ uses static binding by default. Hence we must explicitly state that we wish to use dynamic binding. To do this we can use the virtual keyword. By changing the code in the previous example to look like this: class person { … virtual void print () { // print person details } }; With virtual methods, calls are dispatched to the appropriate implementation at runtime, based upon the class of the object.

Abstract Classes If a method is to be over-ridden by each child class, we can enforce this policy through the use of abstract classes. An abstract class allows method headers to be declared without any implementation. The keyword abstract is used in Java to indicate this but there is no such keyword in C++. We must use what is known as a pure virtual specifier in C++. To change the print method from our previous example to an abstract method, we use the following syntax: class person { … virtual void print () = 0; };

Abstract Classes cont… If a class contains at least one abstract method, then the entire class is abstract. This means that it is impossible to create instances of that class. The sub-classes must provide an implementation of the abstract class and the abstract method provides a ``clue’’ for dynamic method binding. Classes that contain abstract methods are known as interfaces. An appropriate scheme for dynamic method binding must be used.

Multiple Inheritance Sometimes it is desirable for a derived class to inherit features from more than one base class. Multiple inheritance appears in C++ and Python amongst other languages. The syntax for multiple inheritance in C++ looks like: class student : public person, public college { … } In this case the student derives from two parent classes, person and college. Multiple inheritance introduces a number of complicated issues.

Multiple Inheritance Issues 1. If two parent classes provide a method with the same name, which one does the child use? 2. If two parent classes are both derived from some common grandparent class, does the grandchild have one copy or two of the grandparents fields? There are a number of modes of operation for multiple inheritance: 1. Multiple inheritance with a common grandparent is known as repeated inheritance. 2. Repeated inheritance with separate copies of the grandparent is known as replicated inheritance while repeated inheritance with a single copy of the grandparent is known as shared inheritance. 3. The default in C++ is replicated inheritance.

Multiple Inheritance in C++ #include using namespace std; class Student { private: string id; public: string get_id(); }; class Employee { private: string id; public: string get_id(); };

Multiple Inheritance in C++ class TeachingAssistant : public Student, public Employee { … }; Teaching Assistant maintains the attributes and the methods associated with both base classes Student, and Employee StudentEmployee TeachingAssistant

Multiple Inheritance in C++ Polymorphic Assignment TeachingAssistant * M = new TeachingAssistant; Employee * E = M; //Legal as a Teaching Assistant is-an Employee Student * S = M; //Legal as a Teaching Assistant is-a Student

Problems with Multiple Inheritance Semantic ambiguities due to identical names in the parent classes. Similar names can be used for different operations in the base classes. E.g. an employee might have an id_number and a student might also have an id_number field. Both base classes might have a similar get_id() method. It is impossible to determine which version to use in the TeachingAssistant class: the get_id() in the Employee class or the get_id () in the Student class. E.g. TeachingAssistant * M; M -> get_id(); /*Which classes method will this refer to dynamically? */

Problems with Multiple Inheritance Solution 1: Use a fully qualified function name: TeachingAssistant * M = new TeachingAssistant; M -> Employee::get_id();

Problems with Multiple Inheritance Solution 2: Redefine the ambiguous function in the new class and hide the qualified name in a method body: class TeachingAssistant : public Student, public Employee { public: string get_id(); string student_id(); }; string TeachingAssistant :: get_id() { return Employee :: get_id(); } string TeachingAssistant :: student_id() { return Student :: get_id(); }

Replicated Inheritance Recall that replicated inheritance does not share the same common grandparent even if they both derive from the same grandparent! If we have code like this in C++: class A { … }; class B : public A { … }; class C : public A { … };/* Derives from A but it is not shared! */ class D : public B, public C { … }; … This gives us the following situation: A BC A D A* a; B* b; C* c; D* d; a = d; //error ambiguous b = d; // ok c = d; // ok a = b; // ok a = c; // ok

Shared Inheritance Shared inheritance allows for the sharing of any common parent classes. In C++ the default is replicated but shared inheritance is possible. Consider: class A {... }; class B : public virtual A { … }; class C : public virtual A { … }; class D : public B, public C { … }; This creates the following scenario: A D CB