Subclasses & Extends, Pt. 2 (Extending)

Overview of Subclassing and Behavior Extension

  • In object-oriented programming, subclasses allow the extension of behaviors and fields from a superclass.

  • Focusing on behavior allows for the addition of specific methods relevant to the subclass.

Example: Private Plane Behavior

  • When defining a subclass such as PrivatePlane, new unique methods can be added:

    • Example method: bringWarmTowels.

  • This method is specific to PrivatePlane and not found in the general Airplane class.

Instantiation of a Private Plane

  • Declaring a private plane object:

    • Syntax: PrivatePlane pp = new PrivatePlane();

  • Calling the specific method:

    • pp.bringWarmTowels(); executes successfully, showcasing inheritance of behavior.

Type Declaration and Access

  • Declaring variables as the superclass restricts access to subclass-specific methods:

    • Example: Airplane luxPlane = new PrivatePlane(); does not allow calling luxPlane.bringWarmTowels();

  • The declared type (Airplane) must have the method implemented, which it does not.

  • Attempting to call non-existent methods results in errors (e.g., red squiggle in IntelliJ).

Creating the PrivatePlane Class in IntelliJ

  • Class creation involves extending the superclass:

    • Syntax: class PrivatePlane extends Airplane.

  • An empty PrivatePlane class can still function, leveraging inherited methods from Airplane.

  • Inherited methods from Airplane can be called without needing implementations in PrivatePlane itself.

    • Example: Calling pp.fly(); works even when PrivatePlane is empty.

Implementation of New Method in PrivatePlane

  • Adding specific methods to PrivatePlane enhances its functionality:

    • Implementation of bringWarmTowels:

      public void bringWarmTowels() {
          System.out.println("Here is your warm towel.");
      }
  • Calling this method:

    • After implementation, pp.bringWarmTowels(); shows the method as bold in IntelliJ, indicating it exists.

  • Output confirmed through console:

    • "Here is your warm towel."

Luxurious Takeoff Method

  • Creating specialized methods that require specific types:

    • Example method: luxuryTakeoff that takes a PrivatePlane as a parameter.

    • Uses the inherited takeoff method along with bringWarmTowels.

  • Method signature:

    • public void luxuryTakeoff(PrivatePlane plane).

  • Invoking the method:

    • luxuryTakeoff(pp); works due to the matching type.

Limitations on Parameter Passing

  • Attempting to pass a superclass type to methods expecting subclass types fails:

    • Example: Trying to pass Airplane plany to luxuryTakeoff will not work.

  • Reasons for limitation:

    • Airplane lacks access to bringWarmTowels, differentiating it from PrivatePlane despite other shared behaviors.

  • Summary of substitution principle:

    • Superclass instances cannot take the place of subclass instances, maintaining the integrity of specialized behaviors.