Polymorphism appears when inheritance or interface implementation is involved. It has two visible sides: object polymorphism and behavior polymorphism. An object can be referenced through a parent type, while the method that actually runs can be the subclass's overridden version.

Three conditions must be present: an inheritance or implementation relationship; a parent reference that points to a child object; and method overriding. One boundary is easy to miss: polymorphism applies to objects and behavior, not to member variables.

Using polymorphism has practical benefits. In a polymorphic assignment, the concrete object on the right side is decoupled from the parent type used on the left, which makes extension and maintenance easier. When defining a method, a parameter declared with the parent type can receive any subclass object, so the method gains wider extensibility and convenience.

The trade-off is that a parent reference cannot directly use features that exist only in the subclass. Type conversion is the way around this:

  • Automatic conversion: 父类 变量名 = new 子类();
  • Forced conversion: 子类 变量名 = (子类)父类变量

A forced cast is accepted at compile time as long as an inheritance or implementation relationship exists, so no compile-time error appears at that point. At runtime, however, if the object's actual type differs from the cast target, Java throws a ClassCastException. Before casting, Java recommends using instanceof to confirm the object's real type, then performing the forced conversion.

final means final or ultimate in Java. It can modify classes, methods, and variables:

  • A final class cannot be inherited.
  • A final method cannot be overridden.
  • A final variable can be assigned only once.

A member variable declared with static final is a constant. Constants are commonly used to record system configuration information. Their naming convention is uppercase English words, with underscores between multiple words. Using constants for configuration improves code readability and maintainability. After compilation, constants are macro-replaced: every place that uses the constant is replaced with the literal value it stores. This keeps performance the same as using the literal directly.

An abstract class is declared with the abstract keyword, which can modify classes and member methods. An abstract class does not necessarily contain abstract methods, but a class that contains abstract methods must be abstract. Abstract classes can have all the members a normal class can have, including member variables, methods, and constructors. Their key characteristic is that they cannot be instantiated. They serve as a special kind of parent class, allowing subclasses to inherit from them and implement the abstract behavior. If a class inherits from an abstract class, it must override all abstract methods; otherwise, that subclass must also be declared abstract.

The typical use case is straightforward: the parent class knows that every subclass must perform some behavior, but each subclass performs it differently. The parent class defines an abstract method and leaves the implementation to the subclasses. Designing an abstract class this way is meant to support polymorphism more effectively.

An interface is defined with the interface keyword, which creates a special structure in Java. An interface cannot be instantiated. It is implemented by classes using implements, and the class that implements it is called an implementation class. A class can implement multiple interfaces. When it does, the implementation class must override all abstract methods from all the interfaces; otherwise, it must be defined as an abstract class.