0% found this document useful (0 votes)
4 views5 pages

Abstraction Lab PGM

The document provides examples of abstraction in Java through an abstract class 'Employee' with subclasses 'Manager', 'Worker', and 'SalesPerson', each implementing salary calculations and employee details display. It also includes an abstract class 'Shape' with concrete implementations for 'Rectangle' and 'Circle', demonstrating area calculations. Additionally, an interface 'Vehicle' is defined and implemented by the 'Car' class, showcasing method definitions for starting and stopping a vehicle.

Uploaded by

vaarsh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Abstraction Lab PGM

The document provides examples of abstraction in Java through an abstract class 'Employee' with subclasses 'Manager', 'Worker', and 'SalesPerson', each implementing salary calculations and employee details display. It also includes an abstract class 'Shape' with concrete implementations for 'Rectangle' and 'Circle', demonstrating area calculations. Additionally, an interface 'Vehicle' is defined and implemented by the 'Car' class, showcasing method definitions for starting and stopping a vehicle.

Uploaded by

vaarsh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Program to Create an abstract class “Employee” with abstract methods “calculateSalary()” and
“displayEmployeeDetails()”.

abstract class Employee


{
protected String name;
protected int age;
protected String gender;

public Employee(String name, int age, String gender)


{
this.name = name;
this.age = age;
this.gender = gender;
}

public abstract double calculateSalary();


public abstract void displayEmployeeDetails();
}

class Manager extends Employee


{
protected double baseSalary;
protected double bonus;

public Manager(String name, int age, String gender, double baseSalary, double bonus)
{
super(name, age, gender);
this.baseSalary = baseSalary;
this.bonus = bonus;
}

public double calculateSalary()


{
return baseSalary + bonus;
}

public void displayEmployeeDetails() {


System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Gender: " + gender);
System.out.println("Base Salary: " + baseSalary);
System.out.println("Bonus: " + bonus);
System.out.println("Total Salary: " + calculateSalary());
}
}

class Worker extends Employee


{
protected double hourlyRate;
protected int hoursWorked;

public Worker(String name, int age, String gender, double hourlyRate, int hoursWorked)
{

1
super(name, age, gender);
this.hourlyRate = hourlyRate;
this.hoursWorked = hoursWorked;
}

public double calculateSalary()


{
return hourlyRate * hoursWorked;
}

public void displayEmployeeDetails()


{
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Gender: " + gender);
System.out.println("Hourly Rate: " + hourlyRate);
System.out.println("Hours Worked: " + hoursWorked);
System.out.println("Total Salary: " + calculateSalary());
}
}

class SalesPerson extends Manager


{
protected double commissionRate;

public SalesPerson(String name, int age, String gender, double baseSalary, double bonus, double
commissionRate)
{
super(name, age, gender, baseSalary, bonus);
this.commissionRate = commissionRate;
}

public double calculateSalary()


{
return super.calculateSalary() + (super.calculateSalary() * commissionRate);
}

public void displayEmployeeDetails()


{
super.displayEmployeeDetails();
System.out.println("Commission Rate: " + commissionRate);
System.out.println("Total Salary (including commission): " + calculateSalary());
}
}

class AbstractExample
{
public static void main(String[] args)
{
Employee manager = new Manager("John", 40, "Male", 5000, 1000);
Employee worker = new Worker("Mary", 25, "Female", 20, 160);
Employee salesPerson = new SalesPerson("Bob", 45, "Male", 6000, 1500, 0.05);

manager.displayEmployeeDetails();

2
System.out.println();

worker.displayEmployeeDetails();
System.out.println();

salesPerson.displayEmployeeDetails();
}
}

2. Another example of abstraction

abstract class Shape


{
// Abstract method (does not have a body)
public abstract double area();

// Concrete method (has a body)


public void display()
{
System.out.println("This is a shape.");
}
}

class Rectangle extends Shape


{
private double length;
private double width;

public Rectangle(double length, double width)


{
this.length = length;
this.width = width;
}

// Implementing abstract method


public double area()
{
return length * width;
}
}

class Circle extends Shape


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

// Implementing abstract method


public double area() {
return Math.PI * radius * radius;
}
}

3
public class Main
{
public static void main(String[] args)
{
Shape rect = new Rectangle(5, 4);
Shape circle = new Circle(3);

rect.display();
System.out.println("Area of rectangle: " + rect.area());

circle.display();
System.out.println("Area of circle: " + circle.area());
}
}

3. Example of Java Interface

public class Main


{
// Define the Vehicle interface
public interface Vehicle
{
// Abstract method to start the vehicle
void start();

// Abstract method to stop the vehicle


void stop();
}

// Implement the Vehicle interface in the Car class


public static class Car implements Vehicle
{
@Override
public void start()
{
System.out.println("The car is starting.");
}

@Override
public void stop()
{
System.out.println("The car is stopping.");
}
}

public static void main(String[] args)


{
// Create an instance of Car
Vehicle myCar = new Car();

// Call methods defined in the interface


myCar.start(); // Output: The car is starting.
myCar.stop(); // Output: The car is stopping.

4
}
}

You might also like