Skip to content

Commit 92521b0

Browse files
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

4 files changed

+20
-24
lines changed

Section11ObjectOrientedProgramming/OOPs 2.O/src/Test/Animal.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ public class Animal {
44
String name;
55
String age;
66

7-
//Method Behavior hota hai
87
public void makeSound() {
9-
System.out.println("MAke Some sound");
8+
System.out.println("Make Some sound");
109
}
1110
}

Section11ObjectOrientedProgramming/OOPs 2.O/src/Test/Cat.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package Test;
22

33
public class Cat extends Dog {
4-
54
@Override
65
public void makeSound() {
76
System.out.println("Meow Meow Meow");
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package Test;
2-
//Inheritance and Acheving code re-use ability of code.
3-
public class Dog extends Animal
4-
{
2+
3+
//Inheritance and Achieving code re-use ability of code.
4+
public class Dog extends Animal {
55
String breed;
66

77
@Override
8-
public void makeSound()
9-
{
8+
public void makeSound() {
109
System.out.println("Bark Woof Woof");
1110
}
1211
}
Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
package Test;
22

3-
public class TestClass
4-
{
5-
public static void main(String[] args)
6-
{
7-
Dog Dog = new Dog();
3+
public class TestClass {
4+
public static void main(String[] args) {
5+
Dog Obj = new Dog();
86

97
//Inherit kar Raha hai properties ko.
10-
Dog.age = "4";
11-
Dog.breed = "Bob";
12-
Dog.name = "Bob";
8+
Obj.age = "4";
9+
Obj.breed = "Bob";
10+
Obj.name = "Bob";
1311

1412

15-
//Polymorpisam
16-
//Object create kar rahe hai. Dog ka obj and refrence Animal ka hai.
17-
//Sub class ke obj kp treat kar sakte ho as a instance/refernces of super class.
18-
Animal dog = new Dog();
19-
dog.makeSound(); //Obj dog ka and referance super class ka
13+
//Polymorphism.
14+
//Object create kar rahe hai. Dog ka obj and reference Animal ka hai.
15+
//Subclass ke obj kp treat kar sakte ho as a instance/references of super class.
16+
Animal aa = new Dog();
17+
aa.makeSound(); //Obj dog ka and reference super class ka.
2018

2119
//Abstraction
22-
//Hidding Implementaion details().
23-
//Tv and remote ka exampl . we dont care about the internal working of the Tv just foucs on the working of remote.
20+
//Hiding Implementation details().
21+
//Tv and remote ka example .
22+
//we don't care about the internal working of the Tv just focus on the working of remote.
2423
//Car chala rahe ho we hide details like gear working and other things.
2524
}
2625
}
2726

28-
//imperative and Functional paradiam(classes and obj) are style of writing program code.
27+
//imperative and Functional paradigm(classes and obj) are style of writing program code.

0 commit comments

Comments
 (0)