Skip to content

Commit 874375d

Browse files
committed
feat: Demonstrate use of super keyword for accessing parent variables and methods
WHAT the code does: - Defines base class Vehical: - int maxSpeed = 120. - Method vroom() prints "Vroom Vroom". - Defines subclass Car2: - Declares its own int maxSpeed = 100, hiding the parent field. - display() uses super.maxSpeed to access the parent’s variable (prints 120). - Overrides vroom() but calls super.vroom() inside, reusing the parent implementation. - main(): - Creates Car2 instance. - Calls display() → prints parent’s maxSpeed (120). - Calls vroom() → executes Vehical’s vroom() via super, printing "Vroom Vroom". WHY this matters: - Shows **field hiding** vs **method overriding**: - Fields in Java are hidden, not overridden. `super.maxSpeed` allows access to parent’s version. - Methods can be overridden. Using `super.methodName()` allows calling the parent’s version. - Demonstrates **super keyword usage**: - Access parent’s variables when child defines its own with the same name. - Call parent’s methods from an overridden method in the child. - Also usable to call parent constructors (`super(...)`), though not shown here. HOW it works: 1. new Car2() creates an object with both parent and child fields (two copies of maxSpeed). 2. display() prints parent’s field (120). 3. Car2.vroom() executes, which explicitly calls Vehical.vroom() using super → "Vroom Vroom". Tips and gotchas: - Use `super` when the child needs to leverage or extend behavior of the parent. - Fields don’t participate in polymorphism — accessing `maxSpeed` without super in Car2 would give 100, but with super it gives 120. - Always call super() in constructors if the parent has required initialization. Use-cases / analogies: - Child reusing parent’s “tools” (methods or data) while also defining its own versions. - Like a son saying “I’ll use my father’s way of driving” (`super.vroom()`) but also having his own car speed field. Short key: java super-keyword method-overriding field-hiding inheritance. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 505d921 commit 874375d

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed
Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,34 @@
1-
class Vehical
2-
{
1+
class Vehical {
32
int maxSpeed = 120;
43

5-
public void vroom()
6-
{
4+
public void vroom() {
75
System.out.println("Vroom Vroom");
86
}
97
}
108

11-
class Car2 extends Vehical
12-
{
9+
class Car2 extends Vehical {
1310
/*int maxSpeed = 100;*/
11+
1412
int maxSpeed = 100;
15-
public void displau()
16-
{
13+
14+
public void display() {
1715
System.out.println(super.maxSpeed);
1816
}
1917

2018
//Super keyword on method
21-
public void vroom()
22-
{
19+
public void vroom() {
2320
super.vroom();
2421
}
2522
}
2623

27-
public class SuperKeywrd
28-
{
29-
public static void main(String[] args)
30-
{
24+
public class SuperKeyword {
25+
public static void main(String[] args) {
3126
/*
3227
Car c = new Car();
3328
System.out.println(c.maxSpeed);
3429
*/
3530
Car2 c = new Car2();
36-
c.displau();
31+
c.display();
3732
c.vroom();
3833
}
39-
}
34+
}

0 commit comments

Comments
 (0)