1 Lecture 4 Further OO Concepts I - Polymorphism Overview  What is polymorphism?  Why polymorphism is wonderful?  Why is Upcasting useful?  What is.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
1. 2 Introduction to Inheritance  Access Modifiers  Methods in Subclasses  Method Overriding  Converting Class Types  Why up-cast?  Why down-cast?
Jan Object Oriented Programming Yangjun Chen Dept. Business Computing University of Winnipeg.
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.
1 More on Inheritance Overview l Object: The father of all classes l Casting and Classes l Object Cloning l Importance of Cloning.
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Review of Object-Oriented Concepts in JAVA Object-Oriented Concepts supported by JAVA. Advantages of Object-Orientation. Inheritance. Abstract Classes.
Polymorphism What is Polymorphism? Taking Advantage of Polymorphism
1 Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding l Object:
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
1 CS2200 Software Development Lecture 29: Polymorphism I A. O’Riordan, 2008 Based on notes by K. Brown.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1 Further OO Concepts (Part I) Further OO Concepts I - Polymorphism Overview l Polymorphism is Wonderful. l Usefulness of Up casting? l Method call binding?
Review of Object-Oriented Concepts in JAVA Object-Oriented Concepts supported by JAVA. Object-Oriented Concepts supported by JAVA. Advantages of Object-Orientation.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
1 Introduction to Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Programming With Java ICS Chapter 8 Polymorphism.
Inheritance in the Java programming language J. W. Rider.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Classes, Interfaces and Packages
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
Inheritance ndex.html ndex.htmland “Java.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Object-Oriented Programming: Polymorphism Chapter 10.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
1 Tirgul 8 Inheritance and Polymorphism. 2 Inheritance and Polymorphism An important part of Object Oriented Programming. Very often misused by beginners.
BY:- TOPS Technologies
Polymorphism in Methods
7: Polymorphism Upcasting revisited Forgetting the object type
Modern Programming Tools And Techniques-I
Polymorphism.
Advanced Programming in Java
Polymorphism.
ATS Application Programming: Java Programming
CSC 143 Inheritance.
CSC 113 Tutorial QUIZ I.
More inheritance, Abstract Classes and Interfaces
Polymorphism.
Advanced Programming in Java
Presentation transcript:

1 Lecture 4 Further OO Concepts I - Polymorphism Overview  What is polymorphism?  Why polymorphism is wonderful?  Why is Upcasting useful?  What is method call binding?  Programming examples on polymorphism.  Extensibility: Another byproduct of polymorphism.

2 Lecture 4 Polymorphism is Wonderful l Polymorphism means many forms (it derives from the Greek words “poly”, meaning many, and “morphos”, meaning form).  Polymorphism is a way of giving a method one name that is shared up and down an object hierarch, with each object in the hierarch implementing the method in a way appropriate to itself.  The classes must be part of the same inheritance hierarch  It is one of the beautiful features of object-oriented programming languages.  It allows improved code generalization and readability.  It also allows for the creation of extensible programs.

3 Lecture 4 Usefulness of Upcasting  We recall from inheritance that if Parent is a super class of a class Child(subclass), then it is legal to assign an object reference of type Child to object reference variable of type Parent without explicit casting.  That is, we can have: Parent p = new Child();  This is called upcasting – because we are going up the inheritance tree.  The ability to do upcasting is very important because it allows all types derived from the same super class to be treated as one. Example1: consider the following classes: class Person { private String name; public Person(String name) { this.name = name; } public String getName() { return name; } public void print() { System.out.println("Person:“ + name); } }

4 Lecture 4 Example 1 (Cont’d) class Instructor extends Person { private double salary; public Instructor(String name, double salary){ super(name); this.salary = salary; } public double getSalary() { return salary; } public void print() { System.out.println("Instructor: "+getName()+ ", "+salary); } class Student extends Person { private double gpa; public Student(String name, double gpa) { super(name); this.gpa = gpa; } public double getGPA() { return gpa; } public void print() { System.out.println("Student: "+getName()+ ", "+gpa); } Now because of inheritance, it is possible to write a single method that can accept an object of any of the above classes as parameter – see below: public static void describe(Person p) { p.print(); }

5 Lecture 4 Method call binding  Connecting a method call to a method body is called binding.  For example, consider the call to the print() method is the describe() method of the previous example.  An interesting question is which print() method are we actually calling? Are we calling that of the Person, Instructor or Student?  The answer to this question as far as Java Compiler is concerned is: “I don’t know”.  It is only possible to answer this question after knowing the object being passes as actual parameter to the describe method, i.e., the binding occurs at run-time.  Thus, Java exhibit what is called run-time binding, also called dynamic binding or late-binding.  All method binding is java uses late binding unless for methods that are declared as final.(using keyword final to prevent the method form being overridden in subclasses) or extending of the class.  In other languages, binding is determined by the compiler. For instance, Pascal. Such languages are said to exhibit early or static binding.

6 Lecture 4 class Person { private String name; public Person(String name) { this.name = name; } public String getName( ) { return name; } public void print() { System.out.println("Person:" + name); }} class Student extends Person { private double gpa; public Student(String name, double gpa) { super(name); this.gpa = gpa; } public double getGPA() { return gpa; } public void print() { System.out.println("Student: "+getName()+ ", "+gpa); }} class Instructor extends Person { private double salary; public Instructor(String name, double salary){ super(name); this.salary = salary; } public double getSalary() { return salary; } public void print() { System.out.println("Instructor: "+getName()+ ", "+salary ); }} class TestPolymorphism { public static void describe(Person p) {p.print(); } public static void main(String[] args) { Person p = new Person("Amr"); Instructor i = new Instructor("Usman", 9000); Student s = new Student("Ibrahim", 3.5); describe(p); // Invoking the method with describe(i); describe(s); }} ?

7 Lecture 4 What actually is polymorphism?  Since in Java, binding takes place at run-time, it is possible to write code that takes different forms. That is, it behaves differently depending on the parameters passed to it. This is exactly what polymorphism is all about.Note that polymorphism only applies to methods, it does not apply to data members  The following example and its output shows this idea. class TestPolymorphism { public static void describe(Person p) { p.print(); } public static void main(String[] args) { Person p = new Person("Amr"); Instructor i = new Instructor("Usman", 9000); Student s = new Student("Ibrahim", 3.5); describe(p); // Invoking the method with describe(i); describe(s); } }

8 Lecture 4 Polymorphism: An Example  Consider the following inheritance diagram:  Suppose we have the following: Shape s = new Circle(); s.draw();  The question is which draw() method will be called?  Because of polymorphism, the draw of the actual object (not necessarily that of the reference type) will be called. Thus, it is the draw() method of the Circle object that is called.

9 Lecture 4 Polymorphism: An Example (Cont’d)  The following example further demonstrates this idea: class Shape { void draw( ) {} void erase( ) {} } class Circle extends Shape { void draw( ) { System.out.println("Circle.draw( )"); } void erase( ) { System.out.println("Circle.erase( )"); } } class Square extends Shape { void draw( ) { System.out.println("Square.draw( )"); } void erase( ) { System.out.println("Square.erase( )"); } } class Triangle extends Shape { void draw( ) { System.out.println("Triangle.draw()"); } void erase( ) { System.out.println("Triangle.erase()"); } } public static Shape randShape( ) { Shape s = new Shape( ); switch((int)(Math.random( ) * 3)){ case 0: s = new Circle( ); break;} case 1:{ s = new Square( ); break;} case 2:{ s = new Triangle( ); break;} } return s ; } At run time

10 Lecture 4 Polymorphism: An Example (Cont’d) public static void main(String[] args) { Shape[] s = new Shape[9]; // Fill up the array with shapes: for(int i = 0; i < s.length; i++) s[i] = randShape(); // Make polymorphic method calls: for(int i = 0; i < s.length; i++) s[i].draw(); } public static Shape randShape( ) { Shape s = new Shape( ); switch((int)(Math.random( ) * 3)){ case 0: s = new Circle( ); break;} case 1:{ s = new Square( ); break;} case 2:{ s = new Triangle( ); break;} } return s ; }

11 Lecture 4 Extensibility  Suppose we define a subclass, say ResearchAssistant for the Class Student in Example 1 as follows: class ReseachAssistant extends Student { private int workLoad; public ReseachAssistant(String name, double gpa, int workLoad) { super(name, gpa); this.workLoad = workLoad; } public void print() { System.out.println("ResearchAssistant: "+getName()+ ", "+getGPA()+", "+workLoad); } }  It is now possible to call describe() method with an object of type ReseachAssistant without any change to the method. The describe() method is said to be extensible – meaning the functionality can be added to a code simply by inheriting a new data type from the base class.