What are virtual methods?

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the static variable?

556


What is the full name of java?

544


Explain jvm, jre, and jdk?

536


Difference between static and dynamic class loading.

558


Is zero a natural number?

528






What is singletonlist in java?

487


Can we override compareto method?

475


What does += mean in java?

549


Can you override private or static method in java?

524


Which is better stringbuffer or stringbuilder?

510


Is void a type?

566


What is the final variable?

552


What are java packages?

557


Do extraneous variables affect validity?

504


Who is the owner of java?

511