Lab exercise 1: Implement single and multilevel inheritance (Employee)
(Inheritance hierarchy) class Employee { int empId;
String name;
Employee(int empId, String name)
{ [Link] = empId; [Link]
= name;
}
void displayEmployee() {
[Link]("Employee ID: " + empId);
[Link]("Employee Name: " + name);
}
}
class Manager extends Employee {
double salary;
Manager(int empId, String name, double salary)
{ super(empId, name); [Link] = salary;
}
void displayManager() {
displayEmployee();
[Link]("Salary: " + salary);
}
}
class SeniorManager extends Manager {
String department;
SeniorManager(int empId, String name, double salary, String department)
{ super(empId, name, salary); [Link] = department;
}
void displaySeniorManager() {
displayManager();
[Link]("Department: " + department);
}
}
public class Main { public static void
main(String[] args) {
[Link]("Bibhash Raut,25BTRCN2231");
[Link]("----- Single Inheritance -----");
Manager m = new Manager(228, "Oshim", 100000);
[Link]();
[Link]("\n----- Multilevel Inheritance -----");
SeniorManager sm = new SeniorManager(102, "Rahul", 70000, "CA");
[Link]();
}
}
OUTPUT
Bibhash Raut,25BTRCN231
----- Single Inheritance -----
Employee ID: 228
Employee Name: Bibhash
Salary: 100000.0
----- Multilevel Inheritance -----
Employee ID: 102
Employee Name: Krishna
Salary: 70000.0
Department: CA
=== Code Execution Successful ===
Lab exercise 2: Demonstrate method overriding in subclass (Runtime polymorphism)
class Employee {
void work() {
[Link]("Employee is working.");
}
}
class Manager extends Employee {
void work() {
[Link]("Manager lead the team");
}
}
public class Main { public static void
main(String[] args) {
[Link]("Bibhash Raut,25BTRCN231");
Employee emp = new Manager();
[Link]();
}
}
OUTPUT
Bibhash Raut,25BTRCN231
Manager lead the team
=== Code Execution Successful ===
Lab exercise 3: Use super to access parent class members (Keyword usage)
class Employee { String
name = "Bibhash"; int
salary = 100000; void
display() {
[Link]("Employee Name: " + name);
[Link]("Employee Salary: " + salary);
}
}
class Manager extends Employee
{ String name = "Vivek"; int
salary = 80000; void
showDetails() {
[Link]("Manager Name: " + name);
[Link]("Manager Salary: " + salary);
[Link]("\nAccessing Parent Class Members using super:");
[Link]("Employee Name: " + [Link]);
[Link]("Employee Salary: " + [Link]);
[Link]("\nCalling Parent Class Method:");
[Link]();
}
}
public class Main { public static void
main(String[] args) {
[Link]("Bibhash Raut,25BTRCN231");
Manager m = new Manager(); [Link]();
}
}
OUTPUT
Bibhash Raut,25BTRCN231
Manager Name: Dotta kala Raju
Manager Salary: 80000
Accessing Parent Class Members using super:
Employee Name: Bibhash
Employee Salary: 100000
Calling Parent Class Method:
Employee Name: Bibhash
Employee Salary: 100000
=== Code Execution Successful ===
Lab exercise 4: Write a program that uses dynamic method dispatch (Late binding
and reference polymorphism)
Program 1: Late Binding (Dynamic Method Dispatch)
class Employee {
void work() {
[Link]("Employee is working.");
}
}
class Manager extends Employee {
void work() {
[Link]("Manager lead the team");
}
}
public class Main {
public static void main(String[] args) {
[Link]("Bibhash Raut,25BTRCN231");
Employee emp = new Manager(); [Link]();
}
}
OUTPUT
Bibhash Raut,25BTRCN231
Manager lead the team
=== Code Execution Successful ===
Program 2: Reference Polymorphism
class Employee {
void display() {
[Link]("This is an Employee.");
}
}
class Developer extends Employee {
void display() {
[Link]("This is a software developer.");
}
}
public class Main {
public static void main(String[] args) {
[Link](“Bibhash Raut,25BTRCN231"); Employee
emp;
emp = new Employee();
[Link]();
emp = new Developer();
[Link]();
}
}
OUTPUT
Bibhash Raut,25BTRCN231
This is an Employee.
This is a software developer.
=== Code Execution Successful ===