DEV Community

Deepikandas
Deepikandas

Posted on

#17 Known is a Drop! Abstraction in JAVA

What is Abstraction?
Abstraction = hiding implementation details and showing only essential features.

How Java supports Abstraction

  1. Abstract Classes
  2. Interfaces

Abstract class:

  • An abstract class is a class that is declared abstract it may or may not include abstract methods.

  • Abstract classes cannot be instantiated, but they can be subclassed.
    Abstract Method:
    An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon)

Can fields be abstract?

❌ No.
In Java, only methods can be abstract.
Fields always have storage and type, so there’s no way to leave them “unimplemented.”
Can Abstract Class have Constructors?
Abstract class cannot be instantiated directly
Vehicle v = new Vehicle(100); // ❌ Compile-time error

Constructors are called when subclass is created
They initialize fields of the abstract class

Constructor types allowed:
Default constructor ✅
Parameterized constructor ✅
Cannot make constructor abstract

❌ abstract Vehicle() { } → ❌ Not allowed

Only methods can be abstract, not constructors

Subclasses of Abstract class:

So yes, each subclass writes its own definition for abstract methods.
When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class.
here ,abstract means “must override
However, if it does not, then the subclass must also be declared abstract.
One-liner:
Abstract class = contract + blueprint → each subclass fulfills contract, but user code works on abstract type.


Access Modifiers in abstract Methods:

Top comments (0)