OO Concepts

·

·PolymorphismHomepage  « Learn Java6 « Polymorphism
In this lesson we continue our investigation of OO concepts by learning about polymorphism, which roughly translated from the Greek means ‘the ability to assume different forms’. So what does this mean for us Java programmers? To explain this we will refresh our memory about how we create an object:

So lets go through the three step process above:
1.Declaration – Here. we declare a reference type named garbage of type Garbage and the JVM allocates space for it.
2.Creation – Tells the JVM to allocate space on The Heap for a new Garbage object.
3.Assignment – Assign the new Garbage object to the reference type garbage.

The reference type and the object type are the same.
Polymorphic Method Invocation Top
With polymorphism when you declare the reference type in the declaration, it can be from a superclass of the object we are creating. Or looking from a different perspective, when creating an object, we can use any supertype from a class that we have extended. The compiler has no problem with this but the power of polymorphism happens at runtime through the concept of virtual method invocation. This concept is the dynamic selection of overridden methods at runtime based on the actual object type, rather than the reference type. This only applies to instance methods, everything else uses the reference type at runtime.

So what does this give us I hear you ask? Well we can use the supertype for reference type declaration when instantiating our objects, safe in the knowledge that the JVM will use the actual object created to invoke the correct overridden methods of the subtypes at runtime. This ties in with the concept of abstraction that we looked at in the last lesson. By making generic methods abstract in the superclass we can be assured that the overridden method implementations are in place in the subclass at runtime. If you remember we have to implement all abstract methods from the superclass in the first concrete subclass. We will use an array to illustrate polymorphic method invocation:

/*
Polymorphic Array
*/
public class PolymorphicArray {

public static void main (String[] args) {
Truck[] truck = new Truck[2]; // Array declaration
truck[0] = new HGV();
truck[1] = new Garbage();
for (int i=0; i < truck.length; i++) { truck[i].load("stuff"); } } }Save, compile and run the file in directory   c:_OOConcepts in the usual way. The above screenshot shows the output of running our PolymorphicArray class. The JVM uses virtual method invocation to dynamically invoke the correct subtype override. The overrides in the HGV and Garbage classes are shown below:public class HGV extends Truck { public void load(String payload) { System.out.println("We are transporting " + payload + " in our Heavy Goods Vehicle." ); } } public class Garbage extends Truck { public void load(String payload) { System.out.println("Our garbage truck eats " + payload); } }We can happily include new subtypes in the knowledge that any method overrides will be called correctly on that subtype. Polymorphic Arguments Top We can also make the arguments to our methods polymorphic. We will write a new class and a test to see this in action:/* A Transit Class */ public class Transit { private int capacity = 4;public void hold(Vehicle v) { v.carry(capacity); } }Save and compile the file in directory   c:_OOConcepts in the usual way. Here's our test class ./* Test class for polymorphic arguments */ public class TestPolyArgs {public static void main (String[] args) { Vehicle bus = new Bus(); Vehicle hgv = new HGV(); Transit transit = new Transit(); transit.hold(bus); transit.hold(hgv); } }Save, compile and run the file in directory   c:_OOConcepts in the usual way. The above screenshot shows the output of running our TestPolyArgs test class. As you can see we are passing the Vehicle supertype as an argumment to the hold() method. The JVM uses virtual method invocation to dynamically invoke the correct Carry() method based on the actual object type at runtime. Polymorphic Return Types Top Lastly we will look at using polymorphic return types. We will write a new test class to see this in action:/* Test class for polymorphic return */ public class PolymorphicReturn {public static void main (String[] args) { // Array declaration below is creating an Array object so can use Abstract supertype Vehicle[] vehicle = new Vehicle[4]; for (int i=0; i < 4; i++) { vehicle[i] = createVehicle(i); vehicle[i].carry(i); } } /* Create a Vehicle subtype and return the Vehicle supertype */ static Vehicle createVehicle (int i) { Vehicle vehicle; switch (i) { case 0: vehicle = new Car(); break; case 1: vehicle = new HGV(); break; case 2: vehicle = new Bus(); break; default: vehicle = new Garbage(); } return vehicle; } }Save, compile and run the file in directory   c:_OOConcepts in the usual way. The above screenshot shows the output of running our slightly contrived PolymorphicReturn test class. We are returning the Vehicle supertype from the createVehicle() method. The JVM uses virtual method invocation to dynamically invoke the correct Carry() method based on the actual object type on return. We will return to polymorphism when we look at polymorphic interface arguments and polymorphic interface return types in the Deeper Into Polymorphism section of the next lesson. Static Overrides? Top I know what you're thinking, what on earth is something about statics doing in a section on polymorphism. Well, now we have talked about how polymorphism works I just want to hammer home the point that it only relates to overridden instance methods and nothing else. I also wanted to talk about method hiding which relates to static methods in a subclass with the same identifier in the superclass. To look at this we first had to get our heads around the idea of virtual method invocation and how this works with supertypes and subtypes. Now that is done we can use the same principle to look at method hiding:/* Superclass to illustrate method hiding */ public class A {public static void aClassMethod() { System.out.println("The class method in A."); } public void anInstanceMethod() { System.out.println("The instance method in A."); } } /* Subclass to illustrate method hiding */ public class B extends A {public static void aClassMethod() { System.out.println("The class method in B."); } public void anInstanceMethod() { System.out.println("The instance method in B."); } } /* Test Class to illustrate method hiding */ public class TestHiding {public static void main(String[] args) { B b = new B(); A a = b; A.aClassMethod(); a.anInstanceMethod(); } }In the above code the aClassMethod() static method in class B hides the aClassMethod() static method in class A. The anInstanceMethod() instance method in class B overrides the anInstanceMethod() instance method in class A. The above screenshot shows the output of running our TestHiding test class after compiling classes A and B. As you can see the aClassMethod() static method from the superclass gets invoked via the class name. The overridden anInstanceMethod() instance method from the subclass gets invoked via the object type. Polymorphism Quiz Top Try the quiz below to test your knowledge of this lesson Question 1 : What is required to use polymorphism? ·encapsulation ·inheritance ·abstraction - We need to have an inheritance hierarchy to use polymorphism, although abstracting out generic methods to force implementation in subclasses helps to enforce it. Next >>
Question 2 : What do we use in the declaration of an object to enable polymorphism?
·supertype
·subtype
– We use the supertype of the object we are creating
<< Prev Next >>
Question 3 : What are used for polymorphism?
·static variables
·instance variables
·static methods
·instance methods
– overridden instance methods are used in polymorphism.
<< Prev Next >>
Question 4 : We use overloaded methods in polymorphism?
·true
·false
– We use overridden methods in polymorphism.
<< Prev Next >>
Question 5 : What is the following code snippet an example of?

public class A {
public static void aClassMethod() {
}
}
public class B extends A {
public static void aClassMethod() {
}
}
·overloading
·overriding
·hiding
– This is an example of hiding.
<< Prev Next >>
Question 6 : Assuming the following are in different files is the following code snippet valid?

public class A { public void a() {} }
public class B extends A { public void a() {} }
public class TestAB {
public static void main(String[] args) {
B b = new A();
b.a();
}
}
·no
·yes
– Class TestAB doesn’t compile because of incompatible types. We can’t use the subclass as the refernce type to a superclass.
<< Prev Next >>
Question 7 : What is the dynamic selection of overridden methods at runtime based on the actual object type, rather than the reference type known as?
·virtual member invocation
·virtual method invocation
·virtual instance invocation
·virtual object invocation
– This concept is known as virtual method invocation.
<< Prev Finish! Status Bar Please select an answerWhat's Next? In the next lesson we examine interfaces and the contracts we provide when using them. <<  Abstraction                    Interfaces  >>
 Java6 Tutor Homepage  Top
All the Java6 OO Concepts lessons are listed below. Click a link to go to that lesson.

OO Concepts
Encapsulation
Inheritance Basics
Inheritance Concepts
Abstraction
Polymorphism
Polymorphic Method Invocation
Polymorphic Arguments
Polymorphic Return Types
Static Overrides?
Polymorphism Quiz
Interfaces
Nested Classes
The Object Superclass
 

 

Read More Post

STAY CONNECTED

Most Popular

Why Serviced Apartments Are the Perfect Choice for Long-Term Stays in Singapore – Everything You Need to Know
17 Dec

Why Serviced Apartments Are the Perfect Choice for Long-Term Stays in Singapore – Everything You Need to Know

If you’re heading to Singapore for a few weeks or many months, maybe for work or study or just to

Manifesting Your Dream Career: Finding Your Passion And Purpose
01 Aug

Manifesting Your Dream Career: Finding Your Passion And Purpose

Are you passionate about finding your dream career? Manifesting can help you align your career with your passions and purpose.

Revolutionizing Your Ride: Expert Auto Service At Carolina Auto Service
27 Feb

Revolutionizing Your Ride: Expert Auto Service At Carolina Auto Service

Finding a reliable auto service provider is paramount in the bustling world of vehicle maintenance and repair. That's where Carolina

Floratam: The Robust Grass Solution From Council Growers SOD
25 Jan

Floratam: The Robust Grass Solution From Council Growers SOD

Floratam grass, a popular St. Augustine variety, has become a go-to choice for homeowners and landscapers seeking a lush, durable

Carolina Mobile Autoservice: Your Go-To For Expert Brake Service Repair
25 Jan

Carolina Mobile Autoservice: Your Go-To For Expert Brake Service Repair

For vehicle owners, the braking system is among the most critical components for safety and performance. Carolina Mobile Autoservice, a

Safeguarding Your Property: Expert Water Damage Services By MBC Capital
24 Jan

Safeguarding Your Property: Expert Water Damage Services By MBC Capital

Water damage can significantly impact homes and businesses, often leaving a trail of destruction and disruption in its wake. Understanding