• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Java Program
  • Selenium
  • Selenium Programs
  • Manual Testing
  • Difference
  • Tools
  • SQL
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Tutorial » Abstract Class In Java

Abstract Class In Java

Last Updated on: August 15, 2020 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Abstraction In Java
  • How to Achieve Abstraction?
  • Abstract Class in Java
  • What is an abstract class?
  • Why We Need an abstract class?
  • How to Declare an Abstract class?
  • Why You can’t create the object of abstract class?
  • Abstract class in Java Important Points

Abstract Class In Java: In our Java learning series still now we have discussed Encapsulation in Java, and in this post, we are going to learn other new topics about the Abstract class in Java in detail. Before discussing abstract class, let’s understand what an abstraction in java is?

Abstraction In Java

Abstraction is the process of hiding the inner implementation details and shows the only functionality to the user. That means what a user wants to see and hiding the inner details of the application or program. For example, your requirement is to multiply two numbers using the calculator. For that, you press two numbers with the multiple symbols in the calculator, and finally, you got the result, but you don’t know the internal process of the multiplication.

How to Achieve Abstraction?

We can achieve abstraction in two ways:

  • Using an abstract class
  • By Implementing Interface

Abstract Class in Java

In this article, we are going to discuss:

  • What is an abstract class?
  • Why We need an abstract class?
  • How to Declare an abstract class?
  • Why We Can’t create an object of an abstract class?
  • Important points to remember about Abstract class

What is an abstract class?

The class which was declared with the keyword “abstract” is called an abstract class. An abstract class contains a concrete method (method with the body) and abstract method (methods without body). In Java programming language, normal classes have concrete methods, but there are no abstract methods.

Why We Need an abstract class?

An abstract class is helpful when we have the required details, but it’s not complete, and we don’t know the full implementation that time its useful to define that class as abstract class. By declaring that as an abstract class, we force the user to implement later when the user has the complete implementation details. If the user has not implemented the abstract methods, he will get a compilation error.

Abstract Class In Java Implementation
Abstract Class In Java Implementation

Let’s take an example of a Bank Application, as per the Government bodies it has declared every bank have some basic features like Deposit, Withdraw, etc. but when it comes to bank-level, every bank has a different process of implementation. So, in that case, the Government bodies can declare a class “Features” and make that as abstract, so when it comes to the bank level, they have to implement the abstract methods which were declared by the Government bodies.

//abstract parent class
abstract class Rbi
{
   //abstract method
   public abstract void deposit();
}
//Sbi class extends Rbi class
public class Sbi extends Rbi
{
   public void deposit()
   {
      System.out.println("Woof");
   }
   public static void main(String args[])
   {
      Rbi obj = new Sbi();
      obj.deposit();
   }
}

How to Declare an Abstract class?

We can declare an abstract class by below, which have abstract methods and concrete methods

//Declaration using abstract keyword
abstract class A
{
   //This is an abstract method
   abstract void myMethod();

   //This is a concrete method with body
   void anotherMethod()
   {
      //Does something
   }
}

Rules:

  • There are many cases where it is difficult or unnecessary to implement all the methods without complete information of the parent class. In those cases, we can declare the parent class as an abstract class so that whichever class extends the parent class, the class must need to implement the unimplemented or abstract methods of the parent class.
  • If the child class has not implemented the abstract methods of the parent class, then the child class needs to be declared as abstract.
  • An abstract class can not be instantiated means you can not create an object of an abstract class. To use an abstract class, you need to create another class that extends an abstract class and implements all the abstract methods. After implementation, you can create an object of child class to call the abstract methods or parent class and implemented methods of the child class.

Why You can’t create the object of abstract class?

Abstract classes are not complete class because it has incomplete methods (abstract methods) which have no method body. If in Java programming language, you can create an object and calls an abstract method, what will happen because those methods don’t have any implementation. And the other thing is an object, which is concrete, but an abstract class is a template. So for creating an object, you have to extend an abstract class and build a completely implemented class after that you can create the object.

Let’s take an example for understanding

abstract class AbstractDemo
{
   public void myMethod()
   {
      System.out.println("Hello");
   }
   abstract public void anotherMethod();
}
public class Demo extends AbstractDemo
{
   public void anotherMethod() 
   {
      System.out.print("Abstract method");
   }
   public static void main(String args[])
   {
      //error: You can't create an object of it
      AbstractDemo obj = new AbstractDemo();
      obj.anotherMethod();
   }
}

Abstract class in Java Important Points

  • Abstract keyword is used for creating an abstract class.
  • You can’t instantiate an abstract class.
  • For creating an abstract method, we need to use the keyword “abstract,” and the abstract methods don’t have a method body.
  • If a class has abstract methods, then that class must be declared as an abstract keyword.
  • An abstract class doesn’t need to have an abstract method, we can make an abstract class even if we don’t have any abstract methods.
  • If an abstract class doesn’t have any method implementation at that time, it’s better to declare an interface, Because Java programming language doesn’t support multiple inheritances.
  • If a class extends an abstract class, that extended class should have implemented all the abstract methods; otherwise, the extended class should be declared with the “abstract” keyword.
  • In an abstract class, we can implement an interface without implementing any methods of an interface.
  • With the help of the abstract class, we can provide a common method implementation to all the subclasses.

Ref: article

    Filed Under: Java Tutorial

    Reader Interactions

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers