Inheritance

Inheritance

Published by: sadikshya

Published date: 14 Jun 2021

Inheritance photo

Inheritance and Member Access

Inheritance simply defined as the behavior and character that the subclass or the child class gets itself by default from the parent class which makes the software reusability.
Using inheritance, you can create a general class that defines traits common to a set of related items. This class can then be inherited by other, more specific classes, each adding those things that are unique to it. In the terminology of Java, a class that is inherited is called a superclass. The class that does the inheriting is called a subclass. Therefore, a subclass is a specialized version of a superclass. It inherits all of the instance variables and methods defined by the superclass and adds its own, unique elements.

Inheritance Basics

To inherit a class, you simply incorporate the definition of one class into another by using the extends keyword. To see how to let’s begin with a short example. The following program creates a superclass called A and a subclass called B.
Notice how the keyword extends is used to create a subclass of A.

// A simple example of inheritance.
// Create a superclass.
Public class A {
int i, j;
void showij() {
System.out.println(“i and j: ” + i + ” ” + j);
}
}
// Create a subclass by extending class A.
class B extends A {
int k;
void showk() {
System.out.println(“k: ” + k);
}
void sum() {
System.out.println(“i+j+k: ” + (i+j+k));
}
}

Example of the simple inheritance.
Public class SimpleInheritance {
public static void main(String args[]) {
A superOb = new A();
B subOb = new B();
// The superclass may be used by itself.
superOb.i = 10;
superOb.j = 20;
System.out.println(“Contents of superOb: “);
superOb.showij();
System.out.println();
/* The subclass has access to all public members of
its superclass. */
subOb.i = 7;
subOb.j = 8;
subOb.k = 9;
System.out.println(“Contents of subOb: “);
subOb.showij();
subOb.showk();
System.out.println();
System.out.println(“Sum of i, j and k in subOb:”);
subOb.sum();
}
}

The output from this program is shown here:
Contents of superOb:
i and j: 10 20
Contents of subOb:
i and j: 7 8
k: 9
Sum of i, j and k in subOb:
i+j+k: 24

As you can see, the subclass B includes all of the members of its superclass, A. This is why subOb can access i and j and call showij( ). Also, inside sum( ), i and j can be referred to directly, as if they were part of B. Even though A is a superclass for B, it is also a completely independent, stand-alone class. Being a superclass for a subclass does not mean that the superclass cannot be used by itself. Further, a subclass can be a superclass for another subclass.

Member Access and Inheritance

Although a subclass includes all of the members of its superclass, it cannot access those members of the superclass that have been declared as private. For example, consider the following simple class hierarchy:
/* In a class hierarchy, private members remain private to their class. This program contains an error and will not compile. */
// Create a superclass.
class A {
int i; // public by default
private int j; // private to A
void setij(int x, int y) {
i = x;
j = y;
}
}
// A’s j is not accessible here.
class B extends A {
int total;
void sum() {
total = i + j; // ERROR, j is not accessible here
}
}
class Access {
public static void main(String args[]) {
B subOb = new B();
subOb.setij(10, 12);
subOb.sum();
System.out.println(“Total is ” + subOb.total);
}
}
This program will not compile because the reference to j inside the sum( ) method of B causes an access violation. Since j is declared as private, it is only accessible by other members of its own class. Subclasses have no access to it.