|
1 | | -# Runtime Polymorphism in Java |
2 | | -A beginner-friendly Java program demonstrating runtime polymorphism (dynamic method dispatch) — one of the four pillars of Object-Oriented Programming. |
| 1 | +# 🐾 Runtime Polymorphism in Java |
3 | 2 |
|
4 | | -What it covers: |
| 3 | +A beginner-friendly Java project demonstrating **Runtime Polymorphism** (Dynamic Method Dispatch) — one of the four core pillars of Object-Oriented Programming (OOP). |
5 | 4 |
|
6 | | -A base class (Animal) with an overridable method (makeSound()) |
7 | | -Multiple subclasses (Dog, Cat, Cow) that override the method |
8 | | -A main class that calls the method via a parent reference — where the JVM decides at runtime which version to execute |
| 5 | +--- |
9 | 6 |
|
10 | | -Why it's useful: Shows how the same method call behaves differently depending on the actual object type, without needing to know the type at compile time. |
| 7 | +## 📌 What is Runtime Polymorphism? |
11 | 8 |
|
| 9 | +**Runtime Polymorphism** occurs when a method call to an overridden method is resolved at **runtime** rather than at compile time. |
| 10 | + |
| 11 | +In Java, this is achieved through: |
| 12 | +- **Method Overriding** – A subclass provides its own implementation of a method defined in the parent class. |
| 13 | +- **Parent Class Reference** – A reference variable of the parent class points to a child class object. |
| 14 | +- **Dynamic Method Dispatch** – The JVM decides which method to call based on the actual object type at runtime. |
| 15 | + |
| 16 | +> 💡 Key Rule: The method that gets called is determined by the **object type**, not the **reference type**. |
| 17 | +
|
| 18 | +--- |
| 19 | + |
| 20 | +## 📁 Project Structure |
| 21 | + |
| 22 | +``` |
| 23 | +java-runtime-polymorphism/ |
| 24 | +│ |
| 25 | +├── Animal.java # Base/Parent class |
| 26 | +├── Dog.java # Subclass 1 - overrides makeSound() |
| 27 | +├── Cat.java # Subclass 2 - overrides makeSound() |
| 28 | +├── Cow.java # Subclass 3 - overrides makeSound() |
| 29 | +├── Main.java # Entry point - demonstrates polymorphism |
| 30 | +└── README.md |
| 31 | +``` |
| 32 | + |
| 33 | +--- |
| 34 | + |
| 35 | +## 🧩 Class Overview |
| 36 | + |
| 37 | +### `Animal.java` – Parent Class |
| 38 | +Defines the base method `makeSound()` that all subclasses will override. |
| 39 | + |
| 40 | +```java |
| 41 | +class Animal { |
| 42 | + void makeSound() { |
| 43 | + System.out.println("Some animal makes a sound"); |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +--- |
| 49 | + |
| 50 | +### `Dog.java` – Subclass |
| 51 | +Overrides `makeSound()` with dog-specific behavior. |
| 52 | + |
| 53 | +```java |
| 54 | +class Dog extends Animal { |
| 55 | + @Override |
| 56 | + void makeSound() { |
| 57 | + System.out.println("Dog says: Woof!"); |
| 58 | + } |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +--- |
| 63 | + |
| 64 | +### `Cat.java` – Subclass |
| 65 | +Overrides `makeSound()` with cat-specific behavior. |
| 66 | + |
| 67 | +```java |
| 68 | +class Cat extends Animal { |
| 69 | + @Override |
| 70 | + void makeSound() { |
| 71 | + System.out.println("Cat says: Meow!"); |
| 72 | + } |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +--- |
| 77 | + |
| 78 | +### `Cow.java` – Subclass |
| 79 | +Overrides `makeSound()` with cow-specific behavior. |
| 80 | + |
| 81 | +```java |
| 82 | +class Cow extends Animal { |
| 83 | + @Override |
| 84 | + void makeSound() { |
| 85 | + System.out.println("Cow says: Moo!"); |
| 86 | + } |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +--- |
| 91 | + |
| 92 | +### `Main.java` – Entry Point |
| 93 | +Demonstrates how a **parent class reference** calls different overridden methods at runtime. |
| 94 | + |
| 95 | +```java |
| 96 | +public class Main { |
| 97 | + public static void main(String[] args) { |
| 98 | + |
| 99 | + Animal myAnimal; // Parent class reference |
| 100 | + |
| 101 | + myAnimal = new Dog(); // Points to Dog object |
| 102 | + myAnimal.makeSound(); // Output: Dog says: Woof! |
| 103 | + |
| 104 | + myAnimal = new Cat(); // Points to Cat object |
| 105 | + myAnimal.makeSound(); // Output: Cat says: Meow! |
| 106 | + |
| 107 | + myAnimal = new Cow(); // Points to Cow object |
| 108 | + myAnimal.makeSound(); // Output: Cow says: Moo! |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +--- |
| 114 | + |
| 115 | +## ▶️ Output |
| 116 | + |
| 117 | +``` |
| 118 | +Dog says: Woof! |
| 119 | +Cat says: Meow! |
| 120 | +Cow says: Moo! |
| 121 | +``` |
| 122 | + |
| 123 | +Even though `myAnimal` is of type `Animal`, the **actual object** at runtime determines which `makeSound()` is called. This is **Runtime Polymorphism** in action. |
| 124 | + |
| 125 | +--- |
| 126 | + |
| 127 | +## ⚙️ How to Run |
| 128 | + |
| 129 | +### Prerequisites |
| 130 | +- Java Development Kit (JDK) 8 or above installed |
| 131 | +- A terminal / command prompt |
| 132 | + |
| 133 | +### Steps |
| 134 | + |
| 135 | +1. **Clone the repository** |
| 136 | + ```bash |
| 137 | + git clone https://github.com/your-username/java-runtime-polymorphism.git |
| 138 | + cd java-runtime-polymorphism |
| 139 | + ``` |
| 140 | + |
| 141 | +2. **Compile all Java files** |
| 142 | + ```bash |
| 143 | + javac *.java |
| 144 | + ``` |
| 145 | + |
| 146 | +3. **Run the program** |
| 147 | + ```bash |
| 148 | + java Main |
| 149 | + ``` |
| 150 | + |
| 151 | +--- |
| 152 | + |
| 153 | +## 🧠 Concepts Covered |
| 154 | + |
| 155 | +| Concept | Description | |
| 156 | +|---|---| |
| 157 | +| Inheritance | `Dog`, `Cat`, `Cow` extend `Animal` | |
| 158 | +| Method Overriding | Subclasses provide their own `makeSound()` | |
| 159 | +| Dynamic Method Dispatch | JVM resolves method call at runtime | |
| 160 | +| Upcasting | Parent reference holds child object | |
| 161 | + |
| 162 | +--- |
| 163 | + |
| 164 | +## 🆚 Compile-time vs Runtime Polymorphism |
| 165 | + |
| 166 | +| Feature | Compile-time (Static) | Runtime (Dynamic) | |
| 167 | +|---|---|---| |
| 168 | +| Also called | Method Overloading | Method Overriding | |
| 169 | +| Resolved at | Compile time | Runtime | |
| 170 | +| Example | Same method name, different parameters | Parent reference, child object | |
| 171 | + |
| 172 | +--- |
| 173 | + |
| 174 | +## 📚 Prerequisites to Understand This |
| 175 | + |
| 176 | +Before exploring this project, it helps to know: |
| 177 | +- Basic Java syntax |
| 178 | +- Classes and Objects |
| 179 | +- Inheritance (`extends` keyword) |
| 180 | +- The `@Override` annotation |
| 181 | + |
| 182 | +--- |
| 183 | + |
| 184 | +## 🤝 Contributing |
| 185 | + |
| 186 | +Contributions are welcome! Feel free to: |
| 187 | +- Add more subclasses (e.g., `Bird`, `Lion`) |
| 188 | +- Add a GUI version |
| 189 | +- Extend with interfaces to show interface-based polymorphism |
| 190 | + |
| 191 | +Fork the repo, make your changes, and open a pull request. |
| 192 | + |
| 193 | +--- |
| 194 | + |
| 195 | + |
| 196 | + |
| 197 | +> ⭐ If this helped you understand runtime polymorphism, consider giving the repo a star! |
0 commit comments