knowt ap exam guide logo

Unit 9 - Inheritance

Inheritance

  • Inheritance: when a subclass shares methods and variables with a superclass

    • The subclass can have its own specific methods and variables

    • Using subclasses prevents repetition

  • Special rules: 

    • A subclass can only inherit from one superclass (called the diamond problem)

    • One superclass can have multiple subclasses

  • Using this, a class hierarchy can be made

Making a subclass

  • To make a subclass: “public class insert subclass name extends insert super class name”

    • Ex: public class X extends Y

      • This would make X a subclass of the superclass Y

    • Inheritance creates an “is-a” relationship between the subclass and superclass

      • Ex: rectangle (subclass) is a quadrilateral (superclass)

Constructors

  • Constructors are not inherited from superclasses

  • To use a constructor from a superclass, you can call the constructor implicitly or explicitly

  • The super keyword (explicit)

    • super is a constructor

    • It lets you use the constructor of the superclass with the parameters of the subclass 

      • Instance variables are initialized

      • This is especially useful when you want to do the same thing the superclass does but with different input variables 

      • This assumes you’re not changing anything about the implementation of the superclass’s method

    • public class firstClass {

      int x = 1;

      }


      class secClass extends firstClass {

      int x = 15;

      void printNum() {

      System.out.println(“x is “ + x);

      System.out.println(“x is also “ + super.x);

      }

      }

  • When you don’t call the superclass’s constructor explicitly, Java calls the constructor implicitly

    • Java calls the superclass’s constructor with no arguments

  • All constructors in the class hierarchy execute beginning with the Object constructor 

    • The Object Class is the superclass of all classes in java.lang (more on this later)

Overriding methods

  • When a public method from the subclass has the same method signature as a public method from the superclass

    • Method signature: the combination of the method’s name and its parameters (order of parameters does matter!!)

  • Subclasses automatically inherit all public methods from the superclass (these methods stay public)

  • @Override is used to indicate that the method directly below it overrides a method from a superclass (@Override is an annotation)

  • public class firstClass {

    public String rand() {

    return “banana”;

    }


    @Override

    public class secClass extends firstClass {

    public String rand() {

    return “apple”;

    }

    }

Using inheritance hierarchies

  • Think of inheritance hierarchies as a waterfall 

    • Change in the top superclass creates changes in all the subclasses beneath it

  • Subclasses can have indirect superclasses

    • Ex: say X is a subclass of Y and Y is a subclass of Z

      • X is a subclass of Y and Z

  • Polymorphism 

    • Polymorphism states that we can assign an object in subclass X to a reference of type Y (if Y is the superclass)

    • In fact, a reference of type Y can refer to an object of type X or Y

The object superclass 

  • The Object class is the superclass of all classes 

  • Part of java.lang

  • We often overwrite or modify the methods from the Object class (like equals() or toString())

LC

Unit 9 - Inheritance

Inheritance

  • Inheritance: when a subclass shares methods and variables with a superclass

    • The subclass can have its own specific methods and variables

    • Using subclasses prevents repetition

  • Special rules: 

    • A subclass can only inherit from one superclass (called the diamond problem)

    • One superclass can have multiple subclasses

  • Using this, a class hierarchy can be made

Making a subclass

  • To make a subclass: “public class insert subclass name extends insert super class name”

    • Ex: public class X extends Y

      • This would make X a subclass of the superclass Y

    • Inheritance creates an “is-a” relationship between the subclass and superclass

      • Ex: rectangle (subclass) is a quadrilateral (superclass)

Constructors

  • Constructors are not inherited from superclasses

  • To use a constructor from a superclass, you can call the constructor implicitly or explicitly

  • The super keyword (explicit)

    • super is a constructor

    • It lets you use the constructor of the superclass with the parameters of the subclass 

      • Instance variables are initialized

      • This is especially useful when you want to do the same thing the superclass does but with different input variables 

      • This assumes you’re not changing anything about the implementation of the superclass’s method

    • public class firstClass {

      int x = 1;

      }


      class secClass extends firstClass {

      int x = 15;

      void printNum() {

      System.out.println(“x is “ + x);

      System.out.println(“x is also “ + super.x);

      }

      }

  • When you don’t call the superclass’s constructor explicitly, Java calls the constructor implicitly

    • Java calls the superclass’s constructor with no arguments

  • All constructors in the class hierarchy execute beginning with the Object constructor 

    • The Object Class is the superclass of all classes in java.lang (more on this later)

Overriding methods

  • When a public method from the subclass has the same method signature as a public method from the superclass

    • Method signature: the combination of the method’s name and its parameters (order of parameters does matter!!)

  • Subclasses automatically inherit all public methods from the superclass (these methods stay public)

  • @Override is used to indicate that the method directly below it overrides a method from a superclass (@Override is an annotation)

  • public class firstClass {

    public String rand() {

    return “banana”;

    }


    @Override

    public class secClass extends firstClass {

    public String rand() {

    return “apple”;

    }

    }

Using inheritance hierarchies

  • Think of inheritance hierarchies as a waterfall 

    • Change in the top superclass creates changes in all the subclasses beneath it

  • Subclasses can have indirect superclasses

    • Ex: say X is a subclass of Y and Y is a subclass of Z

      • X is a subclass of Y and Z

  • Polymorphism 

    • Polymorphism states that we can assign an object in subclass X to a reference of type Y (if Y is the superclass)

    • In fact, a reference of type Y can refer to an object of type X or Y

The object superclass 

  • The Object class is the superclass of all classes 

  • Part of java.lang

  • We often overwrite or modify the methods from the Object class (like equals() or toString())