What are the ways of polymorphism other than Overridding &
Overloading



What are the ways of polymorphism other than Overridding & Overloading..

Answer / 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

More Core Java Interview Questions

What is qualitative variable?

1 Answers  


What is the use of bin and lib in JDK?

8 Answers   TCS,


How can you handle java exceptions?

1 Answers  


Can you call a private data from an inner class?

6 Answers  


What are JVM.JRE, J2EE, JNI?

1 Answers  


What is an example of declaration?

1 Answers  


What happens if we override private method?

1 Answers  


Can an abstract class have a constructor?

1 Answers   RBS, Wipro,


what is the need to set path in java? how many ways to set path in java? Explain breif?

5 Answers  


How a variable is stored in memory?

1 Answers  


What are other modifiers?

2 Answers   Wipro,


What are the different types of inheritance in java?

1 Answers  


Categories