How to create abstract class in java with detail example

A class declared with the abstract keyword is considered an abstract class in Java. It can have abstract or non-abtract methods. Before learning about abstract classes in Java, you need to understand what abstraction is in Java.

How to create abstraction in Java with detail example

Abstraction class in java is a process that hides the deployment details and only shows the feature to the user. In other words, abstract class in java only shows things that are important to the user and hides the inside details, for example: to send a message, the user simply writes text and sends it. You do not know the internal processing of message delivery. Abstract class in java helps you focus on the object rather than the way it works.

Abstract class in Java

A class declared abstract is an abstract class. It needs to be inherited and its method implemented. It can not be initialized.

Use the abstract keyword to declare an abstract class in java. This keyword appears before the class keyword in the class declaration. For example:
abstract-class-in-java-with-detail-example
There are two ways to achieve abstract class in Java:
  • Abstract class in java (0 to 100%)
  • Interface (100%)

Abstract methods in Java

A method declared as abstract and without a debugger is an abstract method.

If you want a class that contains a particular method but you want to actually implement that method to be decided by subclasses, then you can declare that method in the super class by abstract form.

The abstract keyword in java is used to declare an abstract method. An abstract method in java does not have a method body.

Abstract methods in java will not be defined, followed by a semicolon, without the brackets:

abstract-class-in-java-with-detail-example

Example of abstract classes and abstract methods in Java

In this example, Bike is an abstract class containing only one abstract method, run. Its implementation is provided by the Honda class.

abstract-class-in-java-with-detail-example

Inherit the Abstract class in Java

In this example, Shape is an abstract class in java, its implementation is provided by the Rectangle class and the Circle class. These two classes inherit the abstract class Shape.

abstract-class-in-java-with-detail-example

When we create a representation of the Rectangle class (at the Shape s = new Circle ();), the draw () method of the Rectangle class is summed.

You may not be familiar with how to write all the classes together in the same file, and can make you look awkward. In the next example we will present each file separately for you to understand.

Of course, while programming any language, each class we create, it serves a specific purpose. Therefore, you should create separate classes in each file, do not write all the classes in the same file like above.

Another example of inheriting the Abstract class in Java

First, we have an abstract Bank class whose abstract method is named getRateOfInterest () for the purpose of taking the interest rate of the bank in general.

File: Bank.java


abstract-class-in-java-with-detail-example


Next, we have two SBI and PNB classes representing the banks' name and these two classes inherit the abstract Bank class above. Since these two classes inherit the Bank abstraction layer, both classes must provide the specific implementations for the getRateOfInterest () method.

File: SBI.java
abstract-class-in-java-with-detail-example
File: PNB.java
abstract-class-in-java-with-detail-example
Finally, the TestBank class has the main () method:
abstract-class-in-java-with-detail-example
Abstract classes in java can have data members, abstract methods, constructors, and possibly main () methods.
File: Bike1.java
abstract-class-in-java-with-detail-example
Rule: If you are inheriting any abstract class in java that has an abstract method, you must either provide the implementator of the methods of this abstract class.

The abstraction layer can also be used to provide some Interface implementations. In this situation, the end-user can not be forced to override all methods of that interface.

Note: If you are new to Java, learn Interface before and ignore this example.
abstract-class-in-java-with-detail-example
To find out more about how to use this keyword in java with detail example:

Comments