Polymorphism allows objects of different classes to be treated as objects of a common superclass. This project demonstrates Upcasting by treating a Dog instance as a generic Animal. Even though the reference variable is of type Animal, the Java Virtual Machine (JVM) correctly identifies the underlying object at runtime and executes the overridden makeSound() method. This decoupling of the reference type from the actual object type is the foundation of extensible software architecture.
- Base Class Template: Defined
Animalwith a default sound implementation. - Method Overriding: Specialized the
makeSound()behavior in theDogsubclass. - Upcasting Demonstration: Assigned a
Dogobject to anAnimalreference variable. - Runtime Binding: Verified that the subclass implementation is executed instead of the base class version.
- Clean Code structure: Organized into logical packages (
animal,dog,app).
- Java 8+ (Inheritance, Polymorphism, Late Binding)
- Animal: The general abstraction of a zoo inhabitant.
- Dog: The concrete implementation with specific behavior.
- ZooLauncherApp: The execution entry point demonstrating polymorphic interaction.
Woof!
Project Structure:
JavaBasics_Task_304/
├── src/
│ └── com/yurii/pavlenko/
│ ├── app/
│ │ └── ZooLauncherApp.java
│ └── animal/
│ ├── dog/
│ │ └── Dog.java
│ └── Animal.java
├── LICENSE
├── TASK.md
├── THEORY.md
└── README.md
Code
package com.yurii.pavlenko.app;
import com.yurii.pavlenko.animal.Animal;
import com.yurii.pavlenko.animal.dog.Dog;
public class ZooLauncherApp {
public static void main(String[] args) {
Animal pet = new Dog();
pet.makeSound();
}
}package com.yurii.pavlenko.animal;
public class Animal {
public void makeSound() {
System.out.println("Some sound...");
}
}package com.yurii.pavlenko.animal.dog;
import com.yurii.pavlenko.animal.Animal;
public class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Woof!");
}
}This project is licensed under the MIT License.
Copyright (c) 2026 Yurii Pavlenko
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files...
License: MIT