Thursday, March 12, 2009

What is meant by Virtual function in Java? Does Java supports Virtual

Question :What is meant by Virtual function in Java? Does Java supports Virtual
function? (CoreJava)

Answer :Java supports Virtual functions, all functions in Java are virtual by default.
Virtual functions or virtual methods are functions or methods that will be
redefined in derived classes.
Pure Virtual functions from C++ could be abstract functions without body.
Make your class abstract, define the pure virtual methods you want
subclasseses to provide implementation for
(using the abstract keyword in their definition) and you should be good.
public abstract class MyClass {
public void concreteInitThing() {
// your code here
}
public abstract void specificImplementation(); //Virtual method
}
public class ConcreteImplementationOfMyClass extends MyClass {
public void specificImplementation() {
// Your code here
}
}

No comments: