Software Training Institute in Chennai with 100% Placements – SLA Institute

Easy way to IT Job

Share on your Social Media

Abstract Class in Java

Published On: September 12, 2022

In Java, an abstract class is one that has the abstract keyword stated in its declaration. Both abstract and non-abstract approaches are possible (method with the body). Through this article to understand the abstract class in Java. Let’s first comprehend Java’s abstraction before learning about the abstract class. Join SLA for the Best Java Training in Chennai.

Abstraction in Java

Abstraction is the technique of hiding implementation specifics from the user and just displaying functionality. A different method just displays the user’s needs and hides internal information, such as when sending an SMS, where you write the text and send the message. You are unaware of the internal operations involved in message transmission.

When an item is abstracted, you may concentrate on what it does rather than how it does it.

Abstraction Techniques

In Java, abstraction may be achieved in two different methods.
Abstract Class (0 to 100%)
Interface (100%)

Abstract Class in Java

An abstract class is one that has been declared to be such. Both abstract and non-abstract approaches are possible. It must be expanded, and its strategy put into practice. You can’t instantiate it.

Things to Keep in Mind

  • An abstract class should always be declared with the abstract keyword.
  • Both abstract and non-abstract approaches are possible.
    You can’t instantiate it.
  • It is feasible to use constructors and static methods.
  • It may have final methods that compel the subclass not to alter the method body.

Rules for using Java Abstract Class

  1. The Abstract class should be declared with an abstract keyword.
  2. The Abstract class will have abstract and non-abstract methods
  3. The Abstract class can’t be instantiated.
  4. The Abstract class will have the final methods
  5. The Abstract class will have constructors and static methods.

Example for Abstract Class
 abstract class A{}

Abstract Method in Java

An abstract method is one that has been declared abstract and does not have an implementation.

Example of using the Abstract method                                                                                        abstract void printStatus();

Example for Abstract Class with Abstract Method
abstract class Car{
abstract void run();
class Tata extends Car{
void run(){System.out.println(“Running Smoothly”);}
public static void main(String args[]){
Car obj = new Tata();;

Running Smoothly

Real Scenario of Abstract Class

Most of the time, we are unaware of the implementation class (which is hidden from the consumer), and the factory function provides an object of the implementation class. A factory method is a method that gives back the class instance. The factory technique will be covered later.

In this example, the draw() function of the Rectangle class will be called if you create an instance of the class.
abstract class Shape{
abstract void draw();

class Rectangle extends Shape{
void draw(){System.out.println(“drawing rectangle”);}
class Circle1 extends Shape{
void draw(){System.out.println(“drawing circle”);}
class TestAbstraction1{
public static void main(String args[]){
Shape s=new Circle1();

drawing circle

Finding Rate of Interest with Abstract Class in Java

abstract class Bank{
abstract int getRateOfInterest();
class SBI extends Bank{
int getRateOfInterest(){return 7;}
class PNB extends Bank{
int getRateOfInterest(){return 8;}
class TestBank{
public static void main(String args[]){
Bank b;
b=new SBI();
System.out.println(“Rate of Interest is: “+b.getRateOfInterest()+” %”);
b=new PNB();
System.out.println(“Rate of Interest is: “+b.getRateOfInterest()+” %”);

Rate of Interest is: 7%
Rate of Interest is: 8%

Abstract Class with Constructor, Data Member, and Methods

A data member, abstract method, method body (non-abstract method), function Object() { [Code] }, and even the main() method can all be found in an abstract class.
abstract class Bike{
Bike(){System.out.println(“Bike is Created”);}
abstract void run();
void changeGear(){System.out.println(“Gear Changed”);}
class Honda extends Bike{
void run(){System.out.println(“Running Safely..”);}
class TestAbstraction2{
public static void main(String args[]){
Bike obj = new Honda();;

Bike is Created
Running Safely
Gear Changed

Interface with Abstract Class

The interface can also be implemented in some way using the abstract class. In this situation, the user may not be required to override every interface method.
interface A{
void a();
void b();
void c();
void d();
abstract class B implements A{
public void c(){System.out.println(“I am c”);}
class M extends B{
public void a(){System.out.println(“I am a”);}
public void b(){System.out.println(“I am b”);}
public void d(){System.out.println(“I am d”);}
class Test5{
public static void main(String args[]){
A a=new M();
I am a
I am b
I am c
I am d


Hope this article helps you understand utilizing Abstract Class in Java. Learn practically in our Java Training Institute in Chennai as we follow the experimental-based learning approach for remote and classroom learners worldwide.

Share on your Social Media

Just a minute!

If you have any questions that you did not find answers for, our counsellors are here to answer them. You can get all your queries answered before deciding to join SLA and move your career forward.

We are excited to get started with you

Give us your information and we will arange for a free call (at your convenience) with one of our counsellors. You can get all your queries answered before deciding to join SLA and move your career forward.