Java

Abstract Class In Java

An abstract class is one that has the “abstract” keyword used to define it. Both abstract methods (methods without a body) and concrete methods are possible (regular methods with body). Abstract methods cannot be included in a regular class (a non-abstract class). We will discover what an abstract class is, why we use it, and the guidelines we need to follow while using it in Java in this tutorial.

Why we need an abstract class?

Say we have a class called Animal with the function sound() and subclasses like Dog, Lion, Horse, Cat, etc. Since each animal has a unique sound, it serves no purpose to implement this function in the parent class. This is so that each child class may provide its own implementation details, such as the word “Woof” or “Roar” in this method, which must be overridden by each child class.

Implementing this function in the parent class is thus pointless since we know that all animal child classes will and should override it. Thus, making this function abstract would be a prudent decision as doing so forces all child classes to implement it (otherwise, a compilation error would occur) and frees up the parent class from having to implement it.

You must declare this class abstract since the Animal class has an abstract method.

Now that each animal must have a sound, we made it necessary for the child class to provide implementation details by making this function abstract. We can guarantee that every animal has a sound by doing this.

Abstract class Example

//abstract parent class
abstract class Animal{
   //abstract method
   public abstract void sound();
}
//Dog class extends Animal class
public class Dog extends Animal{

   public void sound(){
	System.out.println("Woof");
   }
   public static void main(String args[]){
	Animal obj = new Dog();
	obj.sound();
   }
}

Output:

Woof

Therefore, in situations like this, we often designate the class as abstract, and afterwards concrete classes extend these classes and override the methods appropriately, as well as having the option of adding their own methods.

Abstract class declaration

Although an abstract class describes the methods, it may not really implement them all.

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

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

Rules

Note 1: As seen in the aforementioned example, there are situations in which it is difficult or often unnecessary to implement every method in the parent class. When this occurs, we may designate the parent class as abstract, converting it into a unique class that cannot stand alone.

All the methods that are marked as abstract in the parent class must be implemented by any classes that are derived from the abstract class.

Note 2: You cannot make an object of an abstract class since it cannot be instantiated. To utilize this class, you must first construct a class that extends it and provides the implementation of any abstract methods. Then, you may use the object of that child class to call both implemented and non-implemented non-abstract methods of the parent class (those that were abstract in parent but implemented in child class).

Note 3: A child class must be designated abstract if it does not implement every abstract method of an abstract parent class.

Why can’t we create the object of an abstract class?

Because these classes aren’t complete, they contain abstract methods with no bodies; thus, if Java lets you construct objects from these classes, what would happen if someone called the abstract method while utilizing those objects? The technique would not really be implemented to use.
A tangible thing is another reason. Since an abstract class functions as a template, you must build upon it before using it.

Example to demonstrate that object creation of abstract class is not allowed

We cannot instantiate an abstract class, as was said earlier. There is a compilation mistake in this application.

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 object of it
      AbstractDemo obj = new AbstractDemo();
      obj.anotherMethod();
   }
}

Output:

Unresolved compilation problem: Cannot instantiate the type AbstractDemo

To avoid having to declare that class abstract as well, the class that extends the abstract class must implement all of its abstract methods.

Abstract class vs Concrete class

Concrete classes are described as not being abstract. Animal is an abstract class in the example given above, whereas Cat, Dog, and Lion are concrete classes, as we saw at the beginning of this article.

Key Ideas:

Unless it is expanded by another class, an abstract class is useless.

If an abstract method is declared in a class, the class must also be declared abstract. In a concrete class, there cannot be an abstract method. Contrary to popular belief, a class may still be designated as abstract even if it has no abstract methods.

It may also use tangible, non-abstract methods.

In a different lesson, I went through the guidelines and examples of abstract methods. You can access that tutorial here: Java’s abstract method

Let’s look at some fundamentals and an example of an abstract method right now.

  1. Abstract methods don’t have bodies.
  2.  A semicolon should always follow the statement (;).
  3. It needs to be ignored. It is necessary to extend an abstract class, just as it is necessary to override an abstract method.
  4. In order to have abstract methods, a class must be declared abstract.

It should be noted that a class that extends an abstract class must override every abstract method.

Example of Abstract class and method

abstract class MyClass{
   public void disp(){
     System.out.println("Concrete method of parent class");
   }
   abstract public void disp2();
}

class Demo extends MyClass{
   /* Must Override this method while extending
    * MyClas
    */
   public void disp2()
   {
       System.out.println("overriding abstract method");
   }
   public static void main(String args[]){
       Demo obj = new Demo();
       obj.disp2();
   }
}

Output:

overriding abstract method

 

 

Related Articles

Leave a Reply

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

Back to top button