This project demonstrates inheritance in Java. A base class called Office stores common employee details, and two subclasses — Teaching and Non_teaching — inherit those fields and add their own designation.
Office with employee number, name, and salaryTeaching and Non_teaching inherit from OfficesetData() reads employee details from the usergetData() prints the stored employee detailsmain()import java.util.Scanner;
class Office
{
int empNo;
String empName;
int salary;
}
class Teaching extends Office
{
String designation;
void setData()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Employee Number: ");
empNo = sc.nextInt();
System.out.println("Enter Employee Name: ");
empName = sc.next();
System.out.println("Enter Employee Salary: ");
salary = sc.nextInt();
System.out.println("Enter Employee Designation: ");
designation = sc.next();
}
void getData()
{
System.out.println("Employee Name: " + empName + "\t\tEmployee Number: " + empNo);
System.out.println("Employee Salary: " + salary + "\t\tEmployee Designation: " + designation);
}
}
class Non_teaching extends Office
{
String designation;
void setData()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Employee Number: ");
empNo = sc.nextInt();
System.out.println("Enter Employee Name: ");
empName = sc.next();
System.out.println("Enter Employee Salary: ");
salary = sc.nextInt();
System.out.println("Enter Employee Designation: ");
designation = sc.next();
}
void getData()
{
System.out.println("Employee Name: " + empName + "\t\tEmployee Number: " + empNo);
System.out.println("Employee Salary: " + salary + "\t\tEmployee Designation: " + designation);
}
}
public class Inheritance
{
public static void main(String[] args)
{
Teaching t1 = new Teaching();
Non_teaching nt1 = new Non_teaching();
System.out.println("Data about Teaching Class: \n");
t1.setData();
t1.getData();
System.out.println("\nData about Non Teaching Class: \n");
nt1.setData();
nt1.getData();
}
}Office stores common employee fields: number, name, and salaryTeaching and Non_teaching extend Office, inheriting those fields and adding a designationsetData() prompts the user and stores the employee detailsgetData() prints the employee details to the consolemain() creates one teaching employee and one non-teaching employee, then shows both