-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuperKeywordDemo.java
More file actions
120 lines (96 loc) · 3.38 KB
/
SuperKeywordDemo.java
File metadata and controls
120 lines (96 loc) · 3.38 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Demo of Java 'super' keyword
// super is used to call parent class methods, constructors, and variables
// Parent class
class Animal {
String name;
int age;
// Parent constructor
Animal(String name, int age) {
this.name = name;
this.age = age;
System.out.println("Animal constructor called: " + name);
}
// Parent method
void displayInfo() {
System.out.println("Animal Name: " + name + ", Age: " + age);
}
void eat() {
System.out.println(name + " is eating...");
}
}
// Child class
class Dog extends Animal {
String breed;
// Child constructor
Dog(String name, int age, String breed) {
// super() calls the parent constructor
super(name, age);
this.breed = breed;
System.out.println("Dog constructor called: " + breed);
}
// Overriding parent method
@Override
void displayInfo() {
// super.displayInfo() calls parent's displayInfo method
super.displayInfo();
System.out.println("Breed: " + breed);
}
@Override
void eat() {
System.out.println(name + " is eating dog food...");
}
// Additional method showing parent method call
void performSuperCall() {
System.out.println("\n--- Demonstrating super keyword ---");
// Calling parent's eat() method
System.out.println("Calling super.eat():");
super.eat();
// Calling child's eat() method
System.out.println("Calling child's eat():");
eat();
// Accessing parent variable through super
System.out.println("Parent variable access - super.name: " + super.name);
}
}
// Another example: Cat class
class Cat extends Animal {
String color;
Cat(String name, int age, String color) {
super(name, age); // Call parent constructor
this.color = color;
System.out.println("Cat constructor called: " + color);
}
@Override
void displayInfo() {
super.displayInfo(); // Call parent method
System.out.println("Color: " + color);
}
@Override
void eat() {
System.out.println(name + " is eating cat food...");
}
}
// Main class to demonstrate
public class SuperKeywordDemo {
public static void main(String[] args) {
System.out.println("===== Super Keyword Demo =====\n");
// Dog example
System.out.println("--- Creating Dog object ---");
Dog dog = new Dog("Buddy", 5, "Golden Retriever");
System.out.println("\n--- Calling displayInfo() ---");
dog.displayInfo();
dog.performSuperCall();
// Cat example
System.out.println("\n\n--- Creating Cat object ---");
Cat cat = new Cat("Whiskers", 3, "Orange");
System.out.println("\n--- Calling displayInfo() ---");
cat.displayInfo();
System.out.println("\n--- Calling eat() ---");
cat.eat();
System.out.println("\n===== Key Points =====");
System.out.println("1. super() - calls parent constructor");
System.out.println("2. super.methodName() - calls parent method");
System.out.println("3. super.variableName - accesses parent variable");
System.out.println("4. When overriding, use super to access parent implementation");
}
}