Follow Our FB Page << CircleMedia.in >> for Daily Laughter. We Post Funny, Viral, Comedy Videos, Memes, Vines...


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 ?    4 Yes 0 No

Post New Answer

More Core Java Interview Questions

Does constructor creates the object ?

0 Answers  


What is difference between local variable and global variable?

0 Answers  


Explain, why the constructor is required in implemented class?

4 Answers  


What happens if an exception is throws from an object's destructor?

0 Answers   Amazon,


What does system out println () do?

0 Answers  






Can we create our own daemon thread?

0 Answers  


How many bits is a char?

0 Answers  


What are daemon Threads in java?

0 Answers   Impetus,


Are arrays primitive data types?

0 Answers  


What is string in java with example?

0 Answers  


Which methods are used during serialization and deserialization process?

0 Answers  


Write a java program to check if a number is prime or not?

0 Answers  






Categories