Software Testing Material

A site for software testers. We provide free online tutorials on Manual Testing, Automation Testing - Selenium, QTP, LoadRunner, Testing Tools and many more.

  • Blog
  • Tutorials
    • Manual Testing
    • Java
    • Selenium
      • TestNG
      • Maven
      • Jenkins
    • Framework
    • Katalon
    • Agile
    • SQL
    • VBScript
    • API Testing
  • Tools
    • TestLodge
    • FrogLogic GUI Tool
    • CrossBrowserTesting
    • BrowserStack
    • TestCaseLab
    • Kobiton
    • PractiTest
    • Sikuli
    • Postman
  • Interview Q & A
    • Selenium
    • TestNG
    • Test Framework
    • Explain Framework
    • Manual Testing
    • Software QA
    • Agile
    • Testing As A Career
    • General Interview Questions
    • API Testing
    • SOAP
    • JIRA
    • Protractor
  • Free Resources
  • Guest Post
    • Guest Post Guidelines
  • Training

Inheritance In Java

Last Updated on March 16, 2018 by Rajkumar

Inheritance is a process where one class inherits the properties of another class.

Inheritance In Java

Let’s say we have two classes namely Parent Class and Child Class. Child class is also known as Derived Class. As per the above definition, the Child class inherits the properties of the Parent Class. The main purpose of Inheritance is to obtain Code Reusability. We can achieve run time polymorphism with inheritance. We will see what is run time polymorphism in few minutes.

Learn what is a Class and Object by using this link

Assume we have a Class named Laptop, Apple MacBook Pro, Lenovo Yoga. Apple MacBook Pro and Lenovo Yoga classes extend the Laptop Class to inherit the properties of the Laptop Class.

We will see detailed explanation with some example programs about Inheritance in the post related to Inheritance.

In inheritance, we use two keywords namely extends and implements

Extends:

We use extends keyword in Java to allow the child class to inherit all the properties (data members and methods) of the parent class and in addition to these, we can also create new data members and methods. If the properties are private then the child class cannot inherit those properties from the parent class.

1
2
3
class ChildClass extends ParentClass
{
}

Technically, we say that child class has IS-A relationship with the parent class

1
2
3
class QA extends Employee
{
}

As per the IS-A relationship, we can say QA IS-A Employee

Employee Class:

1
2
3
4
5
6
7
package classFourOops.inheritance;
 
public class Employee {
 
String name = "Rajkumar";
}

QA Class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package classFourOops.inheritance;
 
public class QA extends Employee{
String fullName = "Rajkumar SM";
public static void main(String [] args){
QA objName = new QA();
System.out.println(objName.name);
System.out.println(objName.fullName);
}
 
}

In the above example, QA object can access the properties of its own as well as Employee class.

Implements:

We use implements keyword in Java to inherit the properties from an interface. Interfaces cannot be extended by the classes.

We will see this in Interface section.

Types of Inheritance:

1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance

There are two more inheritances such as Multiple and Hybrid Inheritance which are supported through interface only. We will see those in the Interface section.

Single Inheritance:

Single Inheritance in Java refers to a child and parent class relationship. In this one class extends another class say Class B extends Class A

Single Inheritance

1
2
3
4
5
6
7
8
9
package classFourOops.singleInheriance;
 
public class ClassA {
 
void methodOneClassA(){
System.out.println("I am a Method One of ClassA");
}  
}

 

1
2
3
4
5
6
7
8
package classFourOops.singleInheriance;
 
public class ClassB extends ClassA{
void methodOneClassB(){
System.out.println("I am a Method One of ClassB");
}  
}

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package classFourOops.singleInheriance;
 
public class InheritanceTest {
public static void main(String args[]){
// Class B extends Class A
// Here I am creating an instance of ClassB
ClassB obj = new ClassB();
// Using object of ClassB, I can call methods of ClassA and ClassB.. Its just because ClassB extends ClassA
obj.methodOneClassA();
obj.methodOneClassB();
}
}

Output:

1
2
I am a Method One of ClassA
I am a Method One of ClassB

Multilevel Inheritance:

Multilevel Inheritance in Java refers to a child and parent class relationship. In this a class extends a child class say Class C extends Class B and Class B extends Class A

Multilevel Inheritance

 

1
2
3
4
5
6
7
8
9
package classFourOops.multipleInheriance;
 
public class ClassA {
 
void methodOneClassA(){
System.out.println("I am a Method One of ClassA");
}  
}

 

1
2
3
4
5
6
7
8
package classFourOops.multipleInheriance;
 
public class ClassB extends ClassA{
void methodOneClassB(){
System.out.println("I am a Method One of ClassB");
}  
}

 

1
2
3
4
5
6
7
8
package classFourOops.multipleInheriance;
 
public class ClassC extends ClassB{
void methodOneClassC(){
System.out.println("I am a Method One of ClassC");
}  
}

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package classFourOops.multipleInheriance;
 
public class InheritanceTest {
public static void main(String args[]){  
// Class B extends Class A
// Class C extends Class B
// Here I am creating an instance of ClassC
ClassC obj = new ClassC();
// Using object of ClassC, I can call methods of ClassA, ClassB and ClassC..
// Its just because ClassB extends ClassA and ClassC extends ClassB
obj.methodOneClassA();
obj.methodOneClassB();
obj.methodOneClassC();
}
}

Output:

1
2
3
I am a Method One of ClassA
I am a Method One of ClassB
I am a Method One of ClassC

Hierarchical Inheritance:

Hierarchical Inheritance in Java refers to a child and parent class relationship. In this more than one class extends the same class say Class B, Class C extends Class A

Hierarchical Inheritance

 

1
2
3
4
5
6
7
8
9
package classFourOops.hierarchialInheriance;
 
public class ClassA {
 
void methodOneClassA(){
System.out.println("I am a Method One of ClassA");
}  
}

 

1
2
3
4
5
6
7
8
package classFourOops.hierarchialInheriance;
 
public class ClassB extends ClassA{
void methodOneClassB(){
System.out.println("I am a Method One of ClassB");
}  
}

 

1
2
3
4
5
6
7
8
package classFourOops.hierarchialInheriance;
 
public class ClassC extends ClassA{
void methodOneClassC(){
System.out.println("I am a Method One of ClassC");
}  
}

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package classFourOops.hierarchialInheriance;
 
public class InheritanceTest {
public static void main(String args[]){  
// Class B extends Class A
// Class C extends Class A
// Here I am creating an instance of ClassC
ClassC obj = new ClassC();
// Using object of ClassC, I can call methods of ClassA and ClassC..
// Its just because ClassC extends ClassA
// Here ClassB cant call methods of ClassB because there is no relation between ClassC and ClassB
obj.methodOneClassA();
obj.methodOneClassC();
  
}
}

Output:

1
2
I am a Method One of ClassA
I am a Method One of ClassC

Multiple Inheritance:

Multiple Inheritance is not possible in Java. In this a child extends two parents classes say Class C extends Class A and Class B.

Multiple Inheritance

Hybrid Inheritance:

Combination of more than one type of inheritance.

Hybrid Inheritance

Must Read: Java Tutorial

SUBSCRIBE TO GET FREE EBOOK AND REGULAR UPDATES ON SOFTWARE TESTING

Filed Under: Java

data-matched-content-rows-num="2" data-matched-content-columns-num="3"
Previous Article:
Exception Handling in Java
Next Article:
For Loop In Java

About the Author

Rajkumar SM is a founder of SoftwareTestingMaterial. He is a certified Software Test Engineer by profession and blogger & youtuber by choice. He has an extensive experience in the field of Software Testing. He writes here about Software Testing which includes both Manual and Automation Testing. He loves to be with his wife and cute little kid 'Freedom'.

ADVERTISEMENT

Froglogic-Squish-GUI-Tester
CrossBrowserTesting

TUTORIALS

  • Manual Testing Tutorial
  • Selenium Tutorial
  • TestNG Tutorial
  • VBScript Tutorial
  • Agile Methodology
  • SQL Tutorial for Testers
  • INTERVIEW QUESTIONS

  • Framework Interview Questions
  • Real Time Manual Testing
  • 100 Top Selenium Interview Questions
  • 30 Top TestNG Interview Questions
  • Agile Interview Questions
  • 80 SQL Interview Questions
  • TESTING TOOLS

  • Test Lodge
  • FrogLogic Squish
  • Cross Browser Testing
  • BrowserStack
  • Test Case Lab
  • Sikuli
  • © 2019 www.softwaretestingmaterial.com | About us | Privacy Policy | Contact us | Guest Post | Disclaimer | Terms of use | Sitemap