Exam Objective : Demonstrate the use of polymorphism PRESENTED BY: SRINIVAS VG.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

Inheritance Inheritance Reserved word protected Reserved word super
Slides 4/22 COP Topics Final Exam Review Final Exam The final exam is Friday, April 29 th at 10:00 AM in the usual room No notes, books, calculators,
Chapter 8 Improving Structure with Inheritance. The DoME Example The Database of Multimedia Entertainment We will be storing information about CDs and.
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.
Polymorphism and Inheritance. Overview  Understanding Inheritance  Designing an Inheritance Tree  What does inheritance really help with  Polymorphism.
Polymorphism & Methods COMP204 Bernhard Pfahringer (with lots of input from Mark Hall) poly + morphos (greek) “many forms”
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance Chapter 8.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Exam Objective : Legal return types PRESENTED BY : SRINIVAS VG.
Static and Dynamic Behavior Fall 2005 OOPD John Anthony.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
LECTURE 07 Programming using C# Inheritance
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Lecture 7 Polymorphism. Review: Constructors ●Parenthesis after constructor, not in class declaration ●The constructor makes the SamBot – do not “ new.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
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.
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
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
Inheritance, Abstract & Interface Pepper With help from and
Inheritence Put classes into a hierarchy derive a new class based on an existing class with modifications or extensions. Avoiding duplication and redundancy.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
CSC 142 Computer Science II Zhen Jiang West Chester University
ECE 122 March 24. Motivation I am tasked to write two classes, one for cat, one for sheep. That is easy. I roll up my sleeve and get it done!
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
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.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
CS61BL - Inheritance CS61BL Summer /30/2009.
Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
© 2006 Pearson EducationPolymorphism 1 of 22 POLYMORPHISM Polymorphism Partial Overriding OOP Review.
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.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Peyman Dodangeh Sharif University of Technology Fall 2014.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
OOP Basics Classes & Methods (c) IDMS/SQL News
(c) University of Washington05-1 CSC 143 Java Abstract Classes and Frameworks Reading: Ch. 11.
Inheritance & Polymorphism Android Club Agenda Inheritance Polymorphism.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
BY:- TOPS Technologies
Comp1004: Inheritance II Polymorphism. Coming up Inheritance Reminder Overriding methods – Overriding and substitution Dynamic Binding Polymorphism –
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
EKT472: Object Oriented Programming Overloading and Overriding Method.
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
Polymorphism in Methods
Overriding Method.
Continuing Chapter 11 Inheritance and Polymorphism
Chapter 9 Inheritance and Polymorphism
Inheritance Inheritance is a fundamental Object Oriented concept
Java Inheritance.
Interfaces and Abstract Classes
Chapter 14 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism Part 2
C++ Object Oriented 1.
Presentation transcript:

Exam Objective : Demonstrate the use of polymorphism PRESENTED BY: SRINIVAS VG

Agenda :  Reference Variable  Few examples  Polymorphic arguments  Roles of compiler and JVM in polymorphism

Lets see how polymorphism works???  Polymorphism means “many forms”  Access an object through reference variable  Key things about reference variable -Can be of only one type -Once declared cannot be changed -Its type determines the methods that can be invoked on the object -It can be declared as class type or an interface type -It can refer to any object of the same type or it can refer to any subtype of the declared type ( in other words the reference type can be a superclass of the actual object type )

Examples :  Dog myDog = new Dog(); Reference variable : myDog Class : Dog Cat myDog;---- >  In above reference variable and object are of same type  But in polymorphism they can be different eg. Animal mydog=new Dog();

// Declare an array of type animal that will hold objects of type animal Animal [] animals = new Animal[3]; animals [0] = new Dog(); // we can put ANY subclass of animal in the animals [1]= new Cat(); // animal array animals [2]= new Wolf(); for(i=0;I < animals.length; i++ ){ animals[i].eat(); // i is 0, a Dog is at index 0, so we get the Dog’s eat() method. i is 1 Cat’s eat() method animals[i].roam(); // same with roam() }

More examples : eat() makeNoise() sleep() roam() Animal Dog Cat sleep() Mammal Interface Superclass

 Dog passes the IS-A test for both Animal class and mammalinterface  So Dog can be treated polymorphically as one of the 4 things :- - An object - A Animal - A Dog - A mammal  Legal Declarations are :- Dog doberman = new Dog(); Object o=doberman; Animal shape=doberman; Mammal mover=doberman;

 Any idea how many objects are formed ?? - Only 1!!!  4 different types of reference variables, all referring to only one object  Which reference variable can invoke a eat() method ?? - Yup! Only Animal and Dog  Reason : eat() method is present in Animal class Dog class inherits from Animal

 Which methods can be invoked when Dog object is being referred to using a reference declared as type mammal ??? - Only the sleep() method

package sam3; interface Mammal { public void sleep(); } class Animal { public void eat() { System.out.println("ofcourse we both eat "); } // more code } class Dog extends Animal implements Mammal{ public void makeNoise() { System.out.println("hmmm...i usually dont bark.."); } public void sleep(){ System.out.println("I dont sleep at night..."); } class Cat extends Animal { public void roam() { System.out.println("I am very lazy....."); } // more code }

public class TestAnimals { /* public static void doShapes(Animal shape) { shape.eat(); } */ public static void main (String[] args) { Dog doberman = new Dog(); Object o= doberman; Animal shape= doberman; Mammal mover= doberman; Cat tile = new Cat(); Object j=tile; doberman.sleep(); doberman.eat(); doberman.makeNoise(); o.equals(j); shape.eat(); mover.sleep(); // doShapes(player); // doShapes(tile); } }

Polymorphic arguments : Eg. Say you declare a reference variable of supertype say Animal and assign a subcalss to it say Dog….see how it works class Vet { public void qiveShot(Animal a){ //do horrible things to the Animal at // the other end of the 'a' parameter a.makeNoise(); } class Petowner { public void start () { Vet v = new Vet(); Dog d=new Dog(); Hippo h=new Hippo(); v.giveshot(d); v.giveShot(h); } }

So what’s the big deal ?????  You can write code that doesn’t have to change when you introduce new subclass types into the program  So with the Vet class having arguments declared as type Animal, your code can handle any Animal subclass  If others take advantage of Vet class, their new Animal type need to extend class Animal  Vet methods still work, even though Vet class was written without knowledge of new Animal subtypes

Compiler vs JVM  Compiler only knows about the declared reference type  JVM knows what the object really is  If Dog object’s eat() method is called using Animal reference variable, if Dog class overrides the eat() method, the JVM will invoke the Dog’s version ( ** Not the Animal’s version ** )