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

Is there any limitation of using inheritance?

0 Answers  


why not override thread to make a runnable? : Java thread

0 Answers  


What is the difference between actual and formal parameters?

0 Answers  


Do we need to manually write Copy Constructor?

0 Answers   HCL,


What is the difference between the paint() and repaint() methods in java programming?

0 Answers  






can you program for reverse string?

7 Answers   IBM,


What is null object in java?

0 Answers  


What are the different types of sorting in java?

0 Answers  


Can we create more than one object singleton class?

0 Answers  


Is JRE required to compile Java files ?

4 Answers   HCL,


When you declare a method as abstract method ?

2 Answers   HP,


What is the default value of local and global variables?

0 Answers  


Categories