Sub topic 3
Why Fields Don't Behave Polymorphically
Fields Follow the Declared Type
A field is a variable that lives inside an object. Methods are chosen by the object's real (runtime) type. Fields are not. Java picks a field from the declared type, which is the type written on the variable. Static methods work the same way.
class Parent {
String name = "Parent";
static String kind() { return "Parent.kind"; }
String who() { return "Parent.who"; }
}
class Child extends Parent {
String name = "Child";
static String kind() { return "Child.kind"; }
@Override
String who() { return "Child.who"; }
}
public class Main {
public static void main(String[] args) {
Parent p = new Child();
System.out.println(p.name); // Parent
System.out.println(p.kind()); // Parent.kind
System.out.println(p.who()); // Child.who
}
}Abstract Classes
An abstract class is a class you cannot create with new. It can declare abstract methods. An abstract method has a name but no body, and every concrete subclass must write that body. For example, an abstract Employee can declare abstract double monthlyPay();, and Salaried and Hourly each supply their own version.
Interfaces
An interface is a list of methods that a class promises to provide. Classes that are not related to each other can all implement the same interface. A default method is an interface method that already has a body, and a class may override it. Interface methods are public, so the methods that implement them must be public too.
Casting and instanceof
Downcasting means treating a general reference as a more specific type. If the object is the wrong type, the cast throws ClassCastException. Check the type first with instanceof:
if (a instanceof Dog d) {
System.out.println(d.sound());
}equals, hashCode, and toString
To override equals, its parameter must be Object. If you write equals(Point), you have created an overload instead, and @Override will flag the mistake. Whenever you override equals, also override hashCode, because objects that are equal must have equal hash codes. Overriding toString gives your objects readable output when printed.
Pitfall: Constructors
The superclass constructor runs before the subclass sets up its fields. If that constructor calls a method the subclass overrides, the overriding method runs while the subclass's fields are still null or 0. Do not call overridable methods from constructors.
Sealed Types
A sealed interface lists every type allowed to implement it, for example sealed interface Shape permits Circle, Square {}. Because the full list is known, a switch can handle each type without a default branch. This needs Java 21 or later.
Exercise
In the Main program above, p is declared as Parent but holds a Child. Why does p.name print "Parent" while p.who() prints "Child.who"?
Show answer
Java picks fields from the declared type, which is Parent. It picks instance methods from the runtime type, which is Child. So the field comes from Parent and the method comes from Child.
Exercise
Why should you override hashCode whenever you override equals?
Show answer
Objects that are equal must have equal hash codes. Without a matching hashCode, collections such as HashMap and HashSet behave incorrectly.