56 TANAY JAVA Exp10
56 TANAY JAVA Exp10
10
Abstraction in Java
Name: Tanay More Date:14/10
Roll No. 56 Batch G3
Aim: Write a abstract class program to calculate area of circle, rectangle and triangle.
Theory:
In Java, an abstract class is a class that cannot be instantiated directly and is intended to be
subclassed. It can contain both abstract methods (which are declared without implementation) and
concrete methods (with implementation). The primary purpose of an abstract class is to provide a
base class with common functionality while enforcing that subclasses must implement certain
methods.
Key Features of an Abstract Class:
1. Abstract Methods: These are methods that are declared without a body (abstract keyword)
and must be implemented by subclasses.
Concrete Methods: Abstract classes can also have fully implemented (concrete) methods
abstract class Animal {
Subclasses Must Implement Abstract Methods: Any class that extends an abstract class must
either implement all abstract methods or be declared abstract itself.
Cannot be Instantiated: You cannot create an instance of an abstract class directly. For example:
Animal a = new Animal(); // Error: Animal is abstract; cannot be instantiated
// Abstract class
abstract class Animal {
// Abstract method (does not have a body)
abstract void sound();
PROGRAM/CODE:
OUTPUT
Conclusion:
We implemented an abstract class program to calculate the area of the triangle.