What are the ways of polymorphism other than Overridding &
Overloading
Answer Posted / alka
Interface is third way to achieve polymorphism.Because when
you call a method on any interface variable then at run time
it is decided which object method to call.
Ex :
Let there is a Interface A:
public interface A
{
void display();
}
And two classes B and C implement A
class B implements A
{
display()
{
System.out.println("Class B");
}
}
class C implements A
{
display()
{
System.out.println("Class C");
}
}
Then while using interface A..see what will happen
class mainClass
{
public static void main(String args[])
{
A obj = new C();
obj.display();
obj = new B();
obj.display();
}
}
Output : Class C
Class B
So it is clear that while run/execution time it is decided
which display() method to call..i.e class B or class C display()
| Is This Answer Correct ? | 17 Yes | 0 No |
Post New Answer View All Answers
What is binary search in java?
Can we declare a static variable inside a method?
Can we use different return types for methods when overridden?
What is the difference between static (class) method and instance method?
What is the significance of java packages?
How can u increase the heap size in the memory?
Explain about method local inner classes or local inner classes in java?
Is cout buffered?
What does localhost mean?
What invokes a thread's run() method in java programming?
What is the difference between a window and a frame in java programming?
Why java uses the concept of the string literal?
What is the tradeoff between using an unordered array versus an ordered array?
What are data types in programming?
How many bits are used to represent unicode, ascii, utf-16, and utf-8 characters?