Understand abstract Class Concept in Java

Understand abstract Class Concept in Java

In this tutorial, you will discover the insights about the concept of Java abstract class. There are follow up examples to get into the thick and thin of what the abstraction does.

The major topics covered are:

  1. Introduction to abstraction
  2. Abstract Class
  3. Abstract methods
  4. Examples

Abstract class in Java is like a template for functions that have definition outside. However, unlike the interfaces, it can also have some methods that could have code inside.

Java Abstract Class Overview With Examples

What Does ABSTRACTION Imply?

In object-oriented programming (OOP), abstraction happens to be one of the essential features, be it Java or any other OOP language. It is the strategy or process of presenting to the user only functioning details while hiding the implementation details of the structure. For example, when you browse the Internet, you are only concerned with the address or domain name of a particular web page. Once you enter the address, your browser has to fetch details and interact with servers across the globe. It is not for the end-user to choose how the protocol of communication takes place between the browser and the servers. Coming back to Java, abstraction allows the programmer to hide specific details from the user while mentioning others. The user only has information about what the object does but does not know how it happens. We can implement Java abstraction with the following two concepts:

• Abstract Classes • Interfaces in Java

We will talk about Abstract Classes in this tutorial.

Abstract Class

An abstract class is the one that has its functions defined in other classes. Hence, we call it Abstract, which works as a template.

The difference between a regular class and an abstract class arises with the keyword “abstract.” To define a class abstract, the programmer should use the abstract keyword before declaring the class.

We can’t instantiate an abstract class. It means we can’t make objects for an abstract class.

If you have to consume the functionality of the abstract class, then get another class extending it. You can attain this behavior by using the “extends” keyword. We term this process as inheriting.

Abstract Methods

An abstract method is defined within an abstract class when the programmer wants the class to contain a method but does not want to provide real implementation details for it.

You should write the abstract keyword before the method to define any method abstract. Also, you can declare an abstract method in the abstract class. However, the inheriting class must have its definition.

By declaring, we mean specifying just the signature of the method. The body of the method or the definition is available in the extending class.

To define an abstract class, after the signature of the method, we put a semi-colon (;), instead of parenthesis.

The inheriting class should provide proper implementation details to all the abstract methods in the abstract class.

An abstract class can have regular as well as abstract methods. An abstract method cannot be declared without an abstract class, while the reverse is possible.

The convention for class variables is to be set to private while we can skip it if we want. The inheriting class can have its own set of methods, but it should define a body for all abstract methods in the abstract class.

Abstraction Examples

In this section, we’ve brought up several examples to demonstrate how abstraction works in Java. You can use the code given here and practice from Java command-line or using the Eclipse IDE.

Example-I

So, let’s create a Java abstract class. It has one regular and one abstract method.

abstract class AbstractClass {
   private String a;
   int b;

   AbstractClass(String label, int num) {
      a = label;
      b = num;
   }

   String getLabel() {
      return a;
   }
   abstract int getNumber();   
}

In the above snippet, the AbstractClass has getNumber() as its abstract method while getName is a standard method. You can’t instantiate an object of this class. Therefore, you should create a concrete class that will inherit the abstract class.

class ConcreteClass extends AbstractClass {

   ConcreteClass(String label, int num) {
      super(label, num);
   }
   int getNumber() {
      return b;
   }
}

Now, we can use the methods of AbstractClass after instantiating the ConcreteClass.

If you see, the ConcreteClass constructor contains the super keyword. It calls AbstractClass constructor with the following parameters:


public class Main {
   public static void main(String args[]) {
      ConcreteClass obj = new ConcreteClass("AJ", 1); 
      System.out.println( obj.getNumber() );
      System.out.println( obj.getLabel() );
   }
}

Here is the consolidated code:


abstract class AbstractClass {
   private String a;
   int b;

   AbstractClass(String label, int num) {
      a = label;
      b = num;
   }

   String getLabel() {
      return a;
   }
   abstract int getNumber();   
}

class ConcreteClass extends AbstractClass {

   ConcreteClass(String label, int num) {
      super(label, num);
   }
   int getNumber() {
      return b;
   }
}

public class Main {
   public static void main(String args[]) {
      ConcreteClass obj = new ConcreteClass("AJ", 1); 
      System.out.println( obj.getNumber() );
      System.out.println( obj.getLabel() );
   }
}

After running the above Java code, you would see the output as shown below:

1
AJ

Example-II

Check out another example to get more clarity. It considers one abstract class, Area and two other subclasses extending it. It surely would help you visualize the need for abstraction:

This is image title


abstract class Area {
   String name = "";

   Area(String num) {
      name = num;
   }

   abstract int getArea();
}

class Rectangle extends Area {
   int length = 0;
   int width = 0;

   Rectangle(String num, int l, int w) {
      super(num);
      length = l;
      width = w;
   }

   int getArea() {
      return length*width;
   }
}

class Circle extends Area {
   int radius = 0;

   Circle(String num, int r) {
      super(num);
      radius = r;
   }

   int getArea() {
      return (int) (3.14*radius*radius);
   }
}

public class Main {
   public static void main(String args[]) {
      Rectangle obj = new Rectangle("Rectangle", 1, 2); 
      System.out.println( obj.name ); 
      System.out.println( obj.getArea() );

      Circle obj1 = new Circle( "Circle", 2 ); 
      System.out.println( obj1.name ); 
      System.out.println( obj1.getArea() );
   }
}

After running the above Java code, you would see the output as shown below:

Rectangle
2
Circle
12

We hope this tutorial on Java abstract class would have helped you figure out how the abstraction works in Java. Please do practice with the examples to get full clarity.

Thanks for reading !

java programming.

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Java Core (2020) | Java for beginners | Brush up your Java Skills | Get Hired

In this video we will learn about all the major concepts that come under Java Core . The course is very carefully designed and is made with keeping simplicit...

Learn Java Programming | Java Complete Tutorial for Beginners | Simpliv

This program on Java programing from Simpliv’s experts uses Java and an Integrated Development Environment (IDE). Just download this, with their help of course, and open your way to hundreds of lines of source code, and hundreds of lines of comments. That is some Java programming learning!

A Game to Boost Your Java Object Oriented Programming Skills

This is a simple black screen console game where first the program will automatically generate a random number, and all the players who play the game here will continue to guess the number one by one and match it with the previous program.

Introduction To Java In Hindi | Learn Java Programming | Java Basics

Introduction To Java In Hindi | Learn Java Programming | Java Basics. This article Session on Introduction to Java in Hindi will cover the 'what, why, and how' of Java. This will include features, data types, operators, conditional statements, and loops.