A form of abstraction which allows you to deal with a superclass rather than the details of an implementation. A rather contrived example follows:
abstract class demoVMI{
abstract void greetings(); // <---- virtual method common across all subclasses
}
class maltese extends demoVMI {
@Override
void greetings() { greetingInMaltese(); } // <---- virtual method calls actual method here
void greetingInMaltese() { System.out.println("Bongu!"); }
}
class estonian extends demoVMI {
@Override
void greetings() { greetingInEstonian(); } // <---- virtual method calls actual method here
void greetingInEstonian() { System.out.println("Tere!"); }
}
public class Main {
private static void greet(demoVMI greeting){ greeting.greetings(); }
public static void main(String[] args) {
maltese Malteser = new maltese();
estonian Eesti = new estonian();
greet(Malteser);
greet(Eesti);
}
}
Note that in Main , the greet method does not know about the implementation-specific greetingInMaltese and greetingInEstonian methods. It just calls the virtual method greetings, which is present in both implementations because of the abstract class. Within the child classes, the virtual method greetings serves only to call the actual method providing the intended functionality
A form of abstraction which allows you to deal with a superclass rather than the details of an implementation. A rather contrived example follows:
Note that in
Main
, thegreet
method does not know about the implementation-specificgreetingInMaltese
andgreetingInEstonian
methods. It just calls the virtual methodgreetings
, which is present in both implementations because of the abstract class. Within the child classes, the virtual methodgreetings
serves only to call the actual method providing the intended functionality