In Object-Oriented Programming, Inheritance allows a new class to acquire the properties and behaviors of an existing class. This project demonstrates this core concept by creating a base class Animal and a specialized subclass Cat. The Cat class automatically receives the petName field and the eat() method from Animal, proving the power of code reuse. Additionally, the subclass introduces its own specific behavior, meow(), illustrating how hierarchies evolve from general to specific.
- Superclass Definition: Created
Animalwith shared state (petName) and behavior (eat). - Subclass Specialization: Implemented
Catusing theextendskeyword to inherit fromAnimal. - Field Reusability: Demonstrated that the subclass can access and use fields defined in the parent class.
- Extended Behavior: Added a unique
meow()method specific only to theCatclass. - Clean Architecture: Separated the domain models from the
PetLauncherAppentry point.
- Java 8+ (Inheritance,
extendskeyword, Access Modifiers)
- Animal: The base (super) class providing the common blueprint.
- Cat: The derived (sub) class extending the base functionality.
- PetLauncherApp: The bootstrap class to instantiate and interact with the virtual pet.
Barsik is eating.
Barsik says: Meow!
Project Structure:
JavaBasics_Task_284/
├── src/
│ └── com/yurii/pavlenko/
│ ├── Animal.java
│ ├── Cat.java
│ └── PetLauncherApp.java
└── README.md
Code
package com.yurii.pavlenko;
public class PetLauncherApp {
public static void main(String[] args) {
Cat cat = new Cat();
cat.petName = "Barsik";
cat.eat();
cat.meow();
}
}package com.yurii.pavlenko;
public class Animal {
public String petName;
public void eat() {
System.out.println(petName + " is eating.");
}
}package com.yurii.pavlenko;
public class Cat extends Animal {
public void meow() {
System.out.println(petName + " says: Meow!");
}
}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