What are virtual methods?



What are virtual methods?..

Answer / amit singh

in java all function are virtual.only the function marked
with the final is not virtual.
means to say that if there is any method in super class and
you implemented this method in subclasses too.
then you can invoke on any subcalsses instances refer to
as a superclasses.and the method behaviour will change
according to subcallses instances .
class Amit
{
public void sleep()
{
System.out.println("amit slleping");
}
public static void main(String []args)
{
Amit a = new Amit();
a.sleep();
a = new Subclass1();
a.sleep();
a= new Subclass2();
a.sleep();
}
}

class Subclass1 extends Amit
{
public void sleep()
{
Sysetm.out.println("subclass sleeping");
}
}

class Subclass1 extends Amit
{
public void sleep()
{
System.out.println("subclass 2 sleeping");
}
}
so the output will be
amit sleeping
Subclass1 sleeping
Subclass2 sleeping

so the eat function behave virtualy for the differnt
instance .so this is called the virtual function .so don't
worry every function in java are virtual not the final
function.

Is This Answer Correct ?    5 Yes 0 No

Post New Answer

More Core Java Interview Questions

What are adapter classes?

0 Answers  


Is namespace same as package in java?

0 Answers  


What do you understand by abstract classes?

0 Answers   HCL,


Why java does not support pointers?

0 Answers  


What are strings in physics?

0 Answers  






A non-static inner class may have object instances that are associated with instances of the class’s outer class. A static inner class does not have any object instances.

0 Answers  


Can an abstract class be a final class?

0 Answers  


What is object-oriented programming?

0 Answers  


How do you define a set in java?

0 Answers  


What are thread priorities and importance of thread priorities in java?

0 Answers  


What do you meant by active and passive objects?

0 Answers   Wipro,


What is the maximum size of hashmap in java?

0 Answers  


Categories