Sub topic 7
Access in Inheritance
Inheritance lets one Java class build on another. The existing class is the superclass. The class that builds on it is the subclass. A subclass can use certain fields and methods from its superclass, as well as define its own.
A member is something declared in a class, such as a field or method. An access modifier controls where code can use a member. Inheritance does not let a subclass use every member directly.
A subclass and its superclass
The keyword extends makes a class a subclass. In the example below, Dog extends Animal.
Save this code in a file named Main.java:
class Animal {
private String name;
Animal(String name) {
this.name = name;
}
public String getName() {
return name;
}
protected void rest() {
System.out.println(name + " is resting.");
}
void eat() {
System.out.println(name + " is eating.");
}
}
class Dog extends Animal {
Dog(String name) {
super(name);
}
public void describe() {
System.out.println(getName() + " is a dog.");
rest();
eat();
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog("Milo");
dog.describe();
System.out.println(dog.getName());
}
}Compile and run it:
javac Main.java
java MainThe output is:
Milo is a dog.
Milo is resting.
Milo is eating.
MiloThe Dog class does not declare getName, rest, or eat. It inherits those methods from Animal, so its describe method can call them here. Inheriting a method means the subclass gets that method from its superclass.
The main method can also call dog.getName() because getName is public.
Why the subclass cannot use `name` directly
The name field is private. A private member can be used directly only by code in the class that declares it. Code in Dog cannot read name directly, even though Dog extends Animal.
A Dog object still has the Animal state, including name. The inherited getName method can return that value because the method is declared inside Animal, where access to name is allowed.
For example, adding System.out.println(name); inside Dog.describe would cause a compile error. Writing super.name would not solve the problem: super does not remove the private access rule.
Access in the same package and across packages
A package is a named group of Java classes. All the classes in the example are in the same package. The access modifier on a superclass member affects whether a subclass can use it.
| Superclass member access | Subclass in the same package | Subclass in a different package |
|---|---|---|
| `public` | Yes | Yes |
| `protected` | Yes | Yes, with extra rules about how it is accessed |
| No modifier, called package-private | Yes | No |
| `private` | No direct access | No direct access |
In the example, rest is protected, and eat is package-private. Both calls work because Dog and Animal are in the same package. Other classes in that package can also use those members where the member and its class are accessible.
If Dog were in a different package, it could not use the package-private eat method. A protected member can still be available to a subclass in another package, but cross-package protected access has additional rules.
Constructors and `super`
A constructor sets up a new object. A subclass does not inherit its superclass constructors. Instead, its constructor calls a superclass constructor to set up the superclass part of the object.
In Dog, this line calls the Animal constructor:
super(name);That call passes "Milo" to Animal, which stores it in the private field. The call to super must be the first statement in the Dog constructor.
Check your understanding
Exercise
If Dog tries to read name directly in describe, will it compile? How can Dog get the name instead?
Show answer
No. name is private to Animal. Dog can call the inherited public getName() method.
Exercise
If Dog moves to a different package from Animal, can Dog still call the package-private eat method?
Show answer
No. Package-private access is limited to code in the same package.