0% found this document useful (0 votes)
12 views

lab 5 Interface

Uploaded by

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

lab 5 Interface

Uploaded by

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

University of Engineering And Techology,Taxila

COMPUTER ENGINEERING DEPARTMENT

COMPUTER ORGANIZATION AND ARCHITECTURE

Lab Manual 05
Interface

Name: AMNA TARIQ


Roll No: 23-CP-17
Section: Alpha Abstract
Semester:
[Draw your reader in with an engaging abstract. It 3
is typically a short
summary of the document. When you’re ready to
Submit To: Sir Shehriyar add your content, just
click here and start typing.]

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.

Abstract Classes and Methods:


An abstract class is a class that cannot be instantiated and is meant to be
subclassed. It may or may not contain abstract methods, which are methods
without a body. Abstract methods must be implemented by subclasses.

Output:
2

Interfaces and Implementation:


An interface in Java is a reference type, similar to a class, that can contain only constants,
method signatures, default methods, static methods, and nested types. Interfaces cannot contain
instance fields or constructors. Interfaces provide a way to achieve full abstraction and multiple
inheritance in Java.
3

1.Functional Interfaces and Lambda


Expressions (Java 8+):
A functional interface is an interface with only one abstract method. Lambda expressions in
Java 8 provide a clear and concise way to represent one method interface using an
expression.
@FunctionalInterface
interface Greeting {
void sayHello(String name);
}
public class Main {
public static void main(String[] args) {
// Using lambda expression to implement the functional interface
Greeting greeting = (name) -> System.out.println("Hello, " + name);
greeting.sayHello("Alice");
// Output: Hello, Alice } }
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
4

• Abstract Classes cannot be instantiated, while Concrete Classes can be instantiated.

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.

Code:
5

Output:

Task 2: Interface Implementation


Objective: Test students' understanding of interfaces and their ability to
implement them.

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

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.
In the main method, create instances of Rectangle and Circle, calculate their areas, and print the
results

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

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.

Code:
10

Output:

You might also like