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

I want to persist data of objects for later use. What’s the best approach to do so?

507


Difference between ‘is-a’ and ‘has-a’ relationship in java?

543


What are the library functions in java?

550


Is int primitive data type?

507


Explain the Propertie sof class?

586






How list contains works in java?

558


What is argument in java?

540


What is the order of arraylist in java?

611


what is thread? What are the high-level thread states? Or what are the states associated in the thread? : Java thread

500


what methods would you overwrite in java.lang.object class?

567


What is the major drawback of internal iteration over external iteration?

589


Is namespace same as package in java?

545


Why there are some null interface in JAVA? What does it mean? Give some null interface in JAVA?

618


What is exception propagation?

593


What is the byte range?

582