Abstract Modifier (class, method) in Java

abstract modifier

Abstract Modifier (class, method) in Java

abstract is a keyword modifier in java. Abstract modifier is applicable for the class and method only. Everything about abstract keyword is explained here with the help of examples. So let’s understand the abstract modifier in detail.

 

Usage of Abstract Modifier :-

1) Abstract with Class :-

we can declare class with abstract modifier. If we want to create any class which could not be instantiated at that time we have to declare that class with the abstract modifier.

Ex.

abstract class AbstractDemo
{
  // this is abstract class
}

If any class contain any abstract method than we must declare that class with abstract modifier. And if any class extend abstract class than that sub-class need to implement all parent class abstract methods. If child class is not implement parent class abstract methods than again we have to declare child class as abstract and so on.

Ex.

abstract class ParentClass
{
  public abstract void abstractMethod();
}

class ChildClass extends ParentClass
{
  public void abstractMethod()
  {
    System.out.println("Here, abstractMethod() has been implemented.");
  }
}

Abstract class can contain both abstract as well as non-abstract methods.

 

2) Abstract with Methods :-

We can declare method also with the abstract modifier. If we just know the method declaration(method signature) part but not about method implementation(definition) at that time we have to declare that method as abstract.

Ex.

abstract class AbstractMethodDemo
{
  public abstract void abstarctMethod();
}
class ChildClass extends AbstractMethodDemo
{
  public void abstarctMethod()
  {
    System.out.println("Here, abstractMethod() has been implemented.");
  }
}

If any class contain any abstract method than we have to declare that class with abstract modifier as we say above. And that method should be implemented in it’s child class.

 

Thank You ! 🙂

Stay Connected with PLace4Java and follow us on our Facebook Page.

Be the first to comment

Leave a Reply

Your email address will not be published.


*