Commit 92521b0
committed
feat: Add Animal, Dog, Cat, and TestClass to demonstrate OOP concepts in Java
WHAT the code does:
Defines an Animal superclass with fields (name, age) and a makeSound() method.
Defines Dog as a subclass of Animal:
- Adds a field breed.
- Overrides makeSound() to print "Bark Woof Woof".
Defines Cat as a subclass of Dog:
- Overrides makeSound() to print "Meow Meow Meow".
Defines TestClass with main() to demonstrate inheritance, polymorphism, and abstraction.
WHY this matters:
Shows how inheritance allows code reuse (Dog inherits name and age from Animal).
Illustrates method overriding, where subclasses provide their own behavior for makeSound().
Demonstrates polymorphism:
- An Animal reference can point to a Dog object.
- Calls to overridden methods execute subclass behavior at runtime.
Introduces abstraction through commentary: focusing on behavior (makeSound) rather than implementation details.
HOW it works:
Dog Obj = new Dog():
- Sets inherited fields (age, name) and Dog-specific field (breed).
Animal aa = new Dog():
- Reference type is Animal, object is Dog.
- aa.makeSound() calls Dog’s implementation due to dynamic dispatch.
Cat extends Dog, showing multilevel inheritance and further overriding.
Tips and gotchas:
Multilevel inheritance (Animal → Dog → Cat) works but should be used carefully to avoid deep hierarchies.
Using Animal aa = new Dog() demonstrates runtime polymorphism but limits access to Dog-specific fields like breed.
In real-world designs, interfaces (e.g., SoundMaker) may better express behavior contracts than base classes.
Avoid overusing inheritance; composition often provides cleaner, more flexible designs.
Use-cases:
Educational demonstration of inheritance, method overriding, and polymorphism in Java.
Foundation for teaching abstraction and the principles of object-oriented programming.
Practical stepping stone toward more advanced OOP topics like interfaces, abstract classes, and design patterns.
Short key: class-animal-dog-cat oop-inheritance polymorphism-abstraction.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent c5fb70d commit 92521b0
File tree
4 files changed
+20
-24
lines changed- Section11ObjectOrientedProgramming/OOPs 2.O/src/Test
4 files changed
+20
-24
lines changedLines changed: 1 addition & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
8 | 7 | | |
9 | | - | |
| 8 | + | |
10 | 9 | | |
11 | 10 | | |
Lines changed: 0 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
5 | 4 | | |
6 | 5 | | |
7 | 6 | | |
| |||
Lines changed: 4 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | | - | |
4 | | - | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
9 | | - | |
| 8 | + | |
10 | 9 | | |
11 | 10 | | |
12 | 11 | | |
Lines changed: 15 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
8 | 6 | | |
9 | 7 | | |
10 | | - | |
11 | | - | |
12 | | - | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
13 | 11 | | |
14 | 12 | | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
20 | 18 | | |
21 | 19 | | |
22 | | - | |
23 | | - | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
24 | 23 | | |
25 | 24 | | |
26 | 25 | | |
27 | 26 | | |
28 | | - | |
| 27 | + | |
0 commit comments