-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodOverridingExample.java
More file actions
134 lines (109 loc) · 3.48 KB
/
Copy pathMethodOverridingExample.java
File metadata and controls
134 lines (109 loc) · 3.48 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
121
122
123
124
125
126
127
128
129
130
131
132
/**
* Day 11 - Inheritance: Method Overriding and Super Keyword
*
* Concept:
* Method overriding allows a child class to provide different implementation
* of a method inherited from parent. The 'super' keyword allows calling
* the parent's version of the method.
*
* This example demonstrates:
* - Method overriding with @Override annotation
* - Using super to call parent method
* - Extending parent behavior vs replacing it
* - Polymorphic behavior
*
* Real-life analogy:
* Parent: General greeting
* Child: Specific greeting (can extend parent or replace)
*/
public class MethodOverridingExample {
/**
* Parent class: Shape
*/
static class Shape {
String name;
Shape(String name) {
this.name = name;
}
void display() {
System.out.println("This is a shape: " + name);
}
double area() {
return 0.0;
}
}
/**
* Child class: Circle
*/
static class Circle extends Shape {
double radius;
Circle(String name, double radius) {
super(name);
this.radius = radius;
}
/**
* Extending parent method (calling super first)
*/
@Override
void display() {
super.display(); // Call parent display
System.out.println("Radius: " + radius);
}
/**
* Replacing parent method (not calling super)
*/
@Override
double area() {
return Math.PI * radius * radius;
}
}
/**
* Another child class: Rectangle
*/
static class Rectangle extends Shape {
double length;
double width;
Rectangle(String name, double length, double width) {
super(name);
this.length = length;
this.width = width;
}
@Override
void display() {
super.display(); // Extend parent behavior
System.out.println("Length: " + length + ", Width: " + width);
}
@Override
double area() {
return length * width;
}
}
/**
* Main method - entry point
*/
public static void main(String[] args) {
System.out.println("--- Method Overriding Example ---\n");
// Creating objects
Circle circle = new Circle("Circle", 5.0);
Rectangle rectangle = new Rectangle("Rectangle", 4.0, 6.0);
System.out.println("--- Circle Information ---");
circle.display();
System.out.println("Area: " + circle.area());
System.out.println("\n--- Rectangle Information ---");
rectangle.display();
System.out.println("Area: " + rectangle.area());
System.out.println("\n--- Polymorphic Behavior ---");
Shape[] shapes = {circle, rectangle};
for (Shape shape : shapes) {
System.out.println("\nProcessing " + shape.name + ":");
shape.display();
System.out.println("Area: " + shape.area());
}
System.out.println("\n--- Key Points ---");
System.out.println("1. Both Circle and Rectangle extend parent's display()");
System.out.println("2. Both replace parent's area() calculation");
System.out.println("3. Using super.display() extends parent behavior");
System.out.println("4. Polymorphic call (shape.area()) uses actual class's method");
System.out.println("5. @Override annotation helps catch mistakes");
}
}