Introduction
Inheritance is a very crucial component of object-oriented programming that works in a similar fashion to how we inherit traits from our parents. In object-oriented programming, properties and behaviours of a class—that is, the data and methods of a class—are referred to as characteristics.Because inheritance allows objects to inherit properties and actions from other objects, it is a cornerstone of object-oriented programming.
Inheritance allows you to spread control over object development and maintenance. A programmer, for example, may be in charge of generating and maintaining the student object. The graduate student object might be created and maintained by another programmer. When a modification that affects all students happens, the student object is updated, and the graduate student object inherits the changes. Because the graduate student object inherits any modifications made to the student object, only the programmer responsible for the student object needs to handle those changes.
Inheritance also allows you to restrict access to certain traits and actions. As we all know, access specifiers are used to designate which sections of the programme have access to certain properties and actions. Members of a class that are available to other members of the class, other classes, and other elements of the programme are defined by the public access specifier.
The private access specifier is used to specify members that can only be accessed by other members of the same class. Other classes and portions of the curriculum do not have access to them. The protected access specifier describes the characteristics and actions that are available to members of the class and inherited classes. Limiting access to properties and actions serves to protect the object's integrity by limiting how other classes and areas of the application interact with it.
Let's see with a practical example of inheritance and have a detailed discussion over class hierarchy in c++ and java.
Also see, Swap Function in Java
Class Hierarchy
A parent-child connection is a term used to describe the hierarchical relationship between classes. The child inherits all of the parent's attributes and behaviours, and it utilises the parent's access specifier to govern how those inherited things are exposed to other classes or functions in a parent-child connection.
The parent is referred to as a base class in C++, whereas the child is referred to as a derived class. The superclass in Java is the parent, while the subclass is the child.
Types of classes
Inheritance may be implemented in a software in three ways. There are three types of inheritance:
1. simple inheritance
2. multiple inheritance, and
3. level inheritance.
Each uses somewhat different ways to allow a class to access the characteristics and actions of another class.
Simple Inheritance
When there is only one parent-child connection, simple inheritance occurs. In other words, just one child inherits from each father.
C++ Program
#include <iostream>
#include <string.h>
using namespace std;
class Student
{
protected:
int m_Graduation, m_ID;
char m_First[16], m_Last[16];
public:
virtual void Display()
{
cout << "ID: " << m_ID << endl;
cout << "First: " << m_First << endl;
cout << "Last: " << m_Last << endl;
cout << "Graduation: " << m_Graduation << endl;
}
void Write(int ID, char First[], char Last[], int Graduation)
{
m_ID = ID;
strcpy(m_First, First);
strcpy(m_Last, Last);
m_Graduation = Graduation;
}
Student()
{
m_ID = m_Graduation = 0;
m_First[0] = m_Last[0] = '\0';
}
};
class GradStudent : public Student
{
protected:
int m_UndergradGraduation;
char m_UndergradSchool[64];
char m_Major[64];
public:
GradStudent()
{
m_UndergradGraduation = 0;
m_UndergradSchool[0] = m_Major[0] = '\0';
}
virtual void Write(int ID, char First[], char Last[], int Graduation, char Major[], char UndergradSchool[], int UndergradGraduation)
{
Student::Write(ID, First, Last, Graduation);
strcpy(m_Major, Major);
strcpy(m_UndergradSchool, UndergradSchool);
m_UndergradGraduation = UndergradGraduation;
}
virtual void Display()
{
Student::Display();
cout << "Major: " << m_Major << endl;
cout << "Undergrad school: " << m_UndergradSchool << endl;
cout << "Undergrad graduation: " << m_UndergradGraduation << endl;
}
};
int main()
{
Student student;
GradStudent graduate;
student.Write(100, "Aman", "Thakur", 2023);
graduate.Write(101, "Aman", "Chowrasia", 2022, "Computer Science", "IIT", 2018);
student.Display();
graduate.Display();
return 0;
}
Output
Java Program
class Student {
protected int nID, nGraduation;
protected String studentFirst, studentLast;
public Student() {
nID = 0;
nGraduation = 0;
studentFirst = new String();
studentLast = new String();
}
public void Display() {
System.out.println("ID: " + nID);
System.out.println("First: " + studentFirst);
System.out.println("Last: " + studentLast);
System.out.println("Graduation: " + nGraduation);
}
public void Write(int nID, String studentFirst, String studentLast, int nGraduation) {
this.nID = nID;
this.studentFirst = studentFirst;
this.studentLast = studentLast;
this.nGraduation = nGraduation;
}
}
class GradStudent extends Student {
String studentMajor, studentUndergradSchool;
int nUndergradGraduation;
public GradStudent() {
studentMajor = "";
studentUndergradSchool = "";
nUndergradGraduation = 0;
}
public void Display() {
super.Display();
System.out.println("Major: " + studentMajor);
System.out.println("Undergrad Graduation year: " +
nUndergradGraduation);
System.out.println("Undergrad School: " + studentUndergradSchool);
}
public void Write(int ID, String First, String Last, int Graduation, String Major, String UndergradSchool,
int UndergradGraduation) {
super.Write(ID, First, Last, Graduation);
studentUndergradSchool = UndergradSchool;
studentMajor = Major;
nUndergradGraduation = UndergradGraduation;
}
}
public class StudentApp {
public static void main(String[] args) {
Student student = new Student();
GradStudent graduate = new GradStudent();
student.Write(100, "Aman", "Thakur", 2023);
graduate.Write(101, "Aman", "Chowrasia", 2022, "Computer Science", "IIT", 2018);
student.Display();
graduate.Display();
}
public static void Display(Student student) {
student.Display();
}
public static void Display(GradStudent graduate) {
graduate.Display();
}
}
Output
Multiple Inheritance
When there are many parents and a child in a relationship, multiple inheritance occurs. In other words, the child receives inheritance from both parents.
C++ Program
#include <iostream>
#include <string.h>
using namespace std;
class Person
{
protected:
int m_ID;
char m_First[16], m_Last[16];
public:
Person()
{
this->m_ID = 0;
this->m_First[0] = this->m_Last[0] = '\0';
}
virtual void Display()
{
cout << "ID: " << m_ID << endl;
cout << "First: " << m_First << endl;
cout << "Last: " << m_Last << endl;
}
void Write(int ID, char First[], char Last[])
{
this->m_ID = ID;
strcpy(this->m_First, First);
strcpy(this->m_Last, Last);
}
};
class Worker
{
protected:
int m_Income;
public:
Worker()
{
this->m_Income = 0;
}
void Write(int Income)
{
m_Income = Income;
}
void Display()
{
cout << "Income: " << m_Income << endl;
}
};
class Instructor : public Person, public Worker
{
protected:
bool m_Tenured;
public:
Instructor()
{
this->m_Tenured = false;
}
void Write(int ID, char First[], char Last[], bool Tenured,
int Salary)
{
Person::Write(ID, First, Last);
Worker::Write(Salary);
m_Tenured = Tenured;
}
void Display()
{
Person::Display();
Worker::Display();
cout << "Tenured: " << (m_Tenured ? "Yes" : "No") << endl;
}
};
int main()
{
Instructor instructor;
instructor.Write(100, "Aman", "Thakur", true, 15000);
instructor.Display();
return 0;
}
Output
Java Program
Java does not allow multiple inheritance. As a result, if you want a class to inherit from two or more other classes, you'll need to employ level inheritance. Remember that the "is a" test must be passed by each class. Level inheritance should not be utilised with any class that fails this condition.
Instead, Java provides interfaces, which might behave or appear to be Multiple Inheritance in Java in various respects. An interface, on the other hand, is best compared to a totally abstract class. That is, it declares the names of member functions but does not give any reusable code from which inheritance may be used. If we use a Java class to implement an interface, we must write all of the necessary code for those functions ourselves.As a result, interfaces are not considered to be part of inheritance.
Interfaces should be thought of as a series of promises. If a class implements an interface (and so has all of the code required to do so), other objects can interact or cooperate with the class through the interface.
You can also read about the Multiple Inheritance in Java, Duck Number in Java
Level inheritance
When a child inherits from a parent and subsequently becomes a parent to a child, this is known as level inheritance.
C++ Program
#include <iostream>
#include <string.h>
using namespace std;
class Person
{
protected:
int m_ID;
char m_First[16], m_Last[16];
public:
Person()
{
m_ID = 0;
m_First[0] = m_Last[0] = '\0';
}
virtual void Display()
{
cout << "ID: " << m_ID << endl;
cout << "First: " << m_First << endl;
cout << "Last: " << m_Last << endl;
}
void Write(int ID, char First[], char Last[])
{
m_ID = ID;
strcpy(m_First, First);
strcpy(m_Last, Last);
}
};
class Student : public Person
{
protected:
int m_Graduation;
public:
virtual void Display()
{
Person::Display();
cout << "Graduation: " << m_Graduation << endl;
}
void Write(int ID, char First[], char Last[], int Graduation)
{
Person::Write(ID, First, Last);
m_Graduation = Graduation;
}
Student()
{
m_Graduation = 0;
}
};
class GradStudent : public Student
{
protected:
int m_UndergradGraduation;
char m_UndergradSchool[64];
char m_Major[64];
public:
GradStudent()
{
m_UndergradGraduation = 0;
m_UndergradSchool[0] = m_Major[0] = '\0';
}
virtual void Write(int ID, char First[], char Last[], int Graduation,
char Major[], char UndergradSchool[], int UndergradGraduation)
{
Student::Write(ID, First, Last, Graduation);
strcpy(m_Major, Major);
strcpy(m_UndergradSchool, UndergradSchool);
m_UndergradGraduation = UndergradGraduation;
}
virtual void Display()
{
Student::Display();
cout << "Major: " << m_Major << endl;
cout << "Undergrad school: " << m_UndergradSchool << endl;
cout << "Undergrad graduation: " << m_UndergradGraduation << endl;
}
};
int main()
{
Student student;
GradStudent graduate;
student.Write(100, "Aman", "Thakur", 2023);
graduate.Write(101, "Aman", "Chowrasia", 2022, "Computer Science", "IIT", 2018);
student.Display();
graduate.Display();
return 0;
}
Output
Java Program
class Person
{
protected int nID;
protected String studentFirst, studentLast;
public Person()
{
this.nID = 0;
this.studentFirst = "";
this.studentLast = "";
}
public void Display()
{
System.out.println("ID: " + nID);
System.out.println("First: " + studentFirst);
System.out.println("Last: " + studentLast);
}
public void Write( int ID, String First, String Last )
{
nID = ID;
this.studentFirst = First;
this.studentLast = Last;
}
}
class Student extends Person
{
protected int nGraduation;
public Student()
{
this.nGraduation = 0;
}
public void Display()
{
super.Display();
System.out.println("Graduation: " + nGraduation);
}
public void Write( int ID, String First, String Last, int Graduation )
{
super.Write(ID, First, Last);
this.nGraduation = Graduation;
}
}
class GradStudent extends Student
{
String studentMajor, studentUndergradSchool;
int nUndergradGraduation;
public GradStudent()
{
this.studentMajor = "";
this.studentUndergradSchool = "";
this.nUndergradGraduation = 0;
}
public void Display()
{
super.Display();
System.out.println("Major: " + studentMajor );
System.out.println("Undergrad Graduation year: " +
nUndergradGraduation);
System.out.println("Undergrad School: " + studentUndergradSchool );
}
public void Write( int ID, String First, String Last, int Graduation,String Major, String UndergradSchool, int UndergradGraduation )
{
super.Write( ID, First, Last, Graduation);
this.studentUndergradSchool = UndergradSchool;
this.studentMajor = Major;
this.nUndergradGraduation = UndergradGraduation;
}
}
public class StudentApp
{
public static void main(String[] args)
{
Student student = new Student();
GradStudent graduate = new GradStudent();
student.Write(100, "Aman", "Thakur", 2023);
graduate.Write(101, "Aman", "Chowrasia", 2022, "Computer Science", "IIT", 2018);
student.Display();
graduate.Display();
}
public static void Display(Student student)
{
student.Display();
}
public static void Display(GradStudent graduate)
{
graduate.Display();
}
}
Output
Try Online Java Compiler.
Know about Single Inheritance in Java in detail.