Inheritance and Polymorphism

Slides:



Advertisements
Similar presentations
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Advertisements

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 15: Polymorphism,
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Chapter 10 Classes Continued
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
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.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
The Procedure Abstraction, Part VI: Inheritance in OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
CSCI 383 Object-Oriented Programming & Design Lecture 17 Martin van Bommel.
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Chapter 12 Support for Object oriented Programming.
Copyright © Curt Hill Inheritance and Polymorphism A Powerful Technique.
Copyright © Curt Hill Inheritance in C++ How to do it.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
CPS Inheritance and the Yahtzee program l In version of Yahtzee given previously, scorecard.h held information about every score-card entry, e.g.,
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Polymorphism Lecture - 9.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
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,
Object-Oriented Programming
Inheritance in C++ How to do it Copyright © Curt Hill
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Sections Inheritance and Abstract Classes
Polymorphism, Virtual Methods and Abstract Classes
Inheritance and Polymorphism
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
Object-Oriented Programming
Polymorphism.
Object Oriented Programming in Java
Week 4 Object-Oriented Programming (1): Inheritance
Types of Programming Languages
Object Oriented Programming
Designing for Inheritance
Inheritance Basics Programming with Inheritance
OOP and ADTs Chapter 14 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved.
Learning Objectives Inheritance Virtual Function.
Java Programming Language
Virtual Functions Department of CSE, BUET Chapter 10.
Advanced Java Topics Chapter 9
The Diamond Problem Its Solution Copyright © 2017 Curt Hill.
Polymorphism Polymorphism
Computer Programming with JAVA
Java – Inheritance.
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Java Inheritance.
Multiple Inheritance in C++
The Java Alternative to Multiple Inheritance
Workshop for Programming And Systems Management Teachers
Overview of C++ Polymorphism
VIRTUAL FUNCTIONS RITIKA SHARMA.
Lecture 10 Concepts of Programming Languages
COP 3330 Object-oriented Programming in C++
Chapter 11 Class Inheritance
C++ Object Oriented 1.
Lecture 6: Polymorphism
Chapter 7 Inheritance.
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.
Programming in C# CHAPTER 5 & 6
Computer Science II for Majors
Presentation transcript:

Inheritance and Polymorphism An Introduction Copyright © 2000-2012 Curt Hill

Biology Example Class is a borrowed word from biology One line from an inheritance tree Animals (a kingdom) Vertebrates (a phylum) Mammals (a class) Rodents (an order) Squirrels (a family) Thirteen line ground squirrel (a specie) Copyright © 2000-2012 Curt Hill

Relationship Characteristics are preserved as we go down the line of inheritance The prior list shows an is a relationship A thirteen line ground squirrel is a squirrel A squirrel is a rodent A thirteen line ground has all the characteristics of any of the above Copyright © 2000-2012 Curt Hill

Biology Example Again Mammals are warm blooded, have fur and feed young milk Rodents have 1 pair of upper incisor teeth and are mammals Squirrels have furry tails and are rodents Copyright © 2000-2012 Curt Hill

A specific instance A thirteen line ground squirrel is a squirrel Does a thirteen line ground squirrel have warm blood? 1 pair of upper incisors? furry tail? Yes: a thirteen line ground squirrel is a squirrel, rodent, mammal, vertebrate and animal Copyright © 2000-2012 Curt Hill

Inheritance Deriving new classes from old classes New class may retain any properties or methods of old New class may add new properties, new methods New class may replace/remove old methods Remember the platypus, which is an egg laying mammal Copyright © 2000-2012 Curt Hill

Copying and Deriving What is the difference? After the copy, the two classes are separate and no longer related After a derivation the derived class can be changed by changing the original Copyright © 2000-2012 Curt Hill

Inheritance example Person has a name and age Student is a person with GPA and major Employee is a person with a wage Grad is a student with degree The characteristics of a person are still contained in a grad Copyright © 2000-2012 Curt Hill

person set_name employee student set_wage set_major grad set_degree Copyright © 2000-2012 Curt Hill

Inherited properties and methods All classes have properties (eg. name and age) and methods (eg. set_name) Person explicitly declares it All the rest inherit it Grad has degree, as well as GPA and major Copyright © 2000-2012 Curt Hill

Terminology of inheritance Person is the base class or superclass Student and Employee are derived classes Copyright © 2000-2012 Curt Hill

Displaying the contents Each class needs a display method How would that work? Each class would call the display of its ancestor Then display the things that are unique to it Much easier that re-doing the work of the older classes If the ancestral class changes the display routine the descendants would take benefit without any change Copyright © 2000-2012 Curt Hill

Polymorphism Literally: many shapes Classes that are related are interchangable In C++ only the pointers are interchangable In Java any container, such as an array can have related contents Copyright © 2000-2012 Curt Hill

Effects of Polymorphism Do a display on any class derived from person Not know until run-time, which kind it is Determining the function to use is called binding Dynamic binding vs static binding Copyright © 2000-2012 Curt Hill

Binding Most programming languages use static (or early) binding At compile or link time determine the function needed Examples: Pascal, C, COBOL Some programming languages use dynamic (or late) binding Determine function needed at run time Examples: LISP and Java C++ uses both, depending on situation Copyright © 2000-2012 Curt Hill

Inherited functions Execute the set_name function against any variable of these types derived from person The same effect occurs Defined in the base type, person Variable is upcast, that is converted to base type This is not polymorphism Copyright © 2000-2012 Curt Hill

Polymorphism Execute the display function against any variable of these four types The different effects occur based on the actual type of the variable Calls display knowing that all person derived classes have a display function They inherit it or redefine it A function can accept a person type and receive either a person, student, employee or grad without problem Copyright © 2000-2012 Curt Hill

Virtual Functions If two classes in the same inheritance tree have the same function they may be virtual Such as display in the person hierarchy Calling a virtual function with any class in the tree and you get that classes function, without knowing until run-time which class you have This is the basis of polymorphism Copyright © 2000-2012 Curt Hill

Requirements of Polymorphism The class must be accessed via pointer Only way in Java The pointer must be of a base class of any possible polymorphic methods The method in question must be virtual It must have same signature Copyright © 2000-2012 Curt Hill

Rodents in North America Rodents are an order consisting of several families Squirrels Pocket gophers Pocket Mice and Kangeroo Rats Beaver Mice, Rats, Voles, Lemmings Old world Rats and Mice Jumping Mice Porcupines Copyright © 2000-2012 Curt Hill

Natural inheritance trees In nature the inheritance tree consists only of leaves No examples of rodents that do not belong to one of the rodent families No generic rodent that is not a squirrel, not a ... Generic rodent is group of characteristics not an animal Copyright © 2000-2012 Curt Hill

Object inheritance trees Programming language object hierarchies may have objects that are branches and leaves We may instantiate a person or a student An ancestral object may be more than a group of characteristics Objects may also be groups of characteristics Copyright © 2000-2012 Curt Hill

Abstract Base Type A type that is not instantiatable Only used to define the interface (the characteristics) for derived classes Consider a generic rodent Has the characteristics of rodent but not a member of any of the subordinate families Copyright © 2000-2012 Curt Hill

Abstract Base Class Example Consider a Shape class Want to be able to: Move a shape Draw a shape The Shape class then has descendents: Circle Square The Shape class may implement move but cannot implement draw The Shape class forces all of its descendents to implement draw Copyright © 2000-2012 Curt Hill

Pure Virtual Functions Only interface if specified Such as draw Every abstract base class has one or more pure virtual functions No class with pure virtual functions may be instantiated An instantiable function overrides the pure virtual with a callable function Copyright © 2000-2012 Curt Hill

Conclusion Two ideas: Reuse as much code as possible Maximize the flexibility Copyright © 2000-2012 Curt Hill