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

Lab Report 08

Uploaded by

mustaneerawanvu
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)
5 views5 pages

Lab Report 08

Uploaded by

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

NAME: ASIM RAZA

ROLL NO: RC-646


SEMESTER: 02
AFTERNOON
Lab Report: 08

Title: Abstract Class and Interface in Java

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:

1. Abstract Methods: These are methods declared without a body, meant to be


implemented by subclasses.
2. Concrete Methods: Methods with an implementation that can be inherited.
3. Can have constructors and fields: An abstract class can have fields and constructors.
4. Inheritance: A subclass must implement the abstract methods of the abstract class.

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:

1. Abstract Methods Only: All methods in an interface are abstract by default.


2. No Implementation: The implementing class must provide the method implementation.
3. Multiple Inheritance: A class can implement multiple interfaces.
4. Cannot have constructors or fields: Interfaces cannot have instance fields or
constructors.

Syntax:

interface InterfaceName {
void methodName();
}

Program
Example: Abstract Class and Interface in Java

Code:
// Abstract Class Example
abstract class Animal {
String name;

abstract void sound();

void sleep() {
System.out.println(name + " is sleeping.");
}
}

class Dog extends Animal {


Dog(String name) {
this.name = name;
}

void sound() {
System.out.println(name + " barks");
}
}

class Cat extends Animal {


Cat(String name) {
this.name = name;
}

void sound() {
System.out.println(name + " meows");
}
}

// Interface Example
interface Shape {
double area();
double perimeter();
}

class Circle implements Shape {


double radius;

Circle(double radius) {
this.radius = radius;
}

public double area() {


return Math.PI * radius * radius;
}

public double perimeter() {


return 2 * Math.PI * radius;
}
}

class Rectangle implements Shape {


double length, width;

Rectangle(double length, double width) {


this.length = length;
this.width = width;
}

public double area() {


return length * width;
}

public double perimeter() {


return 2 * (length + width);
}
}

public class Main {


public static void main(String[] args) {
System.out.println("Abstract Class Example:");
Dog dog = new Dog("Buddy");
dog.sound();
dog.sleep();

Cat cat = new Cat("Whiskers");


cat.sound();
cat.sleep();

System.out.println("\nInterface Example:");

Shape circle = new Circle(5);


System.out.println("Circle Area: " + circle.area());
System.out.println("Circle Perimeter: " + circle.perimeter());

Shape rectangle = new Rectangle(4, 6);


System.out.println("Rectangle Area: " + rectangle.area());
System.out.println("Rectangle Perimeter: " + rectangle.perimeter());
}
}

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.

You might also like