Sub topic 3
Apply Access Rules Across Packages
A package is a group of Java classes. Access modifiers control where a class member, such as a field or method, can be used.
| Modifier | Where the member is accessible |
|---|---|
| `public` | Anywhere the class is accessible |
| `protected` | In the same package and, with restrictions, in subclasses outside it |
| No modifier | Only in the same package |
| `private` | Only inside the declaring class |
A subclass is a class that extends another class. In a different package, it can use a protected member on itself, but not on just any superclass object.
Check Override Rules with @Override
Overriding means giving an inherited instance method a new implementation. An instance method belongs to an object. The @Override annotation tells the compiler to check that the method really overrides another method.
Create directories named creatures and pets. Save these three files in the locations shown.
creatures/Animal.java:
package creatures;
public class Animal {
private final String name;
public Animal(String name) {
this.name = name;
}
protected String description() {
return name + " makes a sound";
}
public String speak() {
return description();
}
}pets/Dog.java:
package pets;
import creatures.Animal;
public class Dog extends Animal {
public Dog(String name) {
super(name);
}
@Override
public String speak() {
return super.description() + " and barks";
}
}Main.java:
import creatures.Animal;
import pets.Dog;
public class Main {
public static void main(String[] args) {
Animal animal = new Dog("Rex");
System.out.println(animal.speak());
}
}From the directory containing Main.java, run:
javac Main.java creatures/Animal.java pets/Dog.java
java MainThe output is Rex makes a sound and barks. Java calls Dog.speak() because the object is a Dog. The subclass can call the protected description() across packages, but it cannot access the private name field directly.
An override must have the same parameters and the same return type, or a more specific reference type. It cannot reduce access: a public method must stay public.
Distinguish Overloading and Hiding
Overloading means using the same method name with different parameters, such as speak(String mood). It is not overriding.
A static method belongs to a class rather than an object. A subclass can hide a superclass static method with the same signature. The class named by the reference determines which static method runs; the object's class does not.
Control Inheritance with abstract and final
An abstract class cannot be created directly. An abstract method has no body, and a non-abstract subclass must implement it. A final class cannot be extended. A final method cannot be overridden.
Extend Classes and Implement Interfaces
An interface is a contract a class can implement. A class can extend one class but implement multiple interfaces.
Choose Inheritance or Composition
Use inheritance when a subclass truly is a kind of its superclass. Use composition when an object should contain and use another object instead. Composition makes it easier to change that helper without changing the class hierarchy.
Exercise
Can Dog change its overridden speak() method from public to protected?
Show answer
No. An override cannot reduce access, and Animal.speak() is public.