Commit 30c9011
committed
feat: Demonstrate access modifiers with inheritance in Animal–Dog example
WHAT the code does:
- Defines Animal class:
- Private field name, accessible only inside Animal.
- Protected field sound, accessible within same package and subclasses.
- Constructor initializes name and sound (bug: parameter `Sound` capitalized but not used; should assign correctly).
- makeSound(): prints name and sound.
- changeSound(): protected method to change sound.
- Defines Dog class extending Animal:
- Constructor calls super(name, "Bark").
- wagTail(): prints class name + wagging message.
- getName(): returns class’s simple name via reflection.
- setDogSound(): wraps the protected changeSound() so external code can safely modify sound.
- Defines Test class:
- Creates Dog instance.
- Calls makeSound() → prints initial "Bark".
- Attempts dog.changeSound("Woof"); (illegal access since Test isn’t subclass of Animal, even though in same package this would compile — but intent is to show protection).
- Calls dog.setDogSound("Woof"); → uses subclass’s public wrapper.
- Calls makeSound() again → prints updated sound.
- Calls wagTail() → accessible since it’s public.
WHY this matters:
- Demonstrates **access modifiers** in Java:
- private: visible only in the class itself.
- protected: visible in same package and subclasses.
- public: visible everywhere.
- Shows **inheritance and encapsulation** working together:
- Subclass Dog can use protected methods of Animal.
- External code (Test) cannot directly access protected members unless in same package context.
- Public wrappers like setDogSound() provide controlled access.
HOW it works:
1. Dog created with name "Bob" and sound "Bark".
2. dog.makeSound() → "null Make a sound: Bark" (bug: Animal constructor didn’t set name correctly).
3. dog.setDogSound("Woof") → updates sound via protected method.
4. dog.makeSound() → prints with new sound.
5. dog.wagTail() → prints "Dogis wagging its tail." (no space because getName() returns "Dog").
Tips and gotchas:
- Bug: Animal constructor’s parameter `Sound` should be lowercase to assign correctly: `this.sound = Sound;`.
- Better naming: `Animal(String name, String sound)` for consistency.
- Avoid naming collision between field name and method name (`getName()` here returns class name, not the private field).
- To expose private `name`, add a getter in Animal.
- Protected access: since Test is in the same package ZOO, it actually *can* call dog.changeSound(), but outside the package it would fail unless Test is subclass.
Use-cases / analogies:
- Encapsulation like this ensures only subclasses or controlled wrappers can change sensitive fields.
- Like a zoo manager (Animal) allowing only zookeepers (Dog subclass) to change animal behaviors, not visitors (Test).
Short key: java access-modifiers inheritance protected-private public encapsulation.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 439eee4 commit 30c9011
1 file changed
+2
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
17 | | - | |
| 16 | + | |
18 | 17 | | |
19 | 18 | | |
20 | | - | |
| 19 | + | |
0 commit comments