Image: Inheritance in JAVA
Image: Inheritance in JAVA

The process by which one class acquires the properties(data members) and functionalities(methods) of another class are called inheritance. Inheritance aims to provide the reusability of code so that a class has to write only the unique features and the rest of the common properties and functionalities can be extended from the other class.

Child Class:

The class that extends the features of another class is known as child class, sub cl s, r derived class.

To learn about the Oops concepts in JAVA, and Encapsulation in Java, click here.

Parent Class:

The class whose properties and functionalities are used(inherited) by another class is known as the parent class, superclass, or Base class.

Inheritance is the process of defining a new class based on an existing class by extending its common data members and methods.

Inheritance allows us to reuse code, it improves reusability in your java application.

Note: The biggest advantage of Inheritance is that the code that is already present in the base class need not be rewritten in the child class. Download the Eduriefy app now for more information.

This means that the data members(instance variables) and methods of the parent class can be used in the child class as.

To read about Polymorphism in Java, click here.

If you are finding it difficult to understand what is class and object then refer to the guide that I have shared on object-oriented programming: OOPs Concepts

Syntax: Inheritance in Java

To inherit a class we use extends keyword. Here class XYZ is child class and class ABC is parent class. The class XYZ inherits the properties and methods of the ABC class. Edureify has Bootcamp courses for different types of coding programs. Visit the Edureify website for more information.

class XYZ extends ABC

{

}

Inheritance Example

In this example, we have a base class Teacher and a subclass PhysicsTeacher. Since class PhysicsTeacher extends the designation and college properties and work() method from a base class, we need not declare these properties and methods in a subclass.

Here we have college name, designation, and work() method which is common to all the teachers so we have declared them in the base class, this way the child classes like MathTeacher, MusicTeacher, and PhysicsTeacher do not need to write this code and can be used directly from the base class.

class Teacher {
String designation = "Teacher";
String college name = "Beginners book";
void does(){
System. out.println("Teaching");
}
}
public class PhysicsTeacher extends Teacher{
String main subject = "Physics";
public static void main(String args[]){
PhysicsTeacher obj = new PhysicsTeacher();
System.out.println(obj. college name);
System.out.println(obj. designation);
System.out.println(obj. main subject);
obj. does();
}
}

Result/Output:-
Beginners book

Teacher

Physics

Teaching

Based on the above example we can say that PhysicsTeacher IS-A Teacher. This means that a child class has an IS-A relationship with the parent class. This is inheritance is known as the IS-A relationship between the child and parent class

Note:

The derived class inherits all the members and methods that are declared as public or protected. If the members or methods of a superclass are declared as private then the derived class cannot use them directly. The private members can be accessed only in their class. Such private members can only be accessed using public or protected getter and setter methods of a superclass as shown in the example below. Try the Bootcamp coding courses for future career development. Must visit the Edureify website for more information.

class Teacher {
private String designation = "Teacher";
private String college name = "Beginners book";
public String getDesignation() {
return designation;
}
protected void setDesignation(String designation) {
this. designation = designation;
}
protected String getCollegeName() {
return college name;
}
protected void setCollegeName(String college name) {
this. college name = college name;
}
void does(){
System. out.println("Teaching");
}
}
public class JavaExample extends Teacher
String main subject = "Physics";
public static void main(String args[]){
JavaExample obj = new JavaExample();
/* Note: we are not accessing the data members
* directly we are usingtheepublicc getter method
* to access the private members of the parent class
*/
System.out.println(obj.getCollegeName());
System.out.println(obj.getDesignation());
System.out.println(obj.m ain subject);
obj. does();
}
}

The output is:

Beginners book

Teacher

Physics

Teaching

The important point to note in the above example is that the child class can access the private members of the parent class through protected methods of the parent class. When we make an instance variable(data member) or method protected, this means that they are accessible only in the class itself and in the child class. These public, protected, private, etc. are all-access specifiers and we will discuss them in the coming tutorials.

Types of inheritance

To learn types of inheritance in detail, refer to Types of Inheritance in Java.

  • Single Inheritance: refers to a child and parent class relationship where a class extends another class. Try the Bootcamp web development course now and for that visit the Edureify website.
  • Multilevel inheritance: refers to a child and parent class relationship where a class extends the child’s class. For example, class C extends class B and class B extends class A.
  • Hierarchical inheritance: refers to a child and parent class relationship where more than one class extends the same class. For example, classes B, C & D extends the same class A.
  • Multiple Inheritance: refers to the concept of one class extending more than one class, which means a child class has two parent classes. For example, class C extends both classes A and B. Java doesn’t support multiple inheritances, read more about it here.
  • Hybrid inheritance: Combination of more than one type of inheritance in a single program. For example, class A & B extends class C and another class D extends class A then this is a hybrid inheritance example because it is a combination of single and hierarchical inheritance.

Constructors and Inheritance

the constructor of the subclass is invoked when we create the object of the subclass, it by default invokes the default constructor of the superclass. Hence, in inheritance, the objects are constructed top-down. The superclass constructor can be called explicitly using the super keyword, but it should be the first statement in a constructor. The super keyword refers to the superclass, immediately above the calling class in the hierarchy. The use of multiple super keywords to access an ancestor class other than the direct parent is not permitted.

class ParentClass{
//Parent class constructor
ParentClass(){
System. out.println("Constructor of Parent");
}
}
class JavaExample extends ParentClass{
JavaExample(){
/* It by default invokes the constructor of the parent class
* You can use super() to call the constructor of a parent.
* It should be the first statement in the child's class
* constructor, you can also call the parameterized constructor
* of parent class by using super like this: super(10), now
* This will invoke the parameterized constructor of int arg
*/
System. out.println("Constructor of Child");
}
public static void main(String args[]){
//Creating the object of child class
new JavaExample();
}
}

Output:

Constructor of Parent

Constructor of Child

Frequently Asked Questions (FAQs)

Question:- What is the inheritance in Java?

Answer:- Inheritance is one of the key features of OOP that allows us to create a new class from an existing class. The new class that is created is known as subclass (child or derived class) and the existing class from where the child class is derived is known as superclass (parent or base class).

Question:- What are the 4 types of inheritance in Java?

Answer:- Types of Java Inheritance

Multi-level Inheritance.

  • The multi-level inheritance includes the involvement of at least two or more two classes.
  • Hierarchical Inheritance
  • Multiple Inheritance.
  • Hybrid Inheritance.

Question:- What is inheritance and its types?

Answer:- There are different types of inheritance viz., Single inheritance, Multiple inheritance, Multilevel inheritance, hybrid inheritance, and hierarchical inheritance. Single Inheritance: When a derived class inherits only from one base class, it is known as single inheritance.

Question:- What is inheritance in Java and types?

Answer:- Inheritance in Java is a concept that acquires the properties from one class to other classes; for example, the relationship between father and son. In Java, a class can inherit attributes and methods from another class. The class that inherits the properties is known as the sub-class or the child class.

Facebook Comments