Polymorphism and Inheritance. Overview  Understanding Inheritance  Designing an Inheritance Tree  What does inheritance really help with  Polymorphism.

Slides:



Advertisements
Similar presentations
Overriding CMPS Overriding Recall, a method in a child class overrides a method in the parent class, if it has the same name and type signature.
Advertisements

OO Programming in Java Objectives for today: Constructors Method Overriding & Overloading Encapsulation.
Chapter 1 Inheritance University Of Ha’il.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 15: Polymorphism,
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
C HAPTER 7 Better Living in Objectville. O VERVIEW Understanding Inheritance Designing an Inheritance Tree Avoiding Duplicate Code Overriding Methods.
Exam Objective : Demonstrate the use of polymorphism PRESENTED BY: SRINIVAS VG.
Polymorphism & Methods COMP204 Bernhard Pfahringer (with lots of input from Mark Hall) poly + morphos (greek) “many forms”
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Object Orientated Concepts. Overview  The world is objects  Defining an object  Not a function, it’s a method  Lets inherit want we can  Objects.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
ECE122 L22: Polymorphism Using Inheritance April 26, 2007 ECE 122 Engineering Problem Solving with Java Lecture 22 Polymorphism using Inheritance.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
LECTURE 07 Programming using C# Inheritance
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.
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Polymorphism & Interfaces
CSE 501N Fall ‘09 15: Polymorphism October 22, 2009 Nick Leidenfrost.
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
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.
What is inheritance? It is the ability to create a new class from an existing class.
These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Object-Oriented Design Justin Callanan CS 597. Object-Oriented Basics ● Data is stored and processed in “objects” ● Objects represent generalized real.
Review Class Inheritance, Abstract, Interfaces, Polymorphism, GUI (MVC)
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Chapter 12 Object-oriented design for more than one class.
Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”
© 2004 Pearson Addison-Wesley. All rights reserved April 14, 2006 Polymorphism ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor:
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
CS1101 Group1 Discussion 6 Lek Hsiang Hui comp.nus.edu.sg
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
03 | Object-Oriented Programming Gerry O’Brien | Technical Content Development Manager Paul Pardi | Senior Content Publishing Manager.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Coming up Which methods are where? – Overriding Calling super’s methods Coupling and cohesion.
Comp1004: Inheritance II Polymorphism. Coming up Inheritance Reminder Overriding methods – Overriding and substitution Dynamic Binding Polymorphism –
Chapter 12: Support for Object- Oriented Programming Lecture # 18.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Coming up A quick word about abstract How a class interfaces
Modern Programming Tools And Techniques-I
Polymorphism November 27, 2006 ComS 207: Programming I (in Java)
Python First Edition STARTING OUT WITH Chapter 10 Inheritance
Inheritance and Polymorphism
Inheritance.
Inherited Classes in Java
Advanced Programming Behnam Hatami Fall 2017.
Introduction Types Syntax Visibility Modes Example Programs
Inheritance, Superclasses, Subclasses
Abstract Classes Page
More About Inheritance & Interfaces
Law firm employee analogy
Interfaces and Abstract Classes
C++ Programming CLASS This pointer Static Class Friend Class
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.
Programming in C# CHAPTER 5 & 6
Computer Science II for Majors
Presentation transcript:

Polymorphism and Inheritance

Overview  Understanding Inheritance  Designing an Inheritance Tree  What does inheritance really help with  Polymorphism

Understanding Inheritance  Sometimes a class is to general and it is useful to have a more specific class for a purpose.  This class can inherit functions and values from a parent  Functions can be replaced with specialize versions

Understanding Inheritance  Parent  Animal  Children  Fish  Dog

Designing an Inheritance Tree  If you have multiple objects that need to inherit from other objects you need and inheritance tree  Inheritance trees show which objects are sub classes to other objects  They show what methods are overridden at each level

Overriding Methods  When you have a class tree, the lowest method or most specific method is called first. wolf.roam calls from class Canine wolf.eat calls from class Wolf, not class Animal llama.eat calls from class Animal, not class Wolf

What does inheritance really help with  In summary the concept of inheritance:  Allows for reducing duplicate code  Allows for the definition of a common protocol for a group of objects protocol (def.): The established code of procedure or behavior in any group, organization, or situation.  Polymorphism

Polymorphism  Polymorphism lets you link a super class object reference to a subclass object.

More Polymorphism Animal animals[4]; animals[0] = new Dog(); animals[1] = new Cat(); animals[2] = new Lion();..... for(int i = 0; i<4; i++){ animals[i].eat(); animals[i].roam(); }

More Polymorphism class Vet { public void giveShot(Animal a){ // Give animal a shot a.makeNoise(); }

Example  polymorph.cpp  We will create several animal classes that inherit from the base 'Animal' class  We will look at how an 'Animal' pointer can point to any type of animal and still make it work

Things to remember  Inheritance is powerful but takes thought  Using overriding makes using multiple object easier  Using overloading gives more versatility to your methods