lab 5 Interface
lab 5 Interface
Lab Manual 05
Interface
Sumaira Sajjad
[Email address]
1
Lab Manual 13
Concept of Abstraction:
Abstraction is the process of hiding the implementation details from the user and only
exposing the essential features of an object. In Java, abstraction can be achieved using
abstract classes and interfaces.
Output:
2
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
4
Code:
5
Output:
Task:
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.
void pause(): This method will print out a message indicating that the object is
paused.
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."
In the main method, create an instance of MusicPlayer, and call the play() and pause()
methods
6
Output:
7
Code:
8
2.Output:
Task:05
3. 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.
9
Code:
10
Output: