Abstract class or methods example-1
This example shows that a abstract class can hold abstract and non abstract methods.
This example shows that a abstract class can hold abstract and non abstract methods.
package roseindia1;
// abstract class can hold non abstract method
abstract class Animal {
void runAnimal() {
System.out.println("runAnimal() method called form Animal class"); // this
// statement will override by the subclass method runAnimal
}
void printAnimal() {
System.out.println("printAnimal() method called form Animal class");
}
}
class BuzzwordAnimal extends Animal {
void printAnimal() {
System.out.println("printAnimal() method called form BuzzwordAnimal class");
}
void runAnimal() {
super.runAnimal();
// super keyword helps to call method of superclass ie Animal
printAnimal(); // this invoke the buzzwordAnimal class method
}
}
class AbstractExample {
public static void main(String[] args) {
BuzzwordAnimal b = new BuzzwordAnimal();
b.runAnimal(); // overriding will take place as it has similar method
// name exits both is sub and superclass
}
}
/*
* --------------------OUTPUT--------------------
* runAnimal() method called form Animal class printAnimal() method called form
* BuzzwordAnimal class
*/
|