-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathH_04_Single_Level_Inheritance.java
66 lines (55 loc) · 1.8 KB
/
H_04_Single_Level_Inheritance.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Single Level Inheritance
public class H_04_Single_Level_Inheritance {
public static void main(String[] args) {
// Creating an object of the subclass Dog
Dog myDog = new Dog("Buddy", 3, "Golden Retriever");
// Accessing methods inherited from the superclass
myDog.eat(); // Output: Buddy is eating.
// Accessing method specific to the subclass
myDog.bark(); // Output: Buddy says: Woof! Woof!
// Displaying all information using the overridden method
myDog.displayInfo();
// Output:
// Name: Buddy, Age: 3
// Breed: Golden Retriever
}
}
// Superclass: Animal
class Animal {
// Attributes (fields) of the superclass
String name;
int age;
// Constructor of the superclass
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
// Method of the superclass
public void eat() {
System.out.println(name + " is eating.");
}
// Method to display animal details
public void displayInfo() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
// Subclass: Dog
class Dog extends Animal {
// Additional attribute specific to Dog
String breed;
// Constructor of the subclass
public Dog(String name, int age, String breed) {
// Call the constructor of the superclass (Animal)
super(name, age);
this.breed = breed;
}
// Additional method specific to Dog
public void bark() {
System.out.println(name + " says: Woof! Woof!");
}
// displayInfo method to include breed
public void displayInfo() {
super.displayInfo(); // Call the superclass method
System.out.println("Breed: " + breed);
}
}