Abstract class in Java | Java For Beginner | Java Example | Java Basic.


Abstract class

A Java abstract class is a class which cannot be instantiated, meaning you cannot create new instances of an abstract class.

Declaring an Abstract Class in Java

public abstract class MyAbstractClass
{

}

Abstract Methods:

An abstract class can have abstract methods. You declare a method abstract by adding the abstract keyword in front of the method declaration. 

Here is a Java abstract method example:

public abstract class MyAbstractClass

public abstract void abstractMethod();
}

If a class has an abstract method, the whole class must be declared abstract. Not all methods in an abstract class have to be abstract methods. 


Example:


abstract class MyClass
{
   public void show()
   {
      System.out.println("Hello World");
   }
   abstract public void show2();
}

class Demo extends MyClass
{
   public void show2()
   {
      System.out.println("Abstract method");
   }
   public static void main(String args[])
   {
      Demo obj = new Demo();
      obj.show2();
   }
}


Output:

Abstract method

Post a Comment

3 Comments

  1. What is difference between abstract class and Interface then?

    ReplyDelete
  2. See the Latest Post in Abstract class Vs Interface . parth your problem solved,.

    ReplyDelete