Table of Contents
Subsequently, one may also ask, what is the difference between abstract and final in Java?
For classes, final is used to prevent inheritance whereas abstract classes depends upon their child classes for complete implementation. In cases of methods, final is used to prevent overriding whereas abstract methods needs to be overridden in sub-classes.
Additionally, what is the difference between abstract method and concrete method? Abstract methods are those which need to be implemented in subclass/child class. Abstract methods are only defined in superclass/parent class(Abstract class) but with no body. A method which is not abstract i.e. if a methods definition is given in the same class its declared is called concrete.
Similarly one may ask, can we have an abstract & final method?
Yes, there may be “final” methods in “abstract” class. But, any “abstract” method in the class can’t be declared final. It will give “illegal combination of modifiers: abstract and final” error. Here is the working example of the implementation.
How do you call an abstract method?
You simply need to create a subclass which extends from the abstract class. Then using the instance of this subclass you can use the defined methods of the abstract class.You can call abstract methods of an abstract class without problems.
Learn Coding in your Language! Enroll Here!
Abstract Method in Java
1: Which of the following data structures allows elements to be added and removed in a Last-In, First-Out (LIFO) order?
In object oriented programming, abstraction is defined as hiding the unnecessary details (implementation) from the user and to focus on essential details (functionality). It increases the efficiency and thus reduces complexity.
In Java, abstraction can be achieved using abstract classes and methods. In this blog, we will learn about abstract methods and its use in Java.
Abstract Method
A method declared using the abstract keyword within an abstract class and does not have a definition (implementation) is called an abstract method. When we need just the method declaration in a super class, it can be achieved by declaring the methods as abstracts.
Abstract method is also called subclass responsibility as it doesn’t have the implementation in the super class. Therefore a subclass must override it to provide the method definition.
Syntax for abstract method:
- abstract return_type method_name( [ argument-list ] );
Here, the abstract method doesn’t have a method body. It may have zero or more arguments.
Learn to code from industry experts! Get a free Demo here!
Points to Remember
Following points are the important rules for abstract method in Java:
- An abstract method do not have a body (implementation), they just have a method signature (declaration). The class which extends the abstract class implements the abstract methods.
- If a non-abstract (concrete) class extends an abstract class, then the class must implement all the abstract methods of that abstract class. If not the concrete class has to be declared as abstract as well.
- As the abstract methods just have the signature, it needs to have semicolon (;) at the end.
- Following are various illegal combinationsof other modifiers for methods with respect to abstract modifier:
- final
- abstract native
- abstract synchronized
- abstract static
- abstract private
- abstract strictfp
- If a class contains abstract method it needs to be abstract and vice versa is not true.
Example of Abstract Method in Java
Example 1:
In the following example, we will learn how abstraction is achieved using abstract classes and abstract methods.
AbstractMethodEx1.java
- // abstract class
- abstract class Multiply {
- // abstract methods
- // sub class must implement these methods
- public abstract int MultiplyTwo (int n1, int n2);
- public abstract int MultiplyThree (int n1, int n2, int n3);
- // regular method with body
- public void show() {
- System.out.println (“Method of abstract class Multiply”);
- }
- }
- // Regular class extends abstract class
- class AbstractMethodEx1 extends Multiply {
- // if the abstract methods are not implemented, compiler will give an error
- public int MultiplyTwo (int num1, int num2) {
- return num1 * num2;
- }
- public int MultiplyThree (int num1, int num2, int num3) {
- return num1 * num2 * num3;
- }
- // main method
- public static void main (String args[]) {
- Multiply obj = new AbstractMethodEx1();
- System.out.println (“Multiplication of 2 numbers: “ + obj.MultiplyTwo (10, 50));
- System.out.println (“Multiplication of 3 numbers: “ + obj.MultiplyThree (5, 8, 10));
- obj.show();
- }
- }
Learn to code from industry experts! Get a free Demo here!
Output:
Example 2:
By default, all the methods of an interface are public and abstract. An interface cannot contain concrete methods i.e. regular methods with body.
AbstractMethodEx2.java
- // interface
- interface SquareCube {
- // abstract methods
- public abstract int squareNum (int n);
- // it not necessary to add public and abstract keywords
- // as the methods in interface are public abstract by default
- int cubeNum (int n);
- // regular methods are not allowed in an interface
- // if we uncomment this method, compiler will give an error
- /*public void disp() {
- System.out.println (“I will give error if u uncomment me”);
- }
- */
- }
- public class AbstractMethodEx2 implements SquareCube {
- // defining the abstract methods of interface
- public int squareNum (int num) {
- return num * num;
- }
- public int cubeNum (int num) {
- return num * num * num;
- }
- // main method
- public static void main(String args[]){
- SquareCube obj = new AbstractMethodEx2();
- System.out.println(“Square of number is: “ + obj.squareNum (7) );
- System.out.println(“Cube of number is: “ + obj.cubeNum (7));
- }
- }
Output:
In this way, we have learned about abstract method and its implementation in Java.
Unlock Your Coding Potential with Our Programming Courses – Enroll Today
Can an abstract class have a final method?
Yes, there may be “final” methods in “abstract” class. But, any “abstract” method in the class can’t be declared final. It will give “illegal combination of modifiers: abstract and final” error.
public abstract final void show();
illegal combination of modifiers: abstract and final
Here is the working example of the implementation.
abstract class Sian //ABSTRACT CLASS
{
public final void show() // FINAL METHOD
{
System.out.println("Yes");
}
public void display()
{
System.out.println("Overriding");
}
public abstract void success();
}
class Ideone extends Sian //INHERTING ABSTRACT CLASS
{
public void display()
{
System.out.println("Overridden");
}
public void success() //OVERRIDING THE ABSTRACT METHOD
{
System.out.println("Success overriding");
}
public static void main (String[] args) throws java.lang.Exception
{
Ideone id = new Ideone(); //OBJECT OF SUBCLASS
id.show(); //CALLING FINAL METHOD
id.display(); //OVERRIDDEN METHODS
id.success();
}
}
OUTPUT:-
Yes
Overridden
Success overriding