Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Lecture 4 Further OO Concepts I - Polymorphism Overview  What is polymorphism?  Why polymorphism is wonderful?  Why is Upcasting useful?  What is."— Presentation transcript:

1 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 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 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 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 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 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 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 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 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 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 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.


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

Similar presentations


Ads by Google