Beruflich Dokumente
Kultur Dokumente
Chapters 8
POLYMORPHISM Overview
Prof. M. Taleb
Department of Computer Science & Software Engineering Concordia University, Montreal, Canada
POLYMORPHISM
Polymorphim means that a variable of supertype can refer to subtype object.
A class defines a type. A type defined by a subclass is called a subtype and a type defined by its superclass is called a supertype. Example: Circle is a subtype of FormsClass and FormsClass is supertype for Circle Inheritance relationship enables a subclass to inherit features from its superclass with additional new features. A subclass is a specialization of its superclass. Every instance of a subclass is also an instance of its superclass but not vice versa. Example : every circle is a form object, but not every form object is a circle Therefore, we can always pass an instance of a subclass to a parameter of its superclass type. An object of a subclass can be used wherever its superclass object is used Polymorphism Example: public class Test_Polymorphism { //Display the circle and rectangle properties public static void main(String[] args) { displayObject(new Circle(1, red, false)); displayObject(new Rectangle(1, 1, black, true)); } //Display forms object properties public static void displayObject (FormsClass object){ //Polymorphic call
System.out.println(Created on + object.getDateCreated() + . Color is + object.getColor() ); }
Late Binding : is determined by objs actual type, not by declared type (If
Cn
java.lang.Object
Cn-1
..
C2
C1
Refer to the example already shown in class (Object Person Student GraduateStudent classes):
Invoking m(new GraduateStudent()) causes the toString() method defined in the Student class to be invoked at the runtime. In Java, Cn is the Object class (General class) and C1 is GraduateStudent class (Specific class) If obj invoke a method m(), the JVM searches the implementation for the method m() in C1, C2, , Cn-1, Cn, in this order, until it is found Once an implementation is found, the search stops, and the first found implementation is invoked.
No Late binding for private, final, and static methods Early Binding (If the method definition is associated with its invocation when the code is compiled)
UPCASTING / DOWNCASTING
see the details in additional slides of Inheritance
The Upcasting
Upcasting is when an object of a derived class is assigned to a variable of a base class (or any ancestor class) It is always possible to cast an instance of a subclass to a variable of a superclass, because an instance of a subclass is always an instance of its superclass. Example:
Person P1 = new Person(Michael, 1234 Maisonneuve West, Montreal, 5141234567); Student S1 = new Student(Clint, 4567 Saint-Catherine West, Montreal, 5149876543, 1234567);
P1 = S1;
The Downcasting
When casting an instance of a superclass to a variable of a subclass, explicit casting must be used to confirm your intention to the compiler with the (SubclassName) cast notation In many cases it doesn't make sense, or is illegal : S1 = (Student)P1 has to be done very carefully
UPCASTING / DOWNCASTING
see the details in additional slides of Inheritance
CLONE() METHOD
Java.lang.Object
Clone()
Copies the values of primitive type fields and the references of an object type fields Can be overridden in subclasses
toString()
Returns a descriptive string representation of the object Should be overridden in given class
equals
This method tests whether two objects are equal
getClass()
Returns an instance of java.lang.class This instance is created automatically by JVM for every class loaded into memory This instance known as a meta Object Contains the information about the class file: class name, constructors, methods Used in a test with == or != tests if two objects were created with the same class Used to check the class of an object
Return true if it is a descendent of that class as well else false Should be overridden
Example
House house1 = new House(1, 1750.50); House house2 = (House) house1.clone();
house1: House Id = 1 Area = 1750.50
Memory 1 1750.50
house2 = house1.clone()
house1: House Id = 1 Area = 1750.50
Memory 1 1750.50
ABSTRACT CLASS
Abstract classes are like regular classes. A class that contains abstract methods must be defined abstract. An abstract method is defined without implementation. Its implementation is provided by subclasses A subclass can be abstract even if its superclass is concrete. For example:
Object class is concrete, but its subclasses, such as FormsClass, may be abstract.
We cannot create instances of abstract classes using the new operator, but an abstract class can be used as a data type. For example:
//Creates an array whose elements are of the FormsClass type FormsClass[ ] fcObjects = new FormsClass[10]; //Creates an instance of FormsClass and assigns its reference to the array fcObjects[0] = new Circle();
The constructor in the abstract class is defined protected, because it is used only by subclasses. When we create an instance of a concrete subclass, its superclasss constructor is invoked to initialize data fields (attributes) defined in the superclass For example: The FormsClass abstract class defines the common features (data and methods) for form objects and provides appropriate constructors. Because we dont know how to compute areas and perimeters of forms objects, getArea() and getPerimeter() are defined as abstract methods.