> For the complete documentation index, see [llms.txt](https://peterlulu666.gitbook.io/tek-system-java-developer/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://peterlulu666.gitbook.io/tek-system-java-developer/week-3/lab/inheritance.md).

# 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.

```java
public class Doctor {
   String DoctorName;
   String Department;
  public void Doctor_Details() {
       System.out.println("Doctor Details...");
   }
}

```

#### Step 3: Create a class named Surgeon, and add the code below.

```java
public class Surgeon extends Doctor {
   void Surgeon_Details() {
       System.out.println("Surgeon  Detail...");
       System.out.println(Department = "Cardio");
   }
}

```

#### Step 4: Create a class named Hospital. In this class, we will create a main() method.

```java
public class Hospital {
   public static void main(String args[]) {
       Surgeon s = new Surgeon();
       s.Doctor_Details();
       s.Surgeon_Details();
   }
}

```

#### The project hierarchy will look like this:

```
inheritanceDemo/
├── Doctor.java
├── Hospital.java
└── Surgeon.java

```

#### 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.

{% file src="/files/71Gtf37BUnHCQh9HXdxq" %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://peterlulu666.gitbook.io/tek-system-java-developer/week-3/lab/inheritance.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
