Lab Manual 13 - Updated
Lab Manual 13 - Updated
(Java)
Dated:
Semester:
2024
Example :
abstract class Animal {
// Abstract method (does not have a body)
abstract void sound();
// Regular method
void eat() {
System.out.println("This animal eats food.");
}
}
2|Pa ge
public void eat() {
System.out.println("The dog eats food.");
}
}
// Abstract class
abstract class Shape {
abstract void draw(); // Abstract method
}
// Concrete class
class Circle extends Shape {
// Implementing the abstract method
void draw() {
System.out.println("Drawing a circle.");
}
}
3|Pa ge
public class Main {
public static void main(String[] args) {
Circle circle = new Circle();
circle.draw(); // Output: Drawing a circle.
}
}
In this example, Shape is an abstract class with an abstract method draw(). The Circle class is a concrete class that
provides the implementation for the draw() method.
Summary:
• Abstraction hides complexity by providing a simpler interface.
• Abstract Classes are partially implemented classes meant to be extended.
• Interfaces define methods that must be implemented by classes that "implement" the interface.
• Functional Interfaces and Lambda Expressions allow for cleaner and more concise code in Java 8+.
• Abstract Classes cannot be instantiated, while Concrete Classes can be instantiated.
Tasks:
Task 1: Abstract Class and Method Implementation
Objective: Test students' ability to define and implement abstract classes and methods.
Task:
1. Create an abstract class called Vehicle with the following properties and methods:
o A string property name.
o An abstract method move() that prints out the movement of the vehicle.
2. Create two subclasses Car and Bicycle that extend the Vehicle class.
o Implement the move() method in each subclass to print out "The car drives on roads." for Car and "The
bicycle is pedaled on paths." for Bicycle.
3. In the main method, instantiate objects of Car and Bicycle and call their move() methods.
Task 2: Interface Implementation
Objective: Test students' understanding of interfaces and their ability to implement them.
Task:
1. Define an interface called Playable with the following methods:
o void play(): This method will print out a message indicating that the object is playing.
o void pause(): This method will print out a message indicating that the object is paused.
2. Create a class called MusicPlayer that implements the Playable interface.
o Implement the play() method to print "Playing music."
o Implement the pause() method to print "Music paused."
3. In the main method, create an instance of MusicPlayer, and call the play() and pause() methods.
Task 3: Functional Interface and Lambda Expression
Objective: Assess students' ability to use functional interfaces and lambda expressions.
Task:
1. Create a functional interface named Calculator with a method int operate(int a, int b).
2. Use a lambda expression to implement the Calculator interface for the following operations:
o Addition
o Subtraction
o Multiplication
3. In the main method, perform the following operations using the lambda expressions:
4|Pa ge
o Add 5 and 3
o Subtract 5 from 3
o Multiply 5 and 3
o Print out the results.
Task 4: Abstract vs. Concrete Classes
Objective: Test students' understanding of the difference between abstract and concrete classes.
Task:
1. Create an abstract class Shape with an abstract method double area().
2. Create two concrete classes Rectangle and Circle that extend the Shape class.
o Implement the area() method in each class:
▪ For Rectangle, the area should be length * width.
▪ For Circle, the area should be π * radius^2.
3. In the main method, create instances of Rectangle and Circle, calculate their areas, and print the results.
Task 5: Polymorphism with Abstract Classes
Objective: Test the understanding of polymorphism through abstract classes.
Task:
1. Create an abstract class Employee with an abstract method double calculateSalary() and a property name.
2. Create two subclasses Manager and Developer that extend Employee.
o Manager has a fixed salary plus a bonus.
o Developer is paid by the hour.
3. In the main method, create an array of Employee objects, including instances of Manager and Developer.
o Use a loop to iterate through the array and print out each employee's name and calculated salary.
5|Pa ge