Superclass and subclass relationship

superclass and subclass relationship

referred to as the Enhanced Entity Relationship (EER) model. One of the most useful Superclass/subclass relationship is one-to-one (). A sub class has an 'is a' relationship with its superclass. This means that . Super classes are higher data types and sub classes are lower data types. Person p. The class from which the subclass is derived is called a superclass (also a Excepting Object, which has no superclass, every class has one and only one.

Subclasses, Superclasses, and Inheritance

Moreover, a Person object is more likely to be passed as that is what is the one required by the method. But when we deal with overridden methods, the method call result resolution appears unusual. Before we look into it, include the following code in a main method and execute the program: The marks are printed along with the name and age. This contradicts what has been already said.

superclass and subclass relationship

The printDetails method call above has to call the super class version as p is a Person object and Student methods cannot be invoked on p. So, p should also not be having any idea of the overriding method. But during run time, the version in Student was called.

superclass and subclass relationship

This is because of dynamic binding also known as run time binding or run time polymorphism. When a class file is being compiled, the methods that can be called on a particular object referred to by a variable are decided by the data type of the variable.

In the above example, the printDetails method has been invoked on the Student object which is referred to by a Person variable p.

superclass and subclass relationship

Hence, the call would be resolved to the printDetails method defined in the Person class during compile time. This is known as static binding which occurs during compile time. However, the actual version of the method to be called is decided during run time.

  • Subclasses, Superclasses, and Inheritance
  • Java™ How To Program (Early Objects), Tenth Edition by Harvey Deitel, Paul Deitel
  • Inheritance

Excepting Object, which has no superclass, every class has one and only one direct superclass single inheritance. In the absence of any other explicit superclass, every class is implicitly a subclass of Object. Classes can be derived from classes that are derived from classes that are derived from classes, and so on, and ultimately derived from the topmost class, Object. Such a class is said to be descended from all the classes in the inheritance chain stretching back to Object.

The idea of inheritance is simple but powerful: When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write and debug!

Creating a superclass or subclass relationship

A subclass inherits all the members fields, methods, and nested classes from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

In the Java platform, many classes derive directly from Object, other classes derive from some of those classes, and so on, forming a hierarchy of classes. Classes near the bottom of the hierarchy provide more specialized behavior. An Example of Inheritance Here is the sample code for a possible implementation of a Bicycle class that was presented in the Classes and Objects lesson: Except for the constructor, it is as if you had written a new MountainBike class entirely from scratch, with four fields and five methods.

superclass and subclass relationship

However, you didn't have to do all the work. This would be especially valuable if the methods in the Bicycle class were complex and had taken substantial time to debug. What You Can Do in a Subclass A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in.

Creating a superclass or subclass relationship

If the subclass is in the same package as its parent, it also inherits the package-private members of the parent. You can use the inherited members as is, replace them, hide them, or supplement them with new members: The inherited fields can be used directly, just like any other fields. You can declare a field in the subclass with the same name as the one in the superclass, thus hiding it not recommended. You can declare new fields in the subclass that are not in the superclass.

The inherited methods can be used directly as they are.


You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it.