Object-Oriented Programming: Inheritance

2023-03-29

Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends.

Generalization and Specialization

There are many objects that are specialized versions of other more general objects. For example, the term mammal describes a very general type of creature with numerous characteristics. Because cat and dog, they have all the general characteristic of a mammal. In addition, they have special characteristics of their own. For example, the cat has its meowing ability, and the dog has its woof. Cats and dogs are specialized versions of a mammal.

Inheritance and the “Is-a” relationship

When one object is a specialized version of another object, there is an “is a” relationship between them. For example, a cat is a mammal. Here are a few other examples:

  • a poodle is a dog
  • a car is a vehicle
  • a rectangle is a shape
  • a flower is a plant

When an “is-a” relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus any additional characteristics that make it special. Inheritance is used to create “is-a” relationships among classes in object-oriented programming. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it. Inheritance involves a superclass and a subclass. The superclass is the general class and the subclass is the specialized class. In a way, the subclass is an extension of the superclass. The subclass inherits fields and methods from the superclass without any of them having to be rewritten. Furthermore, new fields and methods may be added to the subclass, and that is what makes it a specialized version of the superclass. Superclasses are also called base classes and subclasses are also called derived classes.

Polymorphism

A superclass reference variable can reference objects of a subclass.

This is because a subclass object is also a superclass object. Polymorphism is the ability to take many forms. In Java, a reference variable is polymorphic because it can reference objects of types different from its own, as long as those types are subclasses of its type. Although there is a limit to what the variable can do with those objects even though they are inherited from the superclass. The reference variable will be limited to only methods defined in the superclass, and will not be able to execute methods within the subclass. This is because the reference variable is declared as a superclass type and only is assigned the address of the subclass object.

Polymorphism and dynamic binding

A potential problem exists with polymorphism. What if the subclass has overridden a method in the superclass, and the variable makes a call to that method? Which method does the variable call? The superclass’s version or the subclass’s version? The process of matching a method call with the correct method definition is known as binding. Java performs dynamic binding or late binding when a variable contains a polymorphic reference. Meaning that the Java Virtual Machine determines at runtime which method to call, depending on the type of object that the variable references. You can also use parameters to accept arguments to methods polymorphically.

public static void displayGrades(GradedActivity g) { System.out.println("Score " + g.getScore() + ", grade " + g.getGrade()); GradedActivity exam1 = new FinalExam(50, 7); GradedActivity exam2 = new PassFailActivity(70); GradedActivity exam3 = new PassFailExam(100, 10, 70); displayGrades(exam1); // Pass a FinalExam object displayGrades(exam2); // Pass a PassFailActivity object displayGrades(exam3); // Pass a PassFailExam object } 

Abstract classes and abstract methods

An abstract class is not instantiated, but other classes extend it. An abstract method has no body and must be overridden in a subclass. An error will occur if a subclass fails to override the method.

Abstract methods appear in a superclass, but expects to be overridden in a subclass. An abstract method only has a header and no body. Abstract methods are used to ensure that a subclass implements the method.