<p>In this tutorial, we will learn about inheritance in Java with the help of examples.</p>
<h3><span style="color: #000080;"><strong>Inheritance</strong></span></h3>
<p><span style="color: var(--color-text);">Java <span style="color: #008000;"><strong><span style="color: #0000ff;">Inheritance</span> </strong></span>is one of the key feature of <strong><span style="color: #0000ff;">OOP </span>(Object Oriented Programming)</strong>. <strong><span style="color: #008000;">Inheritance</span> </strong>can be defined as the process where one class is allowed to inherit the features (fields and methods) of another class. </span></p>
<p>Inheritance supports the concept of “<strong><span style="color: #008000;">reusability</span></strong>”, i.e. The code that is present in the parent class doesn’t need to be written again in the child class.</p>
<p>The class which inherits the features of other class is known as <strong><span style="color: #008000;">sub class</span> </strong>(or a derived class or a child class).</p>
<p>The class whose features are inherited is known as <span style="color: #008000;"><strong>super class</strong> </span>( or a base class or a parent class).</p>
<h5><span style="color: #0000ff;"><strong>Syntax</strong></span></h5>
<pre>class Subclass-name <span style="color: #0000ff;"><strong>extends</strong> </span>Superclass-name
 {
 <strong><span style="color: #008000;">//methods and fields </span> </strong>
 }</pre>
<p>In Java, we use the <span style="color: #008000;"><strong>extends</strong> </span>keyword to inherit features from a super class in sub class.</p>
<h5><strong><span style="color: #0000ff;">Example</span></strong></h5>
<pre><span style="color: #008000;"><strong>//Calculator is a super class</strong></span>
class <span style="color: #0000ff;"><strong>Calculator</strong> </span>{

<strong><span style="color: #008000;"> //Calculator class members</span></strong>
 public int add(int x, int y) {
 return x + y;
 }

 public int sub(int x, int y) {
 return x - y;
 }
}

<strong><span style="color: #008000;">//CalcAdv is a sub class</span></strong>
class <strong><span style="color: #0000ff;">CalcAdv </span></strong><span style="color: #000000;">extends Calculator</span>{

<strong><span style="color: #008000;"> //CalcAdv class members</span></strong>
 public int sqr(int x) {
 return x * x;
 }

 public double sqrt(float y) {
 return Math.sqrt(y);
 }

}

public class Main {

 public static void main(String args[]) {

 <strong> <span style="color: #008000;">//creating CalcAdv object</span></strong>
 CalcAdv calcAdv = new CalcAdv();

 System.out.println("The sum of the given numbers: " + <span style="color: #0000ff;"><strong>calcAdv.add(10, 20)</strong></span>);
 System.out.println("The difference between the given numbers: " + <span style="color: #0000ff;"><strong>calcAdv.sub(30, 20)</strong></span>);
 System.out.println("The square of a given number: " + <span style="color: #0000ff;"><strong>calcAdv.sqr(10)</strong></span>);
 System.out.println("The square root of a given number: " + <span style="color: #0000ff;"><strong>calcAdv.sqrt(25)</strong></span>);

 }
}</pre>
<h5><strong><span style="color: #0000ff;">Output</span></strong></h5>
<pre>The sum of the given numbers: 30
The difference between the given numbers: 10
The square of a given number: 100
The square root of a given number: 5.0</pre>
<h5><strong><span style="color: #0000ff;">Example explained</span></strong></h5>
<ul>
<li>Here, we have inherited a subclass <span style="color: #0000ff;"><strong>CalcAdv</strong> </span>from super class <span style="color: #0000ff;"><strong>Calculator</strong></span>. The <span style="color: #008000;"><strong>CalcAdv</strong> </span>class inherits the methods <span style="color: #008000;"><strong>add()</strong></span> and <strong><span style="color: #008000;">sub()</span></strong> from the <strong><span style="color: #008000;">Calculator </span></strong>class.</li>
<li><span style="color: var(--color-text);">When an object of <span style="color: #008000;"><strong>CalcAdv</strong> </span></span><span style="color: var(--color-text);">class is created, a copy of all methods and fields of the super class acquire memory in this object. That is why by using the object of the subclass we can also access the members of a super class.</span></li>
<li><span style="color: var(--color-text);">Hence, objects of the <span style="color: #008000;"><strong>CalcAdv</strong> </span>class can access the members of <strong><span style="color: #008000;">Calculator</span> </strong>class.</span></li>
</ul>
<h4></h4>
<h3><span style="color: #000080;"><strong>Types of Inheritance in Java</strong></span></h3>
<p>There are various types of inheritance in Java:</p>
<h4><span style="color: #000080;"><strong>Single Inheritance</strong></span></h4>
<p>In single Inheritance, a class inherits features of another class (one class only).</p>
<p><img class=" wp-image-1877 aligncenter" src="https://c1ctech.com/wp-content/uploads/2020/05/single_inheritance_img.png" alt="single_inheritance_img" width="320" height="271" /></p>
<p>In above diagram, Class B extends only Class A. Class A is a super class and Class B is a Sub class.</p>
<h5><span style="color: #0000ff;"><strong>Example: Single Inheritance</strong></span></h5>
<pre><span style="color: #008000;"><strong>//Person is a super class</strong></span>
class Person {

 private String name;
 private int age;

 public String getName() {
 return name;
 }

 public void setName(String name) {
 this.name = name;
 }

 public int getAge() {
 return age;
 }

 public void setAge(int age) {
 this.age = age;
 }

}

<strong><span style="color: #008000;">//Student is a sub class</span></strong>
class Student extends Person {

 private int rollno;

 public int getRollno() {
 return rollno;
 }

 public void setRollno(int rollno) {
 this.rollno = rollno;
 }

}

public class Main {
 public static void main(String[] args) {
 <strong><span style="color: #008000;">//creating Student instance</span></strong>
 Student student = new Student();
 student.setRollno(101);
 student.setName("Arun Verma");
 student.setAge(18);

 System.out.println("Rollno: " + student.getRollno());
 System.out.println("Name: " + student.getName());
 System.out.println("Age: " + student.getAge());

 }
}</pre>
<h5><span style="color: #0000ff;"><strong>Output</strong></span></h5>
<pre>Rollno: 101
Name: Arun Verma
Age: 18</pre>
<h4></h4>
<h4><span style="color: #000080;"><strong>Multilevel Inheritance</strong></span></h4>
<p>In Multilevel Inheritance, a sub class inherits all the properties and behaviours of more than one super class at multiple levels.</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>

<p><img class=" wp-image-1880 aligncenter" src="https://c1ctech.com/wp-content/uploads/2020/05/multilevel_inheritance_img.png" alt="multilevel_inheritance_img" width="343" height="382" /></p>
<p>In above diagram, Class A is a super class of Class B and Class B is a super class of Class C or Class C is a subclass of Class B and Class B is a subclass of Class A.</p>
<h5><span style="color: #0000ff;"><strong>Example: Multilevel Inheritance</strong></span></h5>
<pre><span style="color: #008000;"><strong>//Person is a super class of Student</strong></span>
class <strong><span style="color: #0000ff;">Person</span> </strong>{
 private String name;
 private int age;

 public String getName() {
 return name;
 }

 public void setName(String name) {
 this.name = name;
 }

 public int getAge() {
 return age;
 }

 public void setAge(int age) {
 this.age = age;
 }

}
<span style="color: #008000;"><strong>//Student is a sub class of Person and super class of School</strong></span>
class <strong><span style="color: #0000ff;">Student</span> </strong>extends Person {

 private int rollno;

 public int getRollno() {
 return rollno;
 }

 public void setRollno(int rollno) {
 this.rollno = rollno;
 }

}
<span style="color: #008000;"><strong>//School is a sub class of Student</strong></span>
class <strong><span style="color: #0000ff;">School</span> </strong>extends Student {

 private String schoolName;
 private String standard;
 private char section;

 public String getSchoolName() {
 return schoolName;
 }

 public void setSchoolName(String schoolName) {
 this.schoolName = schoolName;
 }
}

public class Main {

 public static void main(String[] args) {
 <span style="color: #008000;"><strong>//creating School object</strong></span>
 School school = new School();
 school.setName("Rohit Verma");
 school.setRollno(102);
 school.setSchoolName("Global Public School");

 System.out.println("Name: " + school.getName());
 System.out.println("Rollno: " + school.getRollno());
 System.out.println("School Name: " + school.getSchoolName());

 }
}</pre>
<h5><span style="color: #0000ff;"><strong>Output</strong></span></h5>
<pre>Name: Rohit Verma
Rollno: 102
School Name: Global Public School</pre>
<h4></h4>
<h4><span style="color: #000080;"><strong>Hierarchical Inheritance</strong></span></h4>
<p>In hierarchical inheritance, two or more classes inherits a single class.</p>
<p><img class=" wp-image-1881 aligncenter" src="https://c1ctech.com/wp-content/uploads/2020/05/hierarchical_inheritance_img.png" alt="hierarchical_inheritance_img" width="558" height="247" /></p>
<p>In above diagram, class B, C, D inherit the same class A.</p>
<h5><span style="color: #0000ff;"><strong>Example: Hierarchical Inheritance</strong></span></h5>
<pre><strong><span style="color: #008000;">//Person is a super class of Student and Teacher</span></strong>
class <span style="color: #0000ff;"><strong>Person</strong> </span>{
 private String name;
 private int age;

 public String getName() {
 return name;
 }

 public void setName(String name) {
 this.name = name;
 }

 public int getAge() {
 return age;
 }

 public void setAge(int age) {
 this.age = age;
 }

}
<span style="color: #008000;"><strong>//Student is a sub class of Person</strong></span>
class <strong><span style="color: #0000ff;">Student</span> </strong>extends Person {

 private int rollno;

 public int getRollno() {
 return rollno;
 }

 public void setRollno(int rollno) {
 this.rollno = rollno;
 }

}
<span style="color: #008000;"><strong>//Teacher is a sub class of Person</strong></span>
class <span style="color: #0000ff;"><strong>Teacher</strong> </span>extends Person {

 private int salary;

 public int getSalary() {
 return salary;
 }

 public void setSalary(int salary) {
 this.salary = salary;
 }
}

public class Main {
 public static void main(String[] args) {
 <span style="color: #008000;"><strong>//creating Student object</strong></span>
 Student student = new Student();
 student.setRollno(101);
 student.setName("Arun Verma");
 student.setAge(18);
 <strong><span style="color: #008000;"> //creating Teacher object</span></strong>
 Teacher teacher = new Teacher();
 teacher.setSalary(15000);
 teacher.setName("Sachin Sharma");
 teacher.setAge(30);

 System.out.println("Student Name: " + student.getName() + " | " + "Rollno: " + student.getRollno() + " | " + "Age: " + student.getAge());
 System.out.println("Teacher Name: " + teacher.getName() + " | " + "Salary: " + teacher.getSalary() + " | " + "Age: " + teacher.getAge());

 }
}</pre>
<h5><span style="color: #0000ff;"><strong>Output</strong></span></h5>
<pre>Student Name: Arun Verma | Rollno: 101 | Age: 18
Teacher Name: Sachin Sharma | Salary: 15000 | Age: 30</pre>
<h5></h5>
<h4><span style="color: #000080;"><strong>Multiple Inheritance</strong></span></h4>
<p>In Multiple inheritance, one class can have more than one super class and inherit features from all parent classes.</p>
<p><img class=" wp-image-1883 aligncenter" src="https://c1ctech.com/wp-content/uploads/2020/05/multiple_inheritance.png" alt="multiple_inheritance" width="502" height="220" /></p>
<p>In above diagram, Class C extends Class A and Class B both.</p>
<h4><span style="color: #000080;"><strong>Hybrid Inheritance</strong></span></h4>
<p>Hybrid Inheritance is a mix of two or more of the above types of inheritance.</p>
<p><img class=" wp-image-1885 aligncenter" src="https://c1ctech.com/wp-content/uploads/2020/05/hybrid_inheritance_img.png" alt="hybrid_inheritance_img" width="512" height="322" /></p>
<p> ;</p>
<p><span style="color: #000080;"><strong>Note</strong></span>: Java does not support Multiple and Hybrid inheritance with classes. In java, we can achieve both inheritance only through Interfaces.</p>
<p> ;</p>
<p> ;</p>


