Abstract Class in Java | Abstraction in Java | Abstract Method

In this article Abstract Class in Java we give the information about the class which is declared with the abstract keyword is called Abstract Class. It can have abstract and non-abstract methods.

Abstract Keyword in Java:-

Abstract Class in Java:-

The class which is declared with the abstract keyword is called Abstract Class. It can have abstract and non-abstract methods. A normal class does not have abstract methods.

  • Abstract class is always necessary to declare abstract class with abstract keyword.
  • Abstract class can have abstract and non-abstract methods.
  • It cannot be instantiated, that is, it cannot be used to create objects.
  • It can also have constructors and static methods.
  • To use abstract class, we have to inherit it from another class and provide implementation to abstract methods.
  • It can also have final methods.
  • Abstract methods do not have a body.

Abstraction in Java:-

Abstraction is a process in which the implementation is hidden and only the functionality is shown to the user.

In other words, “It shows only the necessary information to the user and the background details are hidden”.

For example: –

We type the message in whatsapp and send it. But we do not know about its internal working, how the message is sent.

There are two ways to achieve abstraction in java:-

1. Abstract class (0 to 100%)

2. Interface(100%)

What is abstract method?

Abstract Method

abstract return_type method_name(); //abstract method

The method which is declared by the abstract keyword is called abstract method. This method does not have implementation, that is, it does not have a body.

  • In this, the abstract keyword is written in front of the method name.
  • An abstract method contains a method signature but does not contain a body.
  • Instead of curly braces, put a semicolon at the end of this method; It seems.

Its example:-

abstract void showDetail(); //no method body and abstract

Abstract class and method example

Syntax for Abstract Class

abstract class class_name(); //abstract class

Below you have given a simple example of this. It has an abstract method named eat(). And its implementation is provided by a class named Dog.

abstract class Animal

{

abstract void eat();

}

class Dog extends Animal

{

void eat()

{

System.out.println(“they are eating food”);

}

public static void main(String args[])

{

Animal obj = new Dog();

obj.eat();

}

}

O/P:- they are eating food

Accessing the constructor of the Abstract Class –

An abstract class can have the same constructors as a normal class. And we can access the constructor from the subclass using the super keyword.

For example:-

abstract class Animal

{

Animal()

{      ….

}

}

class Dog extends Animal

{

Dog()

{

super();

}

}

Here we have used the super() keyword inside the constructor of Dog to access the constructor of Animal. The point to be noted here is that super should always be the first statement of the subclass constructor.

Some important points of Abstract Class:-

  • An abstract class is created using the abstract keyword.
  • In abstract class cannot created objects.
  • We can create abstract methods by using abstract keyword and such methods do not have body.
  • It is not necessary for an abstract class to have an abstract method. We can create abstract class even without abstract methods.
  • If abstract class does not have any method implementation then we should use interface because Java does not support multiple inheritance.
  • This class can implement interface without providing implementation of interface methods.
  • In java, we can run abstract class like any other class if it has main() method.

Related Link:

Some More: DBMS/ WT/ DMDW

Santosh Nalawade

Work as Assistant Professor and Web Developer.

Leave a Reply

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