What are virtual methods?



What are virtual methods?..

Answer / 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

More Core Java Interview Questions

There can be a abstract class without abstract methods, but what is the need to declare a class abstract if it doesn't contain abstract methods?

5 Answers   HCL,


What types of index data structures can you have in java?

0 Answers   Tech Mahindra,


What is ide with example?

0 Answers  


In Java list the methods that can be overridden?

0 Answers   Accenture,


What is java english?

0 Answers  






What is the difference between Stream Reader and Stream Writer?

4 Answers  


java is puerly object oriented or not ?

5 Answers  


whats the diff between jsp and servlets?

9 Answers   HCL, TCS,


Is ResultSet class?

5 Answers   Bally Technologies, TCS,


What is Yield() method when we r using this ? tell exactly

2 Answers  


Explain, why the constructor is required in implemented class?

4 Answers  


Are maps ordered java?

0 Answers  


Categories