Reputation: 19
I have created two methods in class A being overridden in class B as shown below. I'm having some questions related to dynamic polymorphism and overriding.
Here is my code where class B extends class A.
public class A {
public void methoda()
{
System.out.println("a");
}
public void methodb()
{
System.out.println("aaa");
}
public static void main(String[] args) {
B a =new B();
A b=a;
b.methoda();
}
}
public class B extends A{
@overrides
public void methoda()
{
System.out.println("A");
}
@overrides
public void methodb()
{
System.out.println("g");
}
}
Here I'm overriding two methods and when a superclass reference is used then the method invoked depends on the type of object decided at run time and is an example of dynamic polymorphism.
But if I use a subclass reference for subclass object and override the method then will the overridden get resolved at run time and is a case of dynamic polymorphism or is resolved at compile time only since object type and refernece are of same type and doesnt remain the case of dynamic polymorphism?
Does overriding and dynamic polymorphism occure at the same time always??
Upvotes: 0
Views: 2481
Reputation: 1
class Bike {
void run() {
System.out.println("running");
}
}
class Splender extends Bike {
void run() {
System.out.println("Bike is running");
}
public static void main(String args[]) {
Bike a = new Bike();
a.run(); //output: running
Bike b = new Splender();
b.run(); //output: Bike is running
}
}
Here Superclass (Bike) reference points to the subclass object. The object gets created at runtime and not at compile time. It doesn't matter whether the,
1) reference variable and object type is same or
2) reference variable is superclass type and object is of subclass type.
In the above example object type cannot be determined by the compiler, because the instance of Splender is also an instance of Bike. The method is invoked at runtime by JVM based on which type of object gets created. The both cases are of Dynamic Polymorphism and not the compile time polymorphism.
Upvotes: 0
Reputation: 7
If you use a subclass reference to the subclass object there is no need for overridding.. Override occurs only when a pointer of base class points to an object of one of its subclass and this pointer calls a method of the base class.. but the actual method to get executed ithe method of the sub class.
Upvotes: 0
Reputation: 10224
First, (subtype) polymorphism is always dynamic(i.e. at runtime), so "dynamic" modifier is redundant, unless it's used to distinguish from ad-hoc polymorphism or parametric polymorphism.
Second, @Override(not @overrides) annotation isn't mandatory(in fact it's only introduced by Java 5.0), although it's recommended for more readable(explicitly shows the overridden method) and more robust(compiler will check the misspelt methods) code.
In a word, method overriding is an OOP language's key feature, and annotation @Override is checked at compile-time, while polymorphic behavior is resolved at runtime.
Upvotes: 1
Reputation: 691735
I'm not sure I understand the question, but polymorphic methods are always resolved at runtime. The executed method is the one of the concrete runtime type of the object. The declared type of the variable referencing it doesn't matter.
A Lion is a Lion, and it will always roar, aven if you reference it as an Animal.
Upvotes: 3
Reputation: 3894
Doesn't matter if you reference by the subclass or not. What matters is the instance that is created. If your instance is created from B, you will get B's results.
Upvotes: 0