You are on page 1of 9

Experiment 4: Inheritance and Interfaces Date:

Aim: To implement the following Java Programs

1) An educational institute wishes to maintain a database of its employees. The database application would
involve a number of classes whose hierarchical relationships are shown below. The figure shows the
minimum information required for each class. Specify all the classes and define methods to initialise data
members and display individual information.

Theory:
 In Java, inheritance is a fundamental concept in object-oriented programming
(OOP) where a class (subclass or child class) can inherit properties and
behaviors from another class (superclass or parent class). This allows for
code reuse, abstraction, and the creation of hierarchical relationships
between classes.

1. Inheritance: Inheritance in Java allows a subclass to inherit fields and


methods from its superclass. This enables the subclass to reuse code and
extend the functionality of the superclass. Subclasses can also override
methods from the superclass to provide specific implementations.
2. Types of Inheritance:
 Single Inheritance: A subclass inherits from only one superclass.
 Multilevel Inheritance: A subclass inherits from another subclass,
creating a hierarchy.
 Hierarchical Inheritance: Multiple subclasses inherit from the same
superclass.
 Multiple Inheritance (through interfaces): Java doesn't support
multiple inheritance of classes, but it supports multiple inheritance
through interfaces.
Program:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Staff {
private int code;
private String name;
Staff(int code, String name) {
this.code = code;
this.name = name;
}

void displayInfo() {
System.out.println("Code: " + getCode() + "\nName: " + getName());
}

public int getCode() {


return code;
}

public String getName() {


return name;
}
}

class Teacher extends Staff {


private String specialization;
private int noOfPublications;
private int salary; // New member

Teacher(int code, String name, String specialization, int noOfPublications, int salary) {
super(code, name);
this.specialization = specialization;
this.noOfPublications = noOfPublications;
this.salary = salary;
}

void displayInfo() {
super.displayInfo();
System.out.println("Specialization: " + getSpecialization() + "\nPublications: " +
getNoOfPublications() +
"\nSalary: " + getSalary());
}

public String getSpecialization() {


return specialization;
}
public int getNoOfPublications() {
return noOfPublications;
}

public int getSalary() {


return salary;
}
}

class Typist extends Staff {


private int speed;
private int salary; // New member

Typist(int code, String name, int speed, int salary) {


super(code, name);
this.speed = speed;
this.salary = salary;
}

void displayInfo() {
super.displayInfo();
System.out.println("Speed: " + getSpeed() + "\nSalary: " + getSalary());
}

public int getSpeed() {


return speed;
}

public int getSalary() {


return salary;
}
}

class RegularTypist extends Typist {


RegularTypist(int code, String name, int speed, int salary) {
super(code, name, speed, salary);
}
}

class CasualTypist extends Typist {


private int wages;

CasualTypist(int code, String name, int speed, int salary, int wages) {
super(code, name, speed, salary);
this.wages = wages;
}

void displayInfo() {
super.displayInfo();
System.out.println("Wages: " + getWages());
}

public int getWages() {


return wages;
}
}

class Officer extends Staff {


private int grade;
private int salary; // New member

Officer(int code, String name, int grade, int salary) {


super(code, name);
this.grade = grade;
this.salary = salary;
}

void displayInfo() {
super.displayInfo();
System.out.println("Grade: " + getGrade() + "\nSalary: " + getSalary());
}

public int getGrade() {


return grade;
}

public int getSalary() {


return salary;
}
}

class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
boolean exit = false;

while (!exit) {
System.out.println("Select employee type:");
System.out.println("1. Teacher\n2. Typist\n3. Officer\n4. Exit");
System.out.print("Enter your choice: ");
int choice = Integer.parseInt(reader.readLine());

Staff employee = null;

switch (choice) {
case 1:
employee = createTeacher(reader);
break;
case 2:
employee = createTypist(reader);
break;
case 3:
employee = createOfficer(reader);
break;
case 4:
exit = true;
break;
default:
System.out.println("Invalid choice");
break;
}

if (employee != null && !exit) {


System.out.println("\nEmployee Information:");
employee.displayInfo();
}
}

// Close the BufferedReader


reader.close();
}

private static Teacher createTeacher(BufferedReader reader) throws IOException {


System.out.print("Code: ");
int teacherCode = Integer.parseInt(reader.readLine());
System.out.print("Name: ");
String teacherName = reader.readLine();
System.out.print("Specialization: ");
String specialization = reader.readLine();
System.out.print("Number of Publications: ");
int publications = Integer.parseInt(reader.readLine());
System.out.print("Salary: ");
int salary = Integer.parseInt(reader.readLine());

return new Teacher(teacherCode, teacherName, specialization, publications, salary);


}

private static Typist createTypist(BufferedReader reader) throws IOException {


System.out.println("Select typist type:");
System.out.println("1. Regular Typist\n2. Casual Typist");
System.out.print("Enter your choice: ");
int typistChoice = Integer.parseInt(reader.readLine());

switch (typistChoice) {
case 1:
return createRegularTypist(reader);
case 2:
return createCasualTypist(reader);
default:
System.out.println("Invalid typist choice");
return null;
}
}

private static RegularTypist createRegularTypist(BufferedReader reader) throws IOException {


System.out.print("Code: ");
int typistCode = Integer.parseInt(reader.readLine());
System.out.print("Name: ");
String typistName = reader.readLine();
System.out.print("Speed: ");
int speed = Integer.parseInt(reader.readLine());
System.out.print("Salary: ");
int salary = Integer.parseInt(reader.readLine());

return new RegularTypist(typistCode, typistName, speed, salary);


}

private static CasualTypist createCasualTypist(BufferedReader reader) throws IOException {


System.out.print("Code: ");
int typistCode = Integer.parseInt(reader.readLine());
System.out.print("Name: ");
String typistName = reader.readLine();
System.out.print("Speed: ");
int speed = Integer.parseInt(reader.readLine());
System.out.print("Salary: ");
int salary = Integer.parseInt(reader.readLine());
System.out.print("Wages: ");
int wages = Integer.parseInt(reader.readLine());

return new CasualTypist(typistCode, typistName, speed, salary, wages);


}

private static Officer createOfficer(BufferedReader reader) throws IOException {


System.out.print("Code: ");
int officerCode = Integer.parseInt(reader.readLine());
System.out.print("Name: ");
String officerName = reader.readLine();
System.out.print("Grade: ");
int grade = Integer.parseInt(reader.readLine());
System.out.print("Salary: ");
int salary = Integer.parseInt(reader.readLine());
return new Officer(officerCode, officerName, grade, salary);
}
}
Output:
java -cp /tmp/5vU66oOWDA Main
Select employee type:
1. Teacher
2. Typist
3. Officer
4. Exit
Enter your choice: 2
Select typist type:
1. Regular Typist
2. Casual Typist
Enter your choice: 1
Code: 42
Name: Josmon
Speed: 420
Salary: 10000

Employee Information:
Code: 42
Name: Josmon
Speed: 420
Salary: 10000
Select employee type:
1. Teacher
2. Typist
3. Officer
4. Exit
Enter your choice: 3
Code: 14
Name: Om
Grade: 2
Salary: 10000

Employee Information:
Code: 14
Name: Om
Grade: 2
Salary: 10000
Select employee type:
1. Teacher
2. Typist
3. Officer
4. Exit
Enter your choice: 4
2) Write a Java program to demonstrate the use of an interface.

Theory:
1. Interfaces: An interface in Java is a reference type that can contain only abstract methods, default methods,
static methods, constant variables, and nested types. Interfaces provide a way to achieve abstraction and
multiple inheritance in Java. A class can implement one or more interfaces, which allows it to inherit abstract
methods from those interfaces and define its specific behavior.
 Abstract Methods: These are methods declared in an interface without any implementation. Classes
implementing the interface must provide implementations for these methods.
 Default Methods: Introduced in Java 8, default methods provide a way to add new methods to
interfaces without breaking existing implementations. These methods have a default implementation
that can be overridden by implementing classes.
 Static Methods: Interfaces can also contain static methods, which are not inherited by implementing
classes.
 Constant Variables: Interfaces can declare constants, which are implicitly public, static, and final.
Interfaces in Java serve as contracts, defining a set of methods that implementing classes must provide.
They facilitate loose coupling and polymorphism, allowing different classes to be treated
interchangeably based on their common interfaces.

Program:

interface Shape {
double calculateArea();
void display();
}

class Circle implements Shape {


private double radius;

Circle(double radius) {
this.radius = radius;
}

@Override
public double calculateArea() {
return Math.PI * radius * radius;
}

@Override
public void display() {
System.out.println("Circle - Radius: " + radius + ", Area: " + calculateArea());
}
}

class Rectangle implements Shape {


private double length;
private double width;
Rectangle(double length, double width) {
this.length = length;
this.width = width;
}

@Override
public double calculateArea() {
return length * width;
}

@Override
public void display() {
System.out.println("Rectangle - Length: " + length + ", Width: " + width + ", Area: " + calculateArea());
}
}

public class InterfaceExample {


public static void main(String[] args) {
Circle circle = new Circle(5.0);
Rectangle rectangle = new Rectangle(4.0, 6.0);

circle.display();
rectangle.display();
}
}
Output:
Circle - Radius: 5.0, Area: 78.53981633974483
Rectangle - Length: 4.0, Width: 6.0, Area: 24.0

Conclusion : The implementation of Inheritance and Interfaces was done successfully.

You might also like