Abstract Class in Java

In this post, we completely discuss the abstract class in java it is very important in the Oops concept also.

These fundamental concepts must be understood before creating a Java program. In case you missed it, we also discussed decision-makingloop control, and method in java. Hurry up now! In the past, there have also been brief discussions on a number of other Java-related topics, such as the differences between C++ and JavaJava operators, and many others. This information will significantly improve your understanding. So without any delay, let’s get to the point:

What is an Abstract Class in Java?

In Java, an abstract class is a class whose declaration contains the abstract keyword.

It may use non-abstract and abstract methods.

Objects cannot be created using the restricted class known as an abstract class (to access it, it must be inherited from another class).

Before going to abstract class we first know what is Abstraction in Java.

Abstraction in Java

Abstraction is the technique of hiding implementation specifics from the user and only displaying functionality.

In a different method, it 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 delivery.

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

Abstract Class in Java

In Java, an abstract class is a class whose declaration contains the abstract keyword. It may use non-abstract and abstract methods.

Points to be Remember

  • An abstract keyword must be used when declaring an abstract class.
  • Both abstract and non-abstract approaches are possible.
  • You can’t instantiate it.
  • Both static methods and constructors are possible.
  • It might have final methods that forbid the subclass from changing the body of the method.
Abstract Class in Java
Abstract Class in Java

Example of Abstract Class in Java

abstract class abs{}  

Abstract Method in Java

The only place an abstract method can be used is in an abstract class, and it lacks a body. The subclass provides the body (inherited from).

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

The Abstract Class in Java can have both abstract and regular methods:-

Example of an Abstract class that has an abstract method

abstract class car{  
  abstract void run();  
}  
class Honda extends car{  
void run(){
System.out.println("the car is a honda car");
}  
public static void main(String args[]){  
 car obj = new Honda();  
 obj.run();  
}  
}  

In this case, we can not call direct the main class (car), we first declare a class and extends the class to the main class.

Output:-

Output
the car is a honda car

Let’s take a real-life example for better understanding.

// Abstract class
abstract class Lpu {
  // Abstract method (does not have a body)
  public abstract void Lpustudent();
  // Regular method
  public void Study() {
    System.out.println("We are Studding in LPU");
  }
}

// Subclass (inherit from Lpu)
class MCA extends Lpu {
  public void Lpustudent() {
    // The body of Lpustudent() is provided here
    System.out.println("Lpu student say we are best");
  }
}

class Main {
  public static void main(String[] args) {
    MCA myMCA = new MCA(); // Create a MCA object
    myMCA.Lpustudent();
    myMCA.Study();
  }
}

Output

Output
Lpu student say we are best
We are Studding in LPU

Why And When To Use Abstract Classes and Methods?

  • Hide certain data and only display key information about an object to establish security.

Note:- interfaces—about which you will learn more in the following chapter—can also be used to achieve abstraction.

What is the difference between the Abstract class and the Concrete Class?

Abstract ClassConcrete Class
The abstract modifier is used to declare an abstract class.The abstract modifier is not used to declare a concrete class.
The new keyword cannot be used to instantiate an abstract class directly.The new keyword enables direct instantiation of a concrete class.
It’s possible for an abstract class to have abstract methods or not.An abstract method cannot be found in a concrete class.
A final declaration cannot be made for an abstract class.Declaring a class as final allows it to be concrete.
It is possible to implement an interface by omitting implementations for certain of its methods. A child class is required for this.All of the interface’s methods are simple to use.
Abstract Class in Java

What are abstract classes in Java?

In Java, an abstract class is a class whose declaration contains the abstract keyword. It may use non-abstract and abstract methods.

What is the purpose of abstract class in Java?

Without even giving the implementation of interface methods, Java Abstract classes can implement interfaces. The Java Abstract class is used to offer default implementation or to provide a uniform method implementation to all subclasses.

What is an abstract class example?

For the code to be reusable and expandable, abstract classes must be used to provide an abstraction. For instance, one abstraction that makes it simple to add more vehicles is a parent class called Vehicle that Truck and Motorbike inherit from.

Final Opinion

In this post, we cover all the concepts of Abstract Class in Java and its uses. we hope you understand all the concepts that we describe if you face any problems then please comment we can respond as soon as possible.

For more read with us Oraltadalafil

Read our previous Post

Leave a Comment

%d bloggers like this: