Skip to content

YuriiJavaDev/JavaBasics_Task_272_V0.1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Magical Farm: Anonymous Inner Classes (JavaBasics_Task_272_V0.1)

📖 Description

In software development, we often encounter situations where we need to modify the behavior of a single object without creating a whole new class hierarchy. This project demonstrates Anonymous Inner Classes. We take a base class MagicalCreature and override its makeSound() method during instantiation. This approach is highly efficient for "one-off" logic, keeping the codebase clean and localized.

📋 Requirements Compliance

  • Base Class Implementation: Created MagicalCreature with a default makeSound() method.
  • Anonymous Overriding: Used the new ClassName() { ... } syntax to redefine behavior at the point of creation.
  • Dynamic Behavior: Successfully replaced the generic sound with a unique "Croak-croak!" message.
  • Encapsulation: Demonstrated that specific behavior can be tied to a single instance.

🚀 Architectural Stack

  • Java 8+ (Anonymous Inner Classes, Method Overriding, Polymorphism)

🏗️ Implementation Details

  • MagicalCreature: The base class providing the blueprint.
  • CreatureApp: The entry point where the anonymous class is defined and executed.

📋 Expected result

Croak-croak!

💻 Code Example

Project Structure:

JavaBasics_Task_272/
├── src/
│   └── com/yurii/pavlenko/
│                  ├── MagicalCreature.java
│                  └── CreatureApp.java
└── README.md

Code

package com.yurii.pavlenko;

public class CreatureApp {

    public static void main(String[] args) {

        MagicalCreature uniqueCreature = new MagicalCreature() {
            @Override
            public void makeSound() {
                System.out.println("Croak-croak!");
            }
        };

        uniqueCreature.makeSound();
    }
}
package com.yurii.pavlenko;

public class MagicalCreature {

    public void makeSound() {
        System.out.println("The creature makes a sound.");
    }
}

⚖️ 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_272_V0.1 Magical Farm: implementing anonymous inner classes to override behavior in place without formal class declarations. 230326_1242

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages