In the previous tutorial, we learned inheritance concept in java. Basically, inheritance is a technique in which a new class can be created from the existing class. 

By using inheritance concept, we can acquire all the features (members) of a class and use them in another class by relating the objects of two classes.

The class from where a subclass inherits the features is called superclass. It is also called a base class or parent class. A class that inherits all the members (fields, method, and nested classes) from the other class is called subclass. It is also called a derived class, child class or extended class.

Let's understand the superclass and subclass some more detail in this chapter. 

Superclass and Subclass in Java


If we create an object of the superclass, we can access only the members of superclass. We cannot access the subclass members by creating an object of superclass. Let's create a program where we will create an object of superclass and see that can we access subclass members using superclass reference?

Program source code 1:
    package inheritance; public class Parentclass { void m1(){ System.out.println("Superclass m1 method"); } } public class Childclass extends Parentclass { void m2(){ System.out.println("Childclass m2 method"); } } public class Test { public static void main(String[] args) { // Creating an object of superclass. Parentclass p = new Parentclass(); p.m1(); // Here, subclass members are not available to superclass. So, we cannot access them using superclass reference variable. } }
    Output: Superclass m1 method
As you can see in the above program, the subclass members cannot be accessed by using superclass object reference variable. But if we create an object of subclass, all the members of both superclass and subclass can be accessed. Let's see in the example program.
Program source code 2:
    package inheritance; public class Parentclass { void m1(){ System.out.println("Superclass m1 method"); } } public class Childclass extends Parentclass { void m2(){ System.out.println("Childclass m2 method"); } } public class Test { public static void main(String[] args) { // Creating an object of superclass. Childclass c = new Childclass(); // Accessing superclass and subclass members using subclass object reference variable. c.m1(); c.m2(); } }
    Output: Superclass m1 method Childclass m2 method
Therefore, we always create an object of subclass inheritance.

Suppose that superclass and subclass members have the same name then in this case, by default, only the members of subclass are accessible. Let's write a program where the names of instance variables and methods in superclass and subclass are the same.
Program source code 3:
    package inheritance; public class Number { int x = 20; void display() { System.out.println("X = " +x); } } public class Number2 extends Number { int x = 50; void display() { System.out.println("X = " +x); } } public class NumberTest { public static void main(String[] args) { Number2 n = new Number2(); n.display(); } }
    Output: X = 50