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
Can we split string with in java?
What is the best definition for data?
How do you input a string in java?
What types of index data structures can you have in java?
What are actual parameters?
What is methods in java?
What is fail fast in java?
If an application has multiple classes in it, is it okay to have a main method in more than one class?
FOR EXAMPLE WE R HAVING TWO LIST ELEMENTS ..BOTH LISTS CONTAINS ID,NAME,PLACE ..I NEED TO COMPARE BOTH IDS IN TWO LISTS,IF ID'S R SAME MEANS WE HAVE ADD THE DETAILS(LIKE NAME,PLACE) TO MAP...HOW IS POSSIBLE ?CAN ANY ONE SUGGEST?
Can we convert list to set in java?
What is anonymous inner class?
When throw keyword is used?
Can a class be declared as protected?
Can we force the garbage collection to run?
What are synchronized methods ?