Inheritance
Introduction
The process by which one class acquires the properties (data members) and functionalities (methods) of another class is called inheritance. The aim of inheritance is 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.
Lab Overview
In this lab, we will explore and demonstrate Java Inheritance.
Learning Objective
By the end of this lab, Learners will be able to use Inheritance in Java.
Instructions
Child Class
The class that extends the features of another class is known as a child class, subclass, or derived class.
Parent Class
The class whose properties and functionalities are used (inherited) by another class is known as the parent class, superclass, or base class.
Begin
In this lab, we have a base class, “Doctor,” and a subclass, “Surgeon.”
Step 1: Create a Java project named “inheritanceDemo.”
Step 2: Create a class named Doctor, and add the code below.
Step 3: Create a class named Surgeon, and add the code below.
Step 4: Create a class named Hospital. In this class, we will create a main() method.
The project hierarchy will look like this:
Step 5: Run your Java Project.
Output on Console:
Doctor Details...
Surgeon Detail...
Cardio
Based on the above example, we can say that Surgeon IS-A Doctor. This means a child class has an IS-A relationship with the parent class. This inheritance is known as the IS-A relationship between the child and parent class.
Last updated