0% found this document useful (0 votes)
2 views4 pages

Single Inheritance

The document outlines an experiment to demonstrate single inheritance in Java using Employee and Officer classes. It includes a problem statement, algorithm, and a sample program that creates an Officer object inheriting properties from the Employee class, displaying employee details and salary. The document also provides a sample output and a set of viva questions related to inheritance in Java.

Uploaded by

yadavkrishna7120
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Single Inheritance

The document outlines an experiment to demonstrate single inheritance in Java using Employee and Officer classes. It includes a problem statement, algorithm, and a sample program that creates an Officer object inheriting properties from the Employee class, displaying employee details and salary. The document also provides a sample output and a set of viva questions related to inheritance in Java.

Uploaded by

yadavkrishna7120
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Experiment: Single Inheritance in Java

Aim
To write a Java program to demonstrate single inheritance using Employee and Officer classes.

📚 Theory
Inheritance is a mechanism in Java by which one class acquires the properties and methods of another
class.
 The class which is inherited is called the Parent (Super) Class.
 The class that inherits is called the Child (Sub) Class.
 The keyword used is extends.
👉 In Single Inheritance, one child class inherits from one parent class.

📝 Problem Statement
Write a Java program to demonstrate single inheritance.
Create a class named Employee with the following data members:
 Name
 Age
 Phone number
 Address
 Salary
Include a method printSalary() to display the salary.
Create a derived class Officer that inherits from Employee and has an additional data member:
 Specialization
In the main() method:
 Create an object of Officer
 Assign values to all data members
 Display all details including salary
Algorithm: Single Inheritance (Employee → Officer)
1. Start the program
2. Create a parent class Employee
o Declare variables: name, age, phoneNumber, address, salary

o Define a method printSalary() to display salary


3. Create a child class Officer
o Inherit Employee using extends

o Declare additional variable specialization

4. Define a method display() in Officer


o Print all employee details:

 name, age, phone number, address


o Print specialization

o Call printSalary() method

5. Create the main class


o Define main() method

6. Create an object of Officer


o Example: Officer officer = new Officer();

7. Assign values to all variables


o Set name, age, phone number, address, salary, specialization

8. Call the display method


o [Link]();

9. End the program

💻 Program
class Employee {
String name;
int age;
String phoneNumber;
String address;
double salary;

void printSalary() {
[Link]("Salary: " + salary);
}
}

class Officer extends Employee {


String specialization;

void display() {
[Link]("Name: " + name);
[Link]("Age: " + age);
[Link]("Phone Number: " + phoneNumber);
[Link]("Address: " + address);
[Link]("Specialization: " + specialization);
printSalary();
}
}

public class Main {


public static void main(String[] args) {

Officer officer = new Officer();

[Link] = "Anu";
[Link] = 35;
[Link] = "9876543210";
[Link] = "Kochi";
[Link] = 50000;
[Link] = "IT";

[Link]();
}
}

🧾 Sample Output
Name: Anu
Age: 35
Phone Number: 9876543210
Address: Kochi
Specialization: IT
Salary: 50000.0

🔍 Result
Thus, the Java program to demonstrate single inheritance was successfully executed.

Viva Questions
1. What is inheritance?
2. What is single inheritance?
3. Which keyword is used for inheritance in Java?
4. Can a child class access parent class methods?
5. What is the advantage of inheritance?

You might also like