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?

Slides:



Advertisements
Similar presentations
A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
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.
Jan Object Oriented Programming Yangjun Chen Dept. Business Computing University of Winnipeg.
1 More on Inheritance Overview l Object: The father of all classes l Casting and Classes l Object Cloning l Importance of Cloning.
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
Inheritance Chapter 9.
1 Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding l Object:
1 CS2200 Software Development Lecture 29: Polymorphism I A. O’Riordan, 2008 Based on notes by K. Brown.
1 Lecture 4 Further OO Concepts I - Polymorphism Overview  What is polymorphism?  Why polymorphism is wonderful?  Why is Upcasting useful?  What is.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
CPSC150 Abstract Classes and Interfaces Chapter 10.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
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.
1. Everything is an object 2. A program is a set of objects that interact by sending messages (i.e. function calls) 3. Each object has its own state 4.
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.
 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)
Programming With Java ICS Chapter 8 Polymorphism.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
CSCI 383 Object-Oriented Programming & Design Lecture 17 Martin van Bommel.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
Chapter 10 Inheritance and Polymorphism
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.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Peyman Dodangeh Sharif University of Technology Fall 2014.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
Object-Oriented Programming: Polymorphism Chapter 10.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
Abstract Classes. Recall We have the ability to create hierarchies of classes. Good design practice suggests that we move common methods as high as possible.
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.
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
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 113 Tutorial QUIZ I.
More inheritance, Abstract Classes and Interfaces
Review of Object-Oriented Concepts in JAVA
CS18000: Problem Solving and Object-Oriented Programming
Review of Object-Oriented Concepts in JAVA
Polymorphism.
Java Programming: From the Ground Up
Advanced Programming in Java
Presentation transcript:

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? l What actually is Polymorphism l Examples of Polymorphism l Extensibility – another byproduct of polymorphism. l Coming Next: Further OO Concepts (Part II).

2 Further OO Concepts (Part I) Polymorphism is Wonderful. l Polymorphism means many forms (it derives from the Greek words Poly, meaning many, and Morphos, meaning form). l It is one of the beautiful features of object-oriented programming languages. l It allows improved code generalization and readability. l It also allows for the creation of extensible programs

3 Further OO Concepts (Part I) Usefulness of Up casting We recall from inheritance that if Parent is a super class of a class Child, 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(); l This is called upcasting – because we are going up the inheritance tree. l 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 Further OO Concepts (Part I) 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 Further OO Concepts (Part I) Method call binding: l 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 ? l The answer to this question as far as Java Compiler is concerned is “I don’t know”. l 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. l Thus, Java exhibit what is called run-time binding, also called dynamic binding or late-binding. l All method binding in java uses late binding unless for methods that are declared as final. l In other languages, binding is determined by the compiler. E.g. Pascal. Such languages are said to exhibit early or static binding.

6 Further OO Concepts (Part I) What actually is polymorphism? l 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. l 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); describe(i); describe(s); }

7 Further OO Concepts (Part I) Polymorphism Example l Consider the following inheritance diagram? l Suppose we have the following: Shape s = new Circle(); s.draw(); l The question is which draw() method will be called? l 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.

8 Further OO Concepts (Part I) Polymorphism Example (Cont’d) l 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 class Shapes { public static Shape randShape() { switch((int)(Math.random() * 3)) { case 0: return new Circle(); case 1: return new Square(); case 2: return new Triangle(); }

9 Further OO Concepts (Part I) Polymorphism 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(); }

10 Further OO Concepts (Part I) Extensibility l 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); } l It is now possible to call the describe() 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.

11 Further OO Concepts (Part I) Constructors class Shape { public Shape() { System.out.println("Shape Constructor"); } class FourSided extends Shape { public FourSided() { System.out.println("FourSided Constructor"); } class Rectangle extends FourSided { public Rectangle() { System.out.println("Rectangle Constructor"); } class Square extends Rectangle { public Square() { System.out.println("Square Constructor"); }