Reputation: 343
I'm about to take the final exam in my very first object-oriented programming class and I still don't understand something about the concept of polymorphism.
Let's say I have an abstract class "Vehicle" and this class has one subclass named "Aircraft". My question is, what is the different between these two codes ?
Aircraft Jetplane = new Aircraft();
and
Vehicle Jetplane = new Aircraft();
Upvotes: 6
Views: 5784
Reputation: 1699
As Merlyn said, it takes a long chapter to explain polymorphism, but let me try explaining by simple example.
Let's say you are asked to move the jetplane and the boat and check if each of them moved. Then you could do this:
Aircraft jetPlane = new Aircraft();
jetPlane.moveForward();
boolean movementStatus = jetPlane.didItMoveForward();
Boat boat = new Boat();
boat.moveForward();
boolean movementStatus = boat.didItMoveForward();
Or this
boolean moveIt(Aircraft plane) {
plane.moveForward();
boolean movementStatus = jetPlane.didItMoveForward();
return movementStatus;
}
....
Aircraft jetPlane = new Aircraft();
boolean status = moveIt(jetPlane);
....
boolean moveIt(Boat boat) {
boat.moveForward();
boolean movementStatus = boat.didItMoveForward();
return movementStatus;
}
....
Boat boat = new Boat();
status = moveIt(boat);
Now, as you can see above, you have to define a specific method for each type of vehicle. This is going to cause code-duplication and the code is not reusable. Here's where the polymorphism comes into picture.
Let's say you were to have your method this way:
boolean moveIt(Vehicle vehicle) {
vehicle.moveForward();
boolean movementStatus = vehicle.didItMoveForward();
return movementStatus;
}
Aircraft jetPlane = new Aircraft();
boolean status = moveIt(jetPlane);
Boat boat = new Boat();
status = moveIt(boat);
As you can see above, due to the polymorphic nature of the vehicle instances (plane and boat), one method moveIt is sufficient for a good reuse of logic.
Upvotes: 4
Reputation: 79
According to ORACLE doc:
Using an Interface as a Type When you define a new interface, you are defining a new reference data type. You can use interface names anywhere you can use any other data type name. If you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface.
As an example, here is a method for finding the largest object in a pair of objects, for any objects that are instantiated from a class that implements Relatable:
public Object findLargest(Object object1, Object object2) {
Relatable obj1 = (Relatable)object1;
Relatable obj2 = (Relatable)object2;
if ((obj1).isLargerThan(obj2) > 0)
return object1;
else
return object2;
}
By casting object1 to a Relatable type, it can invoke the isLargerThan method.
If you make a point of implementing Relatable in a wide variety of classes, the objects instantiated from any of those classes can be compared with the findLargest() method—provided that both objects are of the same class. Similarly, they can all be compared with the following methods:
public Object findSmallest(Object object1, Object object2) {
Relatable obj1 = (Relatable)object1;
Relatable obj2 = (Relatable)object2;
if ((obj1).isLargerThan(obj2) < 0)
return object1;
else
return object2;
}
public boolean isEqual(Object object1, Object object2) {
Relatable obj1 = (Relatable)object1;
Relatable obj2 = (Relatable)object2;
if ( (obj1).isLargerThan(obj2) == 0)
return true;
else
return false;
}
These methods work for any "relatable" objects, no matter what their class inheritance is. When they implement Relatable, they can be of both their own class (or superclass) type and a Relatable type. This gives them some of the advantages of multiple inheritance, where they can have behavior from both a superclass and an interface.
Upvotes: 2
Reputation: 2505
First Case:
jetPlane is an object of Aircraft Class
. And can use all the attributes and methods of Aircraft Class.
Second Case:
jetPlane is not an object
. Its a reference to object of Aircraft Class
. Even though it has been created using the constructor of Aircraft class, it can only see and use the methods and attributes of the Vehicle Class and not the Aircraft class
.
Upvotes: 0
Reputation: 35
the second one take advantage of polymorphism... although polymorphisme sometimes is more advantageous with array or list... just imagine u have multiple classse that inherits from vehicle lets say Plane, Car and bicycle, and u have an array with objects of these classe. May be at one point in your program u might like that all the vehicle to slow down... if previously u have declared an array of vehicle like : Vehicle array = new Vehicle[10]; the u could simply do for Vehicle v in array : v.slowDown() and the method slowDown from the class Car, will be called if in the array there is an object car, the slowDown from Plane will also be called , if it is a Plane, and so on...
Upvotes: 0
Reputation: 4891
A second thing also happens. The type of the variable determines which methods are visible. You can declare variables of interface type or of any (including abstract class) type. You may only create objects of concrete (non-abstract) class type.
If your variable is of interface type, all methods declared in the interface are visible. If it is of class type, the same is also true.
The object, however is responsible for doing the actual work in the method. In this way, the method call is "delegated" to the object.
If your variable is of a type that is too general, the methods you want will not be visible. If it is too specific, you won't be able to point at all of the variables you might need to. That's the tradeoff.
Upvotes: 3
Reputation: 308753
The first one is not polymorphic: the compile-time and run-time types of jetplane are both Aircraft.
The second one is polymorphic. The compile-time type of jetplane is Vehicle, but the runtime type is Aircraft.
Upvotes: 5
Reputation: 146910
In the second, then Jetplane could be anything else that inherits from Vehicle, not just an Aircraft. For example, you could have something like
Vehicle veh = null;
if (someCondition)
veh = new Aircraft();
else
veh = new Boat();
That can't be done in the first sample, because a Boat is not an Aircraft.
Upvotes: 14