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
Explain the difference between transient and volatile in java?
What is the use of StringTokenizer class?
What are serialization and deserialization?
Explain the importance of throws keyword in java?
What is a concrete classes? Is Java object class is concrete class?
What is a line break example?
Which is better stringbuilder or stringbuffer?
Can we make constructors static?
How does finally block differ from finalize() method?
Is there a jre for java 11?
What do you understand by Header linked List?
What is a void method?
Explain the reason behind ending a program with a system.exit(0)?
What is a java string?
Can string be considered as a keyword?