110 likes | 134 Views
Further OO Concepts I - Polymorphism. Overview Polymorphism is Wonderful. Usefulness of Up casting? Method call binding? What actually is Polymorphism Examples of Polymorphism Extensibility – another byproduct of polymorphism . Coming Next: Further OO Concepts (Part II).
E N D
Further OO Concepts I - Polymorphism Overview • Polymorphism is Wonderful. • Usefulness of Up casting? • Method call binding? • What actually is Polymorphism • Examples of Polymorphism • Extensibility – another byproduct of polymorphism. • Coming Next: Further OO Concepts (Part II). Further OO Concepts (Part I)
Polymorphism is Wonderful. • Polymorphism means many forms (it derives from the Greek words Poly, meaning many, and Morphos, meaning form). • 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 Further OO Concepts (Part I)
Usefulness of Up casting • We recall from inheritance that if Parentis 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(); • 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: Further OO Concepts (Part I)
Example 1 (Cont’d) 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(); } Further OO Concepts (Part I)
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 in java uses late binding unless for methods that are declared as final. • In other languages, binding is determined by the compiler. E.g. Pascal. Such languages are said to exhibit early or static binding. Further OO Concepts (Part I)
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. • The following example and its output shows this idea. Further OO Concepts (Part I)
Polymorphism 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. Further OO Concepts (Part I)
Polymorphism Example (Cont’d) • The following example further demonstrates this idea Further OO Concepts (Part I)
Polymorphism Example (Cont’d) Further OO Concepts (Part I)
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 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. 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"); } } Further OO Concepts (Part I)