Java – Abstract Classes

Home/Java - Tutorial/Java - Inheritance/Java – Abstract Classes
Java – Abstract Classes 2017-06-13T00:21:44+00:00

Abstract Classes in java:

When the keyword abstract appears in a class definition, it means that zero or more of it’s methods are abstract.

An abstract method has no body.

Some of the subclass has to override it and provide the implementation.

Objects cannot be created out of abstract class.

Abstract classes basically provide a guideline for the properties and methods of an object.

In order to use abstract classes, they have to be subclassed.

There are situations in which you want to define a superclass that declares the structure of a given abstraction without providing a complete implementation of every method.

That is, sometimes you want to create a superclass that only defines generalized form that will be shared by all of its subclasses, leaving it to each subclass to fill in the details.

One way this situation can occur is when a superclass is unable to create a meaningful implementation for a method.

Syntax :

abstract type name(parameter-list);

As you can see, no method body is present.

Any class that contains one or more abstract methods must also be declared abstract.

To declare a class abstract, you simply use the abstract keyword in front of the class keyword at the beginning of the class declaration.

There can be no objects of an abstract class.

That is, an abstract class cannot be directly instantiated with the new operator.

Any subclass of an abstract class must either implement all of the abstract methods of the superclass, or be itself declared abstract.

EX :


 


Output :
B1’s implementation
This is a concrete method
EX 2 :

 


Output:
Area of rectangle.
200.0
Area of triangle.
30.0

Prev Next