Commit 175069f
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 30c9011 commit 175069f
1 file changed
+6
-8
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
| 3 | + | |
| 4 | + | |
7 | 5 | | |
8 | 6 | | |
9 | 7 | | |
10 | | - | |
| 8 | + | |
11 | 9 | | |
12 | 10 | | |
13 | 11 | | |
14 | | - | |
| 12 | + | |
15 | 13 | | |
16 | 14 | | |
17 | 15 | | |
18 | 16 | | |
19 | | - | |
| 17 | + | |
20 | 18 | | |
21 | 19 | | |
22 | | - | |
| 20 | + | |
0 commit comments