Skip to content

Commit 175069f

Browse files
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

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

Section17Packages/src/ZOO/Dog.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
package ZOO;
22

3-
public class Dog extends Animal
4-
{
5-
public Dog(String name)
6-
{
3+
public class Dog extends Animal {
4+
public Dog(String name) {
75
super(name, "Bark");
86
}
97

10-
public void wagTail(){
8+
public void wagTail() {
119
System.out.println(getName()+"is wagging its tail.");
1210
}
1311

14-
public String getName(){
12+
public String getName() {
1513
return getClass().getSimpleName();
1614
//Class ka name provide karta hai getSimpleName
1715
}
1816

19-
public void setDogSound(String newSound){
17+
public void setDogSound(String newSound) {
2018
changeSound(newSound);
2119
}
22-
}
20+
}

0 commit comments

Comments
 (0)