Lab Report 08
Lab Report 08
Objective
To understand and implement the concepts of Abstract Class and Interface in Java, and to
differentiate their usage and characteristics.
Theory
Abstract Class
An Abstract Class is a class that cannot be instantiated on its own. It is used to define common
methods and properties that will be inherited by subclasses. An abstract class may contain both
abstract methods (without implementation) and concrete methods (with implementation).
Key Points:
Syntax:
abstract class ClassName {
abstract void methodName();
}
Interface
An Interface in Java is a contract that defines a set of abstract methods that any implementing
class must provide. An interface can only contain abstract methods and static final fields
(constants).
Key Points:
Syntax:
interface InterfaceName {
void methodName();
}
Program
Example: Abstract Class and Interface in Java
Code:
// Abstract Class Example
abstract class Animal {
String name;
void sleep() {
System.out.println(name + " is sleeping.");
}
}
void sound() {
System.out.println(name + " barks");
}
}
void sound() {
System.out.println(name + " meows");
}
}
// Interface Example
interface Shape {
double area();
double perimeter();
}
Circle(double radius) {
this.radius = radius;
}
System.out.println("\nInterface Example:");
Output:
Abstract Class Example:
Buddy barks
Buddy is sleeping.
Whiskers meows
Whiskers is sleeping.
Interface Example:
Circle Area: 78.53981633974483
Circle Perimeter: 31.41592653589793
Rectangle Area: 24.0
Rectangle Perimeter: 20.0
Explanation
Abstract Class:
1. The Animal class is abstract, containing both abstract methods (like sound()) and a
concrete method (sleep()).
2. The Dog and Cat classes extend Animal and implement the abstract method sound() as
required.
3. Abstract classes can provide common functionality (like sleep()) and leave other
methods abstract for subclasses to implement.
Interface:
1. The Shape interface defines two abstract methods: area() and perimeter().
2. The Circle and Rectangle classes implement the Shape interface and provide specific
implementations for calculating area and perimeter.
3. Interfaces allow multiple classes to share a common contract without forcing inheritance
from a specific class.
Key Points
1. Abstract Class:
o May have both abstract and concrete methods.
o A class can extend only one abstract class (single inheritance).
o Can contain fields and constructors.
2. Interface:
o Contains only abstract methods (until Java 8, later default methods can be added).
o A class can implement multiple interfaces (multiple inheritance).
o Cannot contain fields or constructors.
3. When to Use:
o Use abstract classes when you have common functionality that multiple
subclasses will share.
o Use interfaces when you want to define a contract that can be implemented by
any class, regardless of its position in the class hierarchy.
Conclusion
In this lab, we explored the use of Abstract Classes and Interfaces in Java.
Abstract Classes are useful when you want to share common functionality among
related classes while allowing some methods to be implemented by subclasses.
Interfaces define a contract of methods that any class can implement, promoting a
polymorphic design.
These concepts are crucial for building flexible and maintainable object-oriented software.