Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
42 views

Abstract Classes in Java

Polymorphism concept in java 1

Uploaded by

dennisebenezer
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Abstract Classes in Java

Polymorphism concept in java 1

Uploaded by

dennisebenezer
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Abstract Classes in Java

In the world of object-oriented programming, abstract classes play a crucial role in creating flexible and reusable code. These classes,
declared using the **abstract** keyword, serve as blueprints for subclasses, defining common features and behaviors that can be
inherited and customized. Abstract classes are powerful tools for achieving abstraction, code reusability, and polymorphism in Java.
The Essence of Abstract Classes
1 Uninstantiable Entities 2 Abstract and Concrete 3 Constructors and Static
Methods Members
Abstract classes are not meant to
be instantiated directly. You An abstract class can contain both Although you cannot create
cannot create an object of an abstract methods, which are objects of an abstract class, it can
abstract class. Their purpose is to declared without implementation, still have constructors.
serve as a foundation for concrete and non-abstract methods, which Constructors are used during the
subclasses, which will inherit their have a concrete body of code. initialization of subclasses.
properties and methods. Abstract methods must be Abstract classes can also have
overridden by subclasses, while static members, which are shared
non-abstract methods can be used among all instances of the class
directly or overridden if needed. and its subclasses.

4 Enforcing a Common Interface


Abstract classes act as a template, defining a common interface for a group of related classes. Subclasses are obligated to
implement the abstract methods, ensuring that they adhere to the defined structure and behavior.
Abstract Methods: The Foundation of Inheritance

Definition Subclasses' Responsibility Purpose

An abstract method is declared without Any subclass inheriting an abstract Abstract methods are crucial for
an implementation. It uses the class must provide implementations for enforcing a specific behavior or
**abstract** keyword and is simply a all the abstract methods declared in the functionality that must be implemented
placeholder, a contract that must be parent class. If a subclass doesn't in all subclasses. They allow you to
fulfilled by subclasses. The subclass override all abstract methods, it must define a common pattern without
must override the abstract method and also be declared as abstract, indicating dictating the exact implementation
provide its own implementation. its incompleteness. details.
Benefits of Abstract Classes
Abstraction
Abstract classes help in achieving abstraction by hiding implementation details and exposing only the essential features.
This promotes modularity and makes code easier to understand and maintain.

Code Reusability
By defining common methods in an abstract class, you can reuse those methods across multiple subclasses. This reduces
redundancy and simplifies code development.

Polymorphism
Abstract classes enable polymorphism, which allows you to treat objects of different subclasses as if they were objects of
the abstract class. This makes your code more flexible and adaptable.
Real-World Application: Shape Example
Abstract Class Shape

Abstract Methods calculateArea(), calculatePerimeter()

Subclasses Circle, Square, Rectangle, Triangle

Implementation Each subclass implements the abstract methods, providing


specific calculations for its shape.

In this example, the Shape abstract class defines the common properties and behaviors of all shapes, such as the ability to calculate
area and perimeter. Concrete subclasses like Circle, Square, Rectangle, and Triangle inherit these properties and implement the abstract
methods based on their specific shape characteristics.
abstract class Animal {
abstract void makeSound();
void eat() {
System.out.println("I can eat.");
}
}

class Dog extends Animal {


void makeSound() {
System.out.println("Bark bark");
}
}

public class Main {


public static void main(String[] args) {
Dog d = new Dog();
d.makeSound();
d.eat();
}
}

You might also like