Advertisement

header ads

Java OOP Inheritance - A simple tutorial



Inheritance is one of the major OOP concept in Java. Inheritance simply means the parent - child relationship between two classes in java. That means child class inherits properties and/or behaviors from its parent class. This java inheritance tutorial provides an introduction to inheritance with some simple examples.
We use 2 specific keywords in inheritance,

  • Special keywords used in inheritance: extends, super


Check the following example to get a clear idea about inheritance.

I am using eclipse IDE in Ubuntu.

This is my project hierarchy.

This is my Parent Class (Super Class)
1
2
3
4
5
6
7
package com.example.inheritance;

public class ParentClass {

 int parentClassValue = 1000;

}

This is my Child Class (Sub Class)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.example.inheritance;

public class ChildClass extends ParentClass {

 int childClassValue = 500;

 public static void main(String[] args) {

  ChildClass childClass = new ChildClass();
  childClass.showRelationship();

 }

 private void showRelationship() {

  ChildClass childClassObj = new ChildClass();

//  System.out.println("This is parent value: " + childClassObj.parentClassValue);
  System.out.println("This is parent value: " + super.parentClassValue);

  System.out.println("This is child value: " + childClassObj.childClassValue);
 }

}

You can clearly see that I have used 'extends' and 'super' keywords in line 3 and 19 in ChildClass.java. You can also use line 18 instead of line 19 too. Its acquiring the parentValue variable directly since Childclass has been extended to ParentClass.

NOTE: I have used seperate 'showRelationship' method here to use 'super' keyword because we cannot use 'super' in smain method as it is an static method. If you are using line 18 approach, you can write 16,18,21 lines inside the main method and remove 9,10 lines and 'showRelationship' method.

Invoking Super class constructor (another characteristic of inheritance)
Sub class is also inherits super class's default constructor. Following example demonstrates acquiring default constructor of the super class.

What is a constructor?
A constructor is similar to a method which calls when instance of an object is created.  There are some unique characteristics.
  • Constructor don't have a return type
  • Name should be same as name of the class
  • Not a member of class
Example for invoking super class constructor.

Parent Class (Super Class)
1
2
3
4
5
6
7
8
9
package com.example.inheritance;

public class ParentClass {

 public ParentClass(int parentClassValue) {
  System.out.println("This is parent class value:" + parentClassValue);
 }

}

In above class, you can see that I have created a constructor . In eclipse you can use Alt+Shift+S keys to open the options menu to create a constructor.

Child Class (Sub Class)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
package com.example.inheritance;

public class ChildClass extends ParentClass {

 public ChildClass(int parentClassValue) {
  super(parentClassValue);
  // TODO Auto-generated constructor stub
 }

 int childClassValue = 500;

 public static void main(String[] args) {

  ChildClass childClass = new ChildClass(1000);

 }

}


You can see that constructor of the child class has been invoked the super class constructor.

There are several types of inheritance in java.

Single inheritance is same as the first example.
Multi level inheritance can be demonstrate as follows

MultilevelInheritance.java class
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.example.inheritance;

class ClassA {

 public void ClassAMethod() {
  System.out.println("This is parent class");
 }
}

class ClassB extends ClassA {
 public void ClassBMethod() {
  System.out.println("This is first child class");
 }
}

class ClassC extends ClassB {
 public void ClassCMethod() {
  System.out.println("This is second child class");
 }
}

public class MultiLevelInheritance {
 public static void main(String[] args) {
  ClassC classC = new ClassC();
  classC.ClassAMethod();
  classC.ClassBMethod();
  classC.ClassCMethod();
 }
}

Output:

This is parent class
This is first child class
This is second child class

Hierarchical Inheritance can be demonstrate as follows.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.example.inheritance;

class ClassA {

 public void ClassAMethod() {
  System.out.println("This is parent class");
 }
}

class ClassB extends ClassA {
 public void ClassBMethod() {
  System.out.println("This is first child class");
 }
}

class ClassC extends ClassA {
 public void ClassCMethod() {
  System.out.println("This is second child class");
 }
}

public class MultiLevelInheritance {
 public static void main(String[] args) {
  ClassC classC = new ClassC();
  classC.ClassAMethod();
  classC.ClassCMethod();
  
  ClassB classB = new ClassB();
  classB.ClassBMethod();
  classB.ClassAMethod();
 }
}

Output:

This is parent class
This is second child class
This is first child class
This is parent class

You can get a better idea about above inheritance types by comparing the code blocks with the diagram. Apart from above types, there are two more inheritance types as

  • Multiple inheritance
  • Hybrid Inheritance

but due to reduce the code complexity, these are only available in interface level. Also cannot do  inheritance like (i.e. ClassA extends ClassB extends ClassC) in java.

Hope you have got a better understanding about inheritance in Java using this tutorial. Share this among friends and like our facebook page. Meet you again with another OOP tutorial.



Post a Comment

0 Comments