Skip to content

YuriiJavaDev/JavaBasics_Task_285_V0.1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Virtual Pets: Multiple Subclasses (JavaBasics_Task_285_V0.1)

📖 Description

The power of Inheritance lies in its ability to create a scalable architecture. This project builds upon the virtual pet world by introducing a Dog class that inherits from the same Animal base class as the previous Cat implementation. This demonstrates Hierarchical Inheritance, where multiple subclasses share a single superclass. Both dogs and cats can sleep, but only dogs can bark, illustrating how specific behaviors are encapsulated within their respective subclasses while sharing common traits.

📋 Requirements Compliance

  • Superclass Consistency: Reused the Animal class with a shared petName field and sleep() method.
  • Hierarchical Inheritance: Created a new Dog subclass that extends Animal without affecting existing code.
  • Specific Behavior: Implemented a unique bark() method for the Dog class.
  • Member Access: Confirmed that Dog successfully accesses the inherited petName field.
  • Clean Architecture: Separated domain models into distinct files, keeping the DogLauncherApp clean.

🚀 Architectural Stack

  • Java 8+ (Inheritance, Polymorphism, Access Modifiers)

🏗️ Implementation Details

  • Animal: The superclass providing the sleep() behavior.
  • Dog: The specialized subclass with the bark() behavior.
  • DogLauncherApp: The entry point to instantiate and test the canine pet.

📋 Expected result

Sharik is sleeping.
Sharik says: Woof!

💻 Code Example

Project Structure:

JavaBasics_Task_285/
├── src/
│   └── com/yurii/pavlenko/
│                 ├── Animal.java
│                 ├── Dog.java
│                 └── DogLauncherApp.java
└── README.md

Code

package com.yurii.pavlenko;

public class DogLauncherApp {

    public static void main(String[] args) {

        Dog dog = new Dog();

        dog.petName = "Sharik";

        dog.sleep();
        dog.bark();
    }
}
package com.yurii.pavlenko;

public class Animal {

    public String petName;

    public void sleep() {
        System.out.println(petName + " is sleeping.");
    }
}
package com.yurii.pavlenko;

public class Dog extends Animal {

    public void bark() {
        System.out.println(petName + " says: Woof!");
    }
}

⚖️ License

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

About

This is a tutorial project. JavaBasics_Task_285_V0.1 Virtual Pets: expanding class hierarchies with multiple subclasses and demonstrating method inheritance across different entities. 300326_1202

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages