Page 212 - Scholarly Works of Faculty
P. 212
CAVITE STATE UNIVERSITY
T3 CAMPUS
Department of Information Technology COSC 65 – Programming Languages
Single Inheritance Example
class Animal { public class TestInheritance2 {
void eat() { public static void main(String[]args) {
System.out.prntln(“The animal is eating…”);
} Dog D = new Dog();
}
class Dog extends Animal { D.eat();
void bark() { D.bark();
System.out.println(“The dog is barking…”); }
} }
}
Multilevel Inheritance Example
class Animal {
void eat() {
System.out.prntln(“The animal is eating…”);
}
}
class Dog extends Animal {
void bark() {
System.out.println(“The dog is barking…”);
}
}
class BabyDog extends Dog {
void weep(){
System.out.println(“The Baby Dog is weeping”);
}
}
public class TestInheritance3 {
public static void main(String[]args){
BabyDog D = new babyDog();
D.eat();
D.bark();
D.weep();
}
}
Page | 71