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
What is bubble sorting in java?
What is the difference between normal report & matrix report?
When do we use synchronized blocks and advantages of using synchronized blocks?
What is a stringbuffer?
What are the two basic ways in which classes that can be run as threads may be defined?
How do you invoke a method?
What is t type java?
Is array synchronized in java?
What are the concepts of 'OOPS'?
Can we have try block without catch block?
Can we restart a dead thread in java?
How to retrieve data from database in java using arraylist?
What are the data types supported by java?
Is an empty arraylist null?
Explain jvm, jre, and jdk?