Presentation is loading. Please wait.

Presentation is loading. Please wait.

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?

Similar presentations


Presentation on theme: "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?"— Presentation transcript:

1 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 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 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 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 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 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 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 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 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 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 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"); }


Download ppt "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?"

Similar presentations


Ads by Google