0% found this document useful (0 votes)
3 views10 pages

Java Inheritance Lab-2

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)
3 views10 pages

Java Inheritance Lab-2

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
You are on page 1/ 10

JAVA INHERITANCE LAB-2

Q1.Create a small application in java for College management system

i)A class Teacher contains two fields Name and Qualification. Extends the class to
department it contains dept. no and Dept Name.

ii)An interface named as college


a) it contains one field name of the college
b ) one abstract method getDetails, and showDetails
c) one default and static method which displays general message.
iii)Override base class and interface methods in derived class
Ask department name from user and check it must be either “IT dept” or Management
dept” .If department name does not match print “not a valid Record”
iv) For IT department department number is 10 and for management dept no is 20 .
v) Using the above classes and interface get the appropriate information and display it.
vi)Create a class “CMS “ having main method where all the above functions are called.

SOL: package ASSIGN2INHERITANCE;

import java.util.Scanner;

interface College {

String collegeName = "ABC College";

void getDetails();

void showDetails();

default void generalMessage() {

System.out.println("Welcome to " + collegeName + "!");

static void staticMessage() {

System.out.println("This is a College Management System.");

}
class Teacher {

String name;

String qualification;

Teacher(String name, String qualification) {

this.name = name;

this.qualification = qualification;

class Department extends Teacher implements College {

int deptNo;

String deptName;

Department(String name, String qualification, String deptName) {

super(name, qualification);

this.deptName = deptName;

setDeptNo(deptName);

private void setDeptNo(String deptName) {

if (deptName.equalsIgnoreCase("IT dept")) {

this.deptNo = 10;

} else if (deptName.equalsIgnoreCase("Management dept")) {

this.deptNo = 20;

} else {

this.deptNo = -1;
}

public void getDetails() {

Scanner sc = new Scanner(System.in);

System.out.println("Enter Teacher Name:");

name = sc.nextLine();

System.out.println("Enter Qualification:");

qualification = sc.nextLine();

System.out.println("Enter Department Name (IT dept or Management dept):");

deptName = sc.nextLine();

setDeptNo(deptName);

sc.close();

public void showDetails() {

if (deptNo == -1) {

System.out.println("Not a valid Record");

return;

System.out.println("\n--- Teacher Details ---");

System.out.println("College Name: " + College.collegeName);

System.out.println("Teacher Name: " + name);

System.out.println("Qualification: " + qualification);

System.out.println("Department: " + deptName);

System.out.println("Department No: " + deptNo);

}
public class CMS {

public static void main(String[] args) {

Department dept = new Department("", "", "");

dept.generalMessage();

College.staticMessage();

dept.getDetails();

dept.showDetails();

OUTPUT: Welcome to ABC College!

This is a College Management System.

Enter Teacher Name:

ABCD

Enter Qualification:

PHD

Enter Department Name (IT dept or Management dept):

IT dept

--- Teacher Details ---

College Name: ABC College

Teacher Name: ABCD

Qualification: PHD

Department: IT dept

Department No: 10
Q2. Create a Java program with the following components and a corresponding UML Class
Diagram:Abstract Class Employee:

 Attributes: String name, int employeeId, double baseSalary.

 Constructor to initialize these attributes.

 Abstract method: public abstract double calculateSalary().

 Concrete method: public void displayInfo() that prints the employee's name, ID, and
base salary.

Interface IProjectAssignable:

 Method signatures: public void assignToProject(String project), public List<String>


getAssignedProjects().

Concrete Class Developer:

 Extends Employee and implements IProjectAssignable.

 Additional attribute: double bonus.

 Override calculateSalary() to return baseSalary + bonus.

 Implement assignToProject() and getAssignedProjects() to manage a list of projects.

Concrete Class Manager:

 Extends Employee.

 Additional attributes: double projectBonus, int managedTeamSize.

 Override calculateSalary() to return baseSalary + (projectBonus * managedTeamSize).

 Implement an additional method specific to managers, e.g., public void


conductPerformanceReview().

Final Class HRStaff:

 Extends Employee.

 Override calculateSalary() to simply return baseSalary, as they don't receive


performance-based bonuses.

 Add a unique attribute String department.

Main Class (Main):

 Create objects of Developer, Manager, and HRStaff.

 Demonstrate polymorphism by calling calculateSalary() on each of them.


 Show how IProjectAssignable is used by calling assignToProject() on the Developer
object but not on the Manager or HRStaff objects, showcasing the power of
interfaces for specific functionalities.

 Call displayInfo() on all objects.

SOL: package ASSIGN2INHERITANCE;

import java.util.ArrayList;

import java.util.List;

// Abstract class Employee

abstract class Employee {

String name;

int employeeId;

double baseSalary;

public Employee(String name, int employeeId, double baseSalary) {

this.name = name;

this.employeeId = employeeId;

this.baseSalary = baseSalary;

public abstract double calculateSalary();

public void displayInfo() {

System.out.println("\n--- Employee Info ---");

System.out.println("Name: " + name);

System.out.println("Employee ID: " + employeeId);

System.out.println("Base Salary: $" + baseSalary);

// Interface

interface IProjectAssignable {

void assignToProject(String project);

List<String> getAssignedProjects();
}

// Developer Class

class Developer extends Employee implements IProjectAssignable {

double bonus;

List<String> projects = new ArrayList<>();

public Developer(String name, int id, double baseSalary, double bonus) {

super(name, id, baseSalary);

this.bonus = bonus;

public double calculateSalary() {

return baseSalary + bonus;

public void assignToProject(String project) {

projects.add(project);

public List<String> getAssignedProjects() {

return projects;

// Manager Class

class Manager extends Employee {

double projectBonus;

int managedTeamSize;

public Manager(String name, int id, double baseSalary, double projectBonus, int
teamSize) {

super(name, id, baseSalary);

this.projectBonus = projectBonus;

this.managedTeamSize = teamSize;
}

public double calculateSalary() {

return baseSalary + (projectBonus * managedTeamSize);

public void conductPerformanceReview() {

System.out.println(name + " is conducting a performance review.");

// Final HRStaff Class

final class HRStaff extends Employee {

String department;

public HRStaff(String name, int id, double baseSalary, String department) {

super(name, id, baseSalary);

this.department = department;

public double calculateSalary() {

return baseSalary;

public class UML {

public static void main(String[] args) {

Developer dev = new Developer("Alice", 101, 50000, 10000);

dev.assignToProject("Banking App");

dev.assignToProject("AI Assistant");

Manager mgr = new Manager("Bob", 102, 60000, 3000, 5);

HRStaff hr = new HRStaff("Carol", 103, 45000, "Recruitment");

// Display Information

dev.displayInfo();
System.out.println("Salary: $" + dev.calculateSalary());

System.out.println("Projects: " + dev.getAssignedProjects());

mgr.displayInfo();

System.out.println("Salary: $" + mgr.calculateSalary());

mgr.conductPerformanceReview();

hr.displayInfo();

System.out.println("Salary: $" + hr.calculateSalary());

OUTPUT: --- Employee Info ---

Name: SHIKHAR

Employee ID: 101

Base Salary: RS.50000.0

Salary: RS.60000.0

Projects: [Banking App, AI Assistant]

--- Employee Info ---

Name: VIRAT

Employee ID: 102

Base Salary: RS.60000.0

Salary: RS.75000.0

VIRAT is conducting a performance review.

--- Employee Info ---

Name: ROHIT

Employee ID: 103

Base Salary: RS.45000.0

Salary: RS.45000.0

You might also like