Abstract method in java

Here is the table content of the article will we will cover this topic.
1. What is the abstract method in java?
2. Rules of abstract method in java?
3. Purpose of abstract method?

4. How abstract method works with the final keyword?
5. How abstract method works with the static keyword?
6. How abstract method works with the private keyword?
7. How abstract method works with the synchronized keyword?
8. How abstract method works with the strictfp keyword?

What is the abstract method in java

In this article, we will read about the Abstract method in java and the use of abstract method in java. Many programmers have a common question if there is an abstract method in a class then?. So let’s start, The abstract method is part of abstraction in java. A method declared without a body within an abstract class is an abstract method.  We declare the abstract method only when we require just method declaration in Parent classes and implementation will provide by child classes. We can declare an abstract method by use of the abstract keyword. This is a special type of method because it doesn’t have a body. An abstract method is declared either within the abstract class or interface. You can’t declare an abstract method in concrete class. To declare an abstract method, we have to follow the syntax:

// This is an abstract method
abstract return_Type methodName(); 

To make an abstract method you must provide an abstract keyword at the time of declaration.

Rules of abstract method in java

  • To declare the method as abstract, use the abstract keyword.
  • You must place the abstract keyword before the method name while you are declaring the method.
  • An abstract method does not contain a method body.
  • Instead of curly braces, an abstract method will have a semicolon (;) at the end.
// This is an abstract method without implementation 
    public abstract void calculates(); 

Purpose of Abstract method

Let’s discuss a java abstract method example If a programmer doesn’t want to provide an implementation of the method at the time of declaration. And he/she want to provide the implementation in the derived class. This can be achieved by specifying the abstract keyword. Sometimes, the programmer doesn’t know what will be the implementation of the method in different scenarios. So, it will be the derived class responsibility to provide an implementation of abstract methods.

abstract class Data
{
	abstract void showData();
}

class NationalSchool extends Data
{
	@Override
	void showData()
	{
		System.out.println("This is National School's data");
	}
	
}

class IndianSchool extends Data
{
	@Override
	void showData()
	{
		System.out.println("This is Indian School's data");
	}
	
}

class MainClass 
{
	public static void main(String arg[])
	{
		Data obj = new NationalSchool();
		obj.showData();
		
		Data obj1 = new IndianSchool();
		obj1.showData();
	}
}

Output:
This is National School’s data
This is Indian School’s data

Explanation of Example: Let say programmer want to print the data from different school but he doesn’t know want will be output from different schools. So, he/she declare an abstract method in an abstract class. So that each derived class can provide implements its body according to its functionality. In this example we have three classes, one is abstract class Data and two class is Concrete/non-abstract. Data is an abstract class having one abstract method showData().

So, when a programmer extends the Data class, It is the programmer’s responsibility to provide an implementation of the showData() method in derived classes.

4. How abstract method works with the final keyword?

An abstract method can’t declare as final.
Reason: abstract and final both terms are opposite to each other. An abstract method says you must redefine it in a derived class. A final method says you can’t redefine this method in a derived class. Let’s see how the use of abstract methods in java.

use of abstract method in java
abstract class Data
{
	abstract final void showData();
}

class NationalSchool extends Data
{
	@Override
	void showData()
	{
		System.out.println("This is National School's data");
	}
	
}

class MainClass 
{
	public static void main(String arg[])
	{
		Data obj = new NationalSchool();
		obj.showData();
	}
}

Output: This will show compile time exception.

5. How abstract method works with the private keyword?

An abstract method can’t be private because a private method is not accessible outside the class.
Reason: An abstract method says you must redefine it in a derived class. Users can redefine it if it is accessible from outside the class. A private method is not accessible from outside the class. So, we can’t use the private keyword with abstract methods.

abstract class Data
{
	abstract private void showData();
}

class NationalSchool extends Data
{
	@Override
	void showData()
	{
		System.out.println("This is National School's data");
	}
	
}

class MainClass 
{
	public static void main(String arg[])
	{
		Data obj = new NationalSchool();
		obj.showData();
	}
}

Output: This will show compile time exception.

The abstract method showData() an in type Data can only set a visibility modifier, one of public or protected.

6. How abstract method works with the static keyword?

An abstract method can’t be static because we can’t override a static method.
Reason: If Java allows us to create an abstract static method(Method without body) and someone calls the abstract method using class name(because the static method can access directly by class name), then What would happen?

abstract class Data
{
    static abstract void showData();
}
class NationalSchool extends Data
{
    @Override
    void showData()
    {
        System.out.println("This is National School's data");
    }
    
}
class MainClass 
{
    public static void main(String arg[])
    {
        Data obj = new NationalSchool();
        obj.showData();
    }
}

Output: This will show compile time exception.

7. How abstract method works with the synchronized keyword?

An abstract method can’t be synchronized because a thread gets a lock on an object when it enters the method.
Reason: A thread that enters the synchronized method must get the lock of the object (or of the class) in which the method is defined. We can’t create an abstract class so there is no object with the lock.

abstract class Data
{
	Abstract synchronized void showData();
}

class NationalSchool extends Data
{
	@Override
	void showData()
	{
		System.out.println("This is National School's data");
	}
	
}

class MainClass 
{
	public static void main(String arg[])
	{
		Data obj = new NationalSchool();
		obj.showData();
	}
}

Output: This will show compile time exception.

8. How abstract method works with the strictfp keyword?

An abstract method can’t be strictfp.
Reason: Strictfp is a modifier and as you know we can change the modifier of an overridden method (method of parent class) while overriding the method in child class. It would make no sense to make an abstract method strictfp. That is, methods do not inherit modifiers.

NOTE:  No modifiers are allowed on abstract methods except public and protected. You’ll get a compile-time error if you try.

Example:

abstract class Data
{
	Abstract strictfp void showData();
}

class NationalSchool extends Data
{
	@Override
	void showData()
	{
		System.out.println("This is National School's data");
	}
	
}

class MainClass 
{
	public static void main(String arg[])
	{
		Data obj = new NationalSchool();
		obj.showData();
	}
}

Output: This will show compile time exception.

Leave a Comment