8.
Java program to understand the concept of inheritance
ALGORITHM
Step 1: Start
Step 2: Create a class Employee with required variables name, age, phone no, address and
salary.
Step 3: Define method displayEmployeeDetails() in employee class to display all employee
details.
Step 4: Create a subclass of class Employee named Manager. Include the variable
specialization in manager class.
Step 5: Define method displayManagerDetails() in manager class which calls the
displayEmployeeDetails() of superclass and then prints the specialization of manager.
Step 6: Create another subclass of class Employee named Officer. Include the variable
department in Officer class.
Step 7: Define method displayOfficerDetails() in officer class which calls the
displayEmployeeDetails() of superclass and then prints the department of officer.
Step 8: Create a class with the main function.
Step 9: Inside the main function read the details of manager from user and create an object
of manager class.
Step 10: Print the manager details by calling the method displayManagerDetails() of
subclass, which invokes the method displayEmployeeDetails() of superclass.
Step 11: Inside the main function, next read the details of officer from user and create an
object of officer class.
Step 12: Print the officer details by calling the method displayOfficerDetails() of subclass,
which invokes the method displayEmployeeDetails() of superclass.
Step 13: Stop
INFERENCE
Understood the concept of inheritance in java by creating a super class and then inheriting
two sub classes from it. Called methods of the subclasses from the main method which
inturn invokes the methods of super class.
PROGRAM
import java.util.Scanner;
//superclass class employee
class Employee
private String name;
private int age;
private String phone;
private String address;
private double salary;
public Employee(String name,int age,String phone,String address,double salary)
this.name = name;
this.age = age;
this.phone = phone;
this.address = address;
this.salary = salary;
public void displayEmployeeDetails()
System.out.println("Name = "+name);
System.out.println("Age = "+age);
System.out.println("Phone Number = "+phone);
System.out.println("Address = "+address);
System.out.println("Salary = "+salary);
//class manager inherited from employee.
class Manager extends Employee
private String specialization;
public Manager(String name,int age,String phone,String address,double salary, String
specialization)
super(name,age,phone,address,salary);
this.specialization = specialization;
public void displayManagerDetails()
displayEmployeeDetails();
System.out.println("Manager Specialization ="+specialization);
//class officer inherited from employee.
class Officer extends Employee
{
private String department;
public Officer(String name,int age,String phone,String address,double salary,
String department)
super(name,age,phone,address,salary);
this.department = department;
public void displayOfficerDetails()
displayEmployeeDetails();
System.out.println("Officer Department ="+department);
class EmployeeInheritance
public static void main(String args[])
Scanner sc = new Scanner(System.in);
System.out.println("\nEnter Manager Details");
System.out.print("Name:");
String name = sc.nextLine();
System.out.print("Age:");
int age = sc.nextInt();
sc.nextLine();
System.out.print("Phone Number:");
String phone = sc.nextLine();
System.out.print("Address:");
String addr = sc.nextLine();
System.out.print("Salary:");
double salary = sc.nextDouble();
sc.nextLine();
System.out.print("Specialization:");
String spec = sc.nextLine();
Manager m = new Manager(name,age,phone,addr,salary,spec);
System.out.println("\nDISPLAYING MANAGER DETAILS:\n");
m.displayManagerDetails();
System.out.println("\nEnter Officer Details");
System.out.print("Name:");
String name1 = sc.nextLine();
System.out.print("Age:");
int age1 = sc.nextInt();
sc.nextLine();
System.out.print("Phone Number:");
String phone1 = sc.nextLine();
System.out.print("Address:");
String addr1 = sc.nextLine();
System.out.print("Salary:");
double salary1 = sc.nextDouble();
sc.nextLine();
System.out.print("Officer department:");
String dept = sc.nextLine();
Officer o = new Officer(name1,age1,phone1,addr1,salary1,dept);
System.out.println("\nDISPLAYING OFFICER DETAILS:\n");
o.displayOfficerDetails();