[go: up one dir, main page]

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

56 TANAY JAVA Exp10

Yhgf

Uploaded by

Deadly Ninja
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)
16 views5 pages

56 TANAY JAVA Exp10

Yhgf

Uploaded by

Deadly Ninja
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

Experiment No.

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.

abstract class Animal {


abstract void sound(); // Abstract method
}

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.

void sleep() { // Concrete method


System.out.println("Sleeping...");
}
}

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

class Dog extends Animal {


@Override
void sound() {
System.out.println("Barks");
}
}

// Abstract class
abstract class Animal {
// Abstract method (does not have a body)
abstract void sound();

// Regular method (concrete method with a body)


void eat() {
System.out.println("This animal eats food.");

SIES GRADUATE SCHOOL OF Page 1


TECHNOLOGY
}
}

// Subclass (inherits from Animal)


class Dog extends Animal {
// Providing the implementation of the abstract method
void sound() {
System.out.println("The dog barks");
}
}

public class Main {


public static void main(String[] args) {
Animal myDog = new Dog();
myDog.sound(); // The dog barks
myDog.eat(); // This animal eats food
}
}

 An abstract class can have both abstract and non-abstract methods.


 Subclasses must provide implementations for all abstract methods.
 Abstract classes are useful when you want to define a common base class with some
common functionality and some methods that need to be implemented by child classes.

PROGRAM/CODE:

// Paste code block here.

// Abstract class for Shape


abstract class Shape {
// Abstract method to calculate the area
abstract void calculateArea();
}

// Derived class: Circle


class Circle extends Shape {
double radius;

// Constructor to initialize the radius


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

// Implementing the calculateArea method for Circle


@Override
void calculateArea() {
double area = Math.PI * radius * radius;
System.out.println("Area of Circle: " + area);

SIES GRADUATE SCHOOL OF Page 2


TECHNOLOGY
}
}

// Derived class: Rectangle


class Rectangle extends Shape {
double length, width;

// Constructor to initialize the length and width


Rectangle(double length, double width) {
this.length = length;
this.width = width;
}

// Implementing the calculateArea method for Rectangle


@Override
void calculateArea() {
double area = length * width;
System.out.println("Area of Rectangle: " + area);
}
}

// Derived class: Triangle


class Triangle extends Shape {
double base, height;

// Constructor to initialize the base and height


Triangle(double base, double height) {
this.base = base;
this.height = height;
}

// Implementing the calculateArea method for Triangle


@Override
void calculateArea() {
double area = 0.5 * base * height;
System.out.println("Area of Triangle: " + area);
}
}

public class abstract_demo {


public static void main(String[] args) {
// Creating objects for each shape
Circle circle = new Circle(5.0); // Circle with radius 5.0
Rectangle rectangle = new Rectangle(4.0, 6.0); // Rectangle with length 4.0 and width 6.0
Triangle triangle = new Triangle(3.0, 4.0); // Triangle with base 3.0 and height 4.0

// Calculating and displaying areas


circle.calculateArea(); // Calls Circle's calculateArea method
rectangle.calculateArea(); // Calls Rectangle's calculateArea method
triangle.calculateArea(); // Calls Triangle's calculateArea method

SIES GRADUATE SCHOOL OF Page 3


TECHNOLOGY
}
}

OUTPUT

Conclusion:
We implemented an abstract class program to calculate the area of the triangle.

SIES GRADUATE SCHOOL OF Page 4


TECHNOLOGY
SIES GRADUATE SCHOOL OF Page 5
TECHNOLOGY

You might also like