Professional Documents
Culture Documents
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.
void displayInfo() {
System.out.println("Code: " + getCode() + "\nName: " + getName());
}
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());
}
void displayInfo() {
super.displayInfo();
System.out.println("Speed: " + getSpeed() + "\nSalary: " + getSalary());
}
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());
}
void displayInfo() {
super.displayInfo();
System.out.println("Grade: " + getGrade() + "\nSalary: " + getSalary());
}
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());
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;
}
switch (typistChoice) {
case 1:
return createRegularTypist(reader);
case 2:
return createCasualTypist(reader);
default:
System.out.println("Invalid typist choice");
return null;
}
}
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();
}
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());
}
}
@Override
public double calculateArea() {
return length * width;
}
@Override
public void display() {
System.out.println("Rectangle - Length: " + length + ", Width: " + width + ", Area: " + calculateArea());
}
}
circle.display();
rectangle.display();
}
}
Output:
Circle - Radius: 5.0, Area: 78.53981633974483
Rectangle - Length: 4.0, Width: 6.0, Area: 24.0