Experiment No: 5
AIM: To demonstrate the use of abstract classes and interfaces.
Date:
CO mapped: CO-2
Objectives:
a) To understand the purpose and usage of abstract classes and interfaces in object-oriented
programming. Develop the ability to design and implement abstract classes and interfaces
effectively to promote code reusability, ensure consistent behavior in class hierarchies, and
facilitate the development of flexible and extensible software systems.
b) Abstract classes and interfaces are important OOP concepts that allow you to define
common contracts and behaviors for classes. Achieving this objective will enable you to use
these tools to create more modular and maintainable software, especially when dealingwith
class hierarchies and multiple implementations.
Background:
A class that is declared as abstract is known as an abstract class. It can have abstract and non-
abstract methods. It needs to be extended and its method implemented. It cannot be instantiated.
Points to Remember
● An abstract class must be declared with an abstract keyword.
● It can have abstract and non-abstract methods.
● It cannot be instantiated.
● It can have constructors and static methods also.
● It can have final methods which will force subclass not to change the body of the method.
An interface in Java is a blueprint of a class. It has static constants and abstract methods. The
interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the
Java interface, not the method body. It is used to achieve abstraction and multiple inheritance in
Java. In other words, you can say that interfaces can have abstract methods and variables. It
cannot have a method body.
Practical questions:
1. Describe abstract class called Shape which has three subclasses say Triangle, Rectangle,
Circle. Define one method area() in the abstract class and override this area() in these three
subclasses to calculate for specific object, i.e., area() of Triangle subclass should calculate
area of triangle etc. Same for Rectangle and Circle.
2. Write a program that demonstrates the instance of operator. Declare interfaces I1 and I2.
Interface I3 extends both of these interfaces. Also declare interface I4. Class X implements
I3. Class W extends X and implements I4. Create an object of class W. Use the instance of
operator to test if that object implements each of the interfaces and is of type X.
3. Write a java program to implement an interface called Exam with a method Pass (int mark)
that returns a boolean. Write another interface called Classify with a method Division (int
average) which returns a String. Write a class called Result which implements both Exam
and Classify. The Pass method should return true if the mark is greater than or equal to 50
else false. The Division method must return "First" when the parameter average is 60 or
more, "Second" when average is 50 or more but below 60, "No division" when average is
less than 50.
63
Observations:
1. Describe abstract class called Shape which has three subclasses say Triangle, Rectangle,
Circle. Define one method area() in the abstract class and override this area() in these three
subclasses to calculate for specific object, i.e., area() of Triangle subclass should calculate
area of triangle etc. Same for Rectangle and Circle.
Input:
// Abstract class Shape
abstract class Shape {
// Abstract method to calculate area
public abstract double area();
}
// Subclass Triangle
class Triangle extends Shape {
private double base;
private double height;
// Constructor
public Triangle(double base, double height) {
[Link] = base;
[Link] = height;
}
// Override method to calculate area of triangle
@Override
public double area() {
return 0.5 * base * height;
}
}
// Subclass Rectangle
class Rectangle extends Shape {
private double length;
private double width;
// Constructor
public Rectangle(double length, double width) {
[Link] = length;
[Link] = width;
}
// Override method to calculate area of rectangle
@Override
public double area() {
return length * width;
}
}
64
// Subclass Circle
class Circle extends Shape {
private double radius;
// Constructor
public Circle(double radius) {
[Link] = radius;
}
// Override method to calculate area of circle
@Override
public double area() {
return [Link] * radius * radius;
}
}
public class TestShapes {
public static void main(String[] args) {
// Creating objects of different shapes
Triangle triangle = new Triangle(8, 4);
Rectangle rectangle = new Rectangle(7, 3);
Circle circle = new Circle(4);
// Displaying areas of shapes
[Link]("Area of Triangle: " + [Link]());
[Link]("Area of Rectangle: " + [Link]());
[Link]("Area of Circle: " + [Link]());
}
}
Output:
2. Write a program that demonstrates the instance of operator. Declare interfaces I1 and I2.
Interface I3 extends both of these interfaces. Also declare interface I4. Class X implements
I3. Class W extends X and implements I4. Create an object of class W. Use the instance of
operator to test if that object implements each of the interfaces and is of type X.
Input:
// Declare interfaces
interface I1 {}
65
interface I2 {}
interface I3 extends I1, I2 {}
interface I4 {}
// Class X implements interface I3
class X implements I3 {}
// Class W extends X and implements interface I4
class W extends X implements I4 {}
public class TestInstanceof {
public static void main(String[] args) {
// Create an object of class W
W w = new W();
// Check if the object implements each interface and is of type X
if (w instanceof I1) {
[Link]("Object of class W implements interface I1");
}
if (w instanceof I2) {
[Link]("Object of class W implements interface I2");
}
if (w instanceof I3) {
[Link]("Object of class W implements interface I3");
}
if (w instanceof I4) {
[Link]("Object of class W implements interface I4");
}
if (w instanceof X) {
[Link]("Object of class W is of type X");
}
}
}
Output:
3. Write a java program to implement an interface called Exam with a method Pass (int mark)
that returns a boolean. Write another interface called Classify with a method Division (int
average) which returns a String. Write a class called Result which implements both Exam
and Classify. The Pass method should return true if the mark is greater than or equal to 50
66
else false. The Division method must return "First" when the parameter average is 60 or
more, "Second" when average is 50 or more but below 60, "No division" when average is
less than 50.
Input:
// Interface Exam
interface Exam {
boolean Pass(int mark);
}
// Interface Classify
interface Classify {
String Division(int average);
}
// Class Result implementing both Exam and Classify interfaces
class Result implements Exam, Classify {
// Implementing Pass method from Exam interface
@Override
public boolean Pass(int mark) {
return mark >= 50;
}
// Implementing Division method from Classify interface
@Override
public String Division(int average) {
if (average >= 60) {
return "First";
} else if (average >= 50 && average < 60) {
return "Second";
} else {
return "No division";
}
}
}
public class TestResult {
public static void main(String[] args) {
// Create an object of class Result
Result result = new Result();
// Test the Pass method
[Link]("Pass result for mark 70: " + [Link](70)); // true
[Link]("Pass result for mark 40: " + [Link](40)); // false
// Test the Division method
67
[Link]("Division result for average 65: " + [Link](65));
[Link]("Division result for average 55: " + [Link](55));
[Link]("Division result for average 40: " + [Link](40));
division
}
}
Output:
Conclusion:
Quiz: (Sufficient space to be provided for the answers)
1. Explain how interfaces promote the concept of multiple inheritance in OOP.
2. What is an interface, and how does it differ from an abstract class?
3. When would you choose to use an abstract class over an interface, and vice versa, in your software
design?
4. Can a class implement multiple interfaces? If so, what benefits does this provide?
5. Can you declare an interface method static? Justify your answer.
Suggested Reference:
1. [Link]
2. [Link]
3. [Link]
4. [Link]
References used by the students:
[Link]
68
Rubric wise marks obtained:
Rubrics Criteria Need Good Excellent Total
Improvement
Marks Design of Program has Program has slight Program is
logic (4) significant logic errors that do logically well
logic errors. no significantly designed (3)
Correct (1) affect the results
output (4) (2)
Output has
Mock viva multiple Output has minor Program
test (2) errors. (1) errors. (2) displays correct
output with no
Delayed & Partially correct errors (3)
only few response (1)
correct All questions
answers (1) responded
Correctly (2)
Signature of Faculty:
69