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

LAB10

Uploaded by

f23ari18
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)
13 views

LAB10

Uploaded by

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

Aror University of Art, Architecture, Design &

Heritage Sukkur.

________________________________________________________________________________

Department of Artificial Intelligence and Multimedia Gaming


Object Oriented Programming(Spring-2024)
LAB No. 10
Prepared by: Abdul Haseeb Shaikh

Objective of Lab No. 10:


After performing lab10, students will be able to:
o Implement Interfaces in Java
o Achieve Multiple Inheritance in Java
o Use Inheritance among Interfaces
o Use default and static methods of Interfaces

Lab Exercises:
1. Interface Implementation:
 Define an interface Shape with methods calculateArea() and calculatePerimeter().
 Implement the Shape interface with classes Circle and Rectangle.
 Test the implementation by creating instances of Circle and Rectangle and calling
their methods.

2. Multiple Inheritance using Interfaces:


 Define an interface Attack with a method attack() that returns the damage dealt.
 Define an interface Defend with a method defend() that returns the damage reduced.
 Create a class Warrior that implements both Attack and Defend interfaces.
 Implement attack() to return a random value representing the attack power.
 Implement defend() to return a random value representing the defense power.
 Create an instance of Warrior and simulate a battle by calling attack() and defend()
methods, displaying the outcome of each action.

3. Inheritance in Interfaces:
 Define an interface Animal with a method eat().
 Define a sub-interface Mammal that extends Animal and adds a method giveBirth().
 Implement the Mammal interface with a class Dog.
 Test the eat() and giveBirth() methods of Dog.
Aror University of Art, Architecture, Design &
Heritage Sukkur.

________________________________________________________________________________

4. Default Methods in Interfaces:


 Define an interface Logger with a default method log(String message).
 Create a class ConsoleLogger that implements Logger.
 Implement the log() method in ConsoleLogger to print the message to the console.
 Demonstrate the usage of ConsoleLogger to log messages.

5. Static Methods in Interfaces:


 Define an interface MathUtils with a static method double square(double number).
 Implement MathUtils with a class Calculator.
 Implement the square() method in Calculator to return the square of a number.
 Use the square() method of Calculator to calculate and display the square of a given
number.

You might also like