What is the use of Abstract modifier in Java

 

What is the use of Abstract modifier in Java

What is the use of Abstract modifier in Java

What is the use of Abstract modifier in Java

An abstract is a modifier applicable to classes and methods but not for variables.

Abstract Method

Even though we don’t know about implementation still, we can declare a method with abstract modifier i.e. for the abstract method the only declaration is available but not implementation hence abstract declaration should end with a semicolon.

Example…

public abstract void m1();

  • The child class is responsible to provide an implementation for parent class abstract methods.
  • By declaring abstract method in the parent class we can provide guidelines to child classes such that which method compulsory child has to implement.
  • Abstract method never talks about implementation if any modifier talks about implementation then its forms illegal combination with an abstract modifier. The following are varies the illegal combination of modifiers for a method with respect to abstract:- Final, private, strictfp, native, synchronized, static.

Abstract class

For any java class if we are not allowed to create an object (Because of partial implementation) such type of class we have to declare with abstract modifier ie for abstract classes instantiation is not possible.

//Example:--What is the use of Abstract modifier in Java
abstract class Test {
    public static void main(String args[]) {
        Test t = new Test();
        System.out.println("Hello");
    }
}
// Compile time Exception:- Test is abstract cannot be instantiated

Leave a Reply

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

*