I'm new to Java Programming and learning polymorphism.
__EDIT__
As per the answers I received from everyone,I have code:
Here I'm typecasting my Derived
object (obj
) to Base
type and then calling method()
.
public class Tester {
public static void main(String[] args) {
Base obj=(Base)new Derived();
obj.method();
}
}
class Base{
public void method(){
System.out.println("In Base");
}
}
class Derived extends Base{
public void method(){
System.out.println("In Derived");
}
}
Output I'm getting is: "In Derived".
So after typecasting my object should become of type Base
referenced by Base
type.
But it's not happening? Why?
Does typecast work on child->parent conversion or it has no effect here?
Base obj=new Derived();
In the above statement, the reference part points to type Base
. This is how the compiler identifies which class to consider. But your code will create an error. Let me explain the structure of the above statement before explaining why will it show an error.
Structure of the above statement:
Base obj
.Instantiation: The new keyword is a Java operator that creates the object/allocates space in the memory.
Initialization: The new operator is followed by a call to a constructor, which initializes the new object.
Since, Derived
is a sub-class of Base
, you are allowed to call the constructor in the Derived
class. This is how inheritance and polymorphism works.
Okay, Now let us go back to the error part.
The obj.method()
in your code is looking for a function method()
in Base
class but the method(int a)
function in Base
class requires an argument of type integer to be passed. So for the code to work, the calling statement has to be something like obj.method(5)
.This statement works because the calling statement is actually passing 5 to the function.
There is an easy fix for your code:
Derived obj=new Derived();
Have you noticed?
Derived
.Why does that work?
method()
function in your Derived
class which doesn't require an integer argument.There is one more amazing fact about inheritance in Java:
Everything possessed by a super-class is also possessed by the sub-class but the reverse is not true. And yes, the sub-class has the right to redefine any method/function it has inherited from super-class.
The above statement means the following code will work:
Derived obj=new Derived();
obj.method(5);
You must be wondering-How come this code works even though method()
in Derived
requires no argument. In fact, Derived
has no method(int a)
.
Well, the answer to this is the amazing fact I have mentioned above.
Yes, method(int a)
also belongs to Derived
since it's a sub-class of Base
.
But How does the code mentioned below work?
Derived obj=new Derived();
obj.method(5);
Simple, the JVM looks for the method(int a)
in class Derived
and it finds the function since Derived
has inherited the function from Base
class.
Remember this too, the sub-class also has a privilege to over-ride a method in super class. This means that you can add method(int a)
function in class Derived
which over-rides the original method inherited from Base
.
How inheritance works?
obj.method(5)
in the above code, the JVM first looks for any over-ridden method of the same type in Derived
. If it does not find any over-ridden method, it moves up in the inheritance hierarchy chain
to the super class and looks for the same method. But the reverse is not the true.