Inheritance and Polymorphism ✏ AP Computer Science A

Rucete ✏ AP Computer Science A In a Nutshell

3. Inheritance and Polymorphism

This chapter introduces key concepts in Java inheritance and polymorphism, covering superclasses, subclasses, method overriding, constructors, the super keyword, type compatibility, and dynamic method selection at run time.


Inheritance

• Inheritance allows a new class (subclass) to absorb state and behavior from an existing class (superclass).

• A subclass contains more data and methods than a superclass.

• Inheritance enables code reuse; tested superclass code does not need rewriting.

Inheritance Hierarchy

• Subclasses can also act as superclasses, forming hierarchies (e.g., Person → Student → GradStudent).

• The "is-a" relationship is transitive: a GradStudent is-a Student and a Person.

• Subclasses inherit public and protected methods/variables but not private ones.

Implementing Subclasses with extends

• Subclass declaration uses the extends keyword.

• Private fields of a superclass are accessed via inherited public accessor and mutator methods.

• Sibling subclasses (like UnderGrad and GradStudent) do not inherit from each other.

Method Overriding and the super Keyword

• A subclass can override a public method from the superclass by defining a method with the same signature.

• Partial overriding uses the super keyword to call the superclass version within the overridden method.

• Private methods cannot be overridden.

Constructors and super

• Constructors are not inherited.

• If no constructor is written, a default no-argument constructor is generated.

• If the superclass only has parameterized constructors, the subclass must explicitly call super with appropriate parameters.

• The call to super must be the first statement in a subclass constructor.

Rules for Subclasses

• Can add new fields and methods.

• Can override inherited methods but cannot reduce their visibility (e.g., public to private).

• Cannot override static methods.

• Should define their own constructors to initialize subclass-specific fields.

Declaring Subclass Objects

• A superclass reference can point to a subclass object (e.g., Student s = new GradStudent();).

• Only superclass methods/fields can be accessed directly through a superclass reference unless casting is used.

Polymorphism

• Polymorphism allows one object reference to take many forms.

• A superclass reference variable can refer to any of its subclass objects.

• Behavior depends on the actual object type, not the reference type.

Dynamic Binding (Late Binding)

• Method calls are resolved at run time based on the actual object type, not the reference type.

• Java automatically handles dynamic method selection when polymorphism is used.

• Example: A call to print() on a Student reference that points to a GradStudent object invokes GradStudent’s version of print().

Type Compatibility and Downcasting

• A superclass reference can be safely assigned a subclass object without casting (upcasting).

• To access subclass-specific fields or methods, explicit downcasting is needed.

• Downcasting must be done carefully to avoid ClassCastException at run time.

• instanceof operator checks if an object belongs to a specific class before casting.

Abstract Classes and Methods

• An abstract class cannot be instantiated; it may contain abstract methods (without implementation).

• Subclasses must implement all abstract methods unless they are also abstract.

• Abstract methods force subclasses to provide specific behaviors.

Benefits of Inheritance and Polymorphism

• Code reuse: Common code is written once in the superclass.

• Extensibility: New classes can be easily created with minimal changes.

• Flexibility: Programs can be written more generally using superclass references, making code easier to maintain and extend.

Best Practices

• Favor composition over inheritance when appropriate to avoid deep, rigid class hierarchies.

• Use inheritance when "is-a" relationships are clear and logical.

• Override methods carefully, maintaining contract behavior expected from the superclass.

In a Nutshell

Inheritance and polymorphism enable flexible, reusable, and extensible object-oriented programming. Through inheritance, subclasses expand on or customize the behavior of superclasses, while polymorphism allows a single reference to operate on different types at run time. Understanding method overriding, constructor chaining, dynamic binding, and safe downcasting is critical to writing powerful, modular Java programs.

Post a Comment

Previous Post Next Post