Chapter 10: Inheritance

Inheritance

  • Overview

    • Inheritance allows for the creation of specialized subclasses based on a more general superclass.

    • Inheritance provides shared attributes and methods among subclasses.

The “is a” Relationship

  • The relationship between a superclass and an inherited class is known as the “is a” relationship.

    • Examples:

    • A grasshopper is a insect.

    • A poodle is a dog.

    • A car is a vehicle.

  • Characteristics of Specialized Objects:

    • Contains all characteristics of the general object plus additional unique attributes.

  • In object-oriented programming, the use of inheritance creates an “is a” relationship between classes.

Superclass and Subclass

  • Superclasses and subclasses:

    • The superclass is the general class and the subclass is the specialized class.

    • Subclasses are based on or extended from superclasses.

    • Alternate terms:

    • Superclass: base class

    • Subclass: derived class

  • Class relationships function as parent (superclass) and child (subclass) classes.

Inheritance Mechanics

  • Subclass inherits fields and methods from the superclass, all without needing to rewrite them.

  • New fields and methods can be added to the subclass.

  • The Java keyword extends is used in the class header to define the subclass:

    • Example:
      java public class FinalExam extends GradedActivity

Inheritance, Fields, and Methods

  • Access Specifiers and Inheritance:

    • Private members in a superclass:

    • Not inherited by the subclass.

    • Exist in memory when the subclass object is created.

    • Accessible only through public methods of the superclass.

    • Public members in a superclass:

    • Inherited by the subclass.

    • Directly accessible from the subclass.

  • Subclass Object Creation:

    • When a subclass instance is created:
      java FinalExam exam = new FinalExam(); exam.setScore(85.0); System.out.println("Score = " + exam.getScore());

    • Non-private methods of the superclass are available through the subclass object.

Inheritance and Constructors

  • Constructors in Inheritance:

    • Constructors are not inherited.

    • When instantiating a subclass, the superclass default constructor executes first.

    • Example files:

    • SuperClass1.java

    • SubClass1.java

    • ConstructorDemo1.java

Calling the Superclass Constructor

  • Using the super keyword:

    • The keyword super refers to an object’s superclass.

    • It allows explicit calling of the superclass constructor from the subclass using the syntax:
      java super();

  • If a parameterized constructor is defined in the superclass:

    • The superclass must provide a no-argument constructor or subclasses must provide and call a superclass constructor.

  • Calls to a superclass constructor must be the first statement in the subclass constructors.

Overriding Superclass Methods

  • A subclass can define a method with the same signature as a superclass method, thus overriding it.

  • Method Overriding:

    • Example:
      java public class GradedActivity { // methods and fields } public class CurvedActivity extends GradedActivity { // methods and fields }

  • The method signature consists of:

    • The method's name

    • The data types of the method's parameters in the order they appear.

  • The subclass method that overrides a superclass method must have the same signature.

  • @Override Annotation:

    • This should be used before the subclass method declaration to check for correct overriding at compile time.

Calling the Overridden Method in Subclass

  • A subclass method can invoke the overridden superclass method using super keyword:

    • Example:
      java super.setScore(rawScore * percentage);

  • Overloading vs Overriding:

    • Overloading: Same method name with different signatures.

    • Overriding: Same method name and same signature.

Preventing Method Overriding

  • The final modifier can be used to prevent a superclass method from being overridden in subclasses:

  public final void message() {
  // method code
  }
  • If a subclass attempts to override a final method, the compiler generates an error.

Protected Members

  • Protected Members in a class can be accessed by methods in the subclass and by methods within the same package.

  • The protected access modifier provides a level of access that is between private and public.

  • Example:

    • Class files like GradedActivity2.java, FinalExam2.java, and ProtectedDemo.java illustrate this concept.

Chains of Inheritance

  • A superclass can also be derived from another class, forming chains of inheritance.

    • Example:

    • GradedActivity.java

    • PassFailActivity.java

    • PassFailExam.java

    • PassFailExamDemo.java

The Object Class

  • All Java classes are derived from the Object class, either directly or indirectly.

  • Object Class:

    • Located in the java.lang package.

    • Any class that doesn't specify an extends keyword automatically inherits from Object.

    • Example:
      java public class MyClass { // This class is derived from Object. }

    • Every class implicitly inherits members from the Object class, including toString() and equals() methods.

Polymorphism

  • A reference variable can reference objects of classes that are derived from the variable's class.

  • Example:

  GradedActivity exam;
  exam = new GradedActivity();
  • Polymorphism allows a class reference to point to objects of its subclass.

  • A GradedActivity variable can also reference a FinalExam object:

  GradedActivity exam = new FinalExam(50, 7);
  • Dynamic Binding:

    • The Java Virtual Machine determines at runtime which method to call, depending on the actual object type referenced by the variable.

  • In Java, the object type (not the reference type) determines which method is executed.

Abstract Classes

  • An abstract class cannot be instantiated. Instead, it serves as a superclass for other classes.

  • Abstract classes represent a generic or abstract form of derived classes.

  • To declare a class as abstract, the keyword abstract is used in the class definition:

  public abstract class ClassName {
  //Abstract methods and properties
  }

Abstract Methods

  • An abstract method must be overridden in a subclass, and it does not contain a body.

  • Syntax for an abstract method:

  AccessSpecifier abstract ReturnType MethodName(ParameterList);
  • Abstract methods ensure that subclasses implement the methods.

  • Example classes such as Student.java, CompSciStudent.java demonstrate this concept.

Final Classes and Methods

  • Final Class:

    • A final class cannot be inherited from:
      java public final class YourClass { // member declarations }

    • Compiler will error if another class attempts to inherit from a final class.

  • Final Method:

    • A final method cannot be overridden in subclasses:
      java public final void message() { // method code }

    • Ensures a particular version of a method is used across subclasses.