Inheritance & Polymorphism Review

Inheritance in Java

10.1 Overview of Inheritance

  • Inheritance is a fundamental concept in object-oriented programming, allowing a class to inherit properties and behaviors (methods) from another class.

  • A superclass is the general class, while a subclass is the specialized version that inherits from the superclass.

10.2 Key Concepts in Inheritance

  • Generalization vs. Specialization

    • Objects can typically be seen as specialized versions of more general objects.

    • Example:

      • "Insect" is a general term that describes a category, with specific types such as grasshoppers and bumblebees.

      • Grasshoppers “have jumping ability,” while bumblebees “have a stinger.”

  • "Is a" Relationship

    • Establishes a relationship between a superclass and subclass.

    • Examples:

      • A grasshopper is an insect, a poodle is a dog, and a car is a vehicle.

10.3 Class Structure and Members in Inheritance

  • All subclasses inherit non-private members (fields and methods) of the superclass.

  • Private fields in the superclass are not accessible to the subclass directly; they can only be accessed through public methods of the superclass.

  • Example Code:

public class FinalExam extends GradedActivity {
    // Additional fields and methods specific to FinalExam.
}
  • Inheritance Hierarchy:

    • A superclass can inherit from another superclass (chain of inheritance).

10.4 Superclass and Subclass Constructors

  • Constructors are not inherited.

  • The default constructor of the superclass is executed first when creating an instance of a subclass.

  • The super keyword is used to call the superclass's constructor explicitly.

10.5 Method Overriding

  • A subclass can have a method with the same signature as a method in its superclass, thus overriding the superclass method.

@Override
public void setScore(double score) {
    // Specialized behavior
}
  • The @Override annotation helps ensure that a method is indeed overriding a superclass method.

10.6 Access Modifiers in Inheritance

  • Protected Members:

    • Accessible within the same package and by subclasses.

    • Recommendation: Keep fields private and use public methods for access.

10.7 Polymorphism

  • Polymorphism allows a superclass reference to point to subclass objects.

  • Example:

GradedActivity exam = new FinalExam(50, 5);
  • Dynamic binding occurs when the actual method that gets executed is determined at runtime, based on the object's type rather than the reference type.

10.8 Abstract Classes and Methods

  • An abstract class cannot be instantiated and may contain abstract methods (methods without bodies) that must be overridden in subclasses.

  • Example of an abstract method:

public abstract void someMethod();

10.9 Interfaces

  • An interface defines a contract with all abstract methods, providing flexibility in implementation across different classes.

  • Interfaces can be implemented in any class, allowing multiple inheritance of types.

  • From Java 8 onwards, interfaces can include default methods (methods with a body) and starting from Java 9, they can also have private methods.

10.10 Functional Interfaces and Lambda Expressions

  • A functional interface contains only one abstract method, allowing instances to be created using lambda expressions for cleaner, concise code.

  • Example:

IntCalculator square = x -> x * x;
  • This expression behaves like a method that calculates x squared.