[go: up one dir, main page]

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

Lab Manual 13 - Updated

Oop labs java in oop

Uploaded by

Hajra Wajid
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)
38 views5 pages

Lab Manual 13 - Updated

Oop labs java in oop

Uploaded by

Hajra Wajid
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

Object Orienting Programming

(Java)

Lab Manual No 06-(Part2)

Dated:
Semester:

2024

Lab Instructor: SHEHARYAR KHAN Page 1


57 | P a g e
1. 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.
2. 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.

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.");
}
}

class Dog extends Animal {


// Implementing the abstract method
void sound() {
System.out.println("The dog barks.");
}
}

public class Main {


public static void main(String[] args) {
Dog myDog = new Dog();
myDog.sound(); // Output: The dog barks.
myDog.eat(); // Output: This animal eats food.
}
}

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.
interface Animal {
void sound();
void eat();
}

class Dog implements Animal {


// Implementing the methods of the interface
public void sound() {
System.out.println("The dog barks.");
}

2|Pa ge
public void eat() {
System.out.println("The dog eats food.");
}
}

public class Main {


public static void main(String[] args) {
Dog myDog = new Dog();
myDog.sound(); // Output: The dog barks.
myDog.eat(); // Output: The dog eats food.
}
}

4. 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.
Example of Functional Interface and Lambda 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
}
}

Abstract vs. Concrete Classes


A concrete class is a class that has a complete implementation for all of its methods and can be instantiated. On the other
hand, an abstract class cannot be instantiated on its own and is used as a base class for other classes.

// 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

You might also like