What are virtual methods?
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 |
Is string a datatype?
What is nextint java?
What do u mean by method and also contructer in java ?
What are synchronized methods ?
What is string manipulation?
Difference between start() and run() method of thread class?
What classes of exceptions, thrown by a throw statement?
What is * argv?
What is a qualifier in a sentence?
If system.exit (0); is written at the end of the try block, will the finally block still execute?
How can you read content from file in java?
What is scope & storage allocation of static, local and register variables? Explain with an example.