Skip to main content Skip to docs navigation
TechSpiderTutorials

Java 'abstract' Keyword

java abstract modifer

abstract is a modifier, and it can be used with the methods and with the classes.

java abstract method

The abstract modifier applies to methods and indicates that the code can only be run when implemented in a child class.

Abstract methods are mainly of benefit to class designers.

java Abstract Classes

An abstract class is a class that is declared abstract-it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be sub classed.

If a class includes abstract methods, the class itself must be declared abstract.

An abstract class may have static fields and static methods. You can use these static members with a class reference.

java abstract modifer Example

abstract class ABSClass
{
	//abstract method : only decalaration
  abstract void show();

  //non-abstract
  void accept()
  {
      System.out.println("non-abstract mehtod...");
  }
}
class AbsImpClass extends ABSClass
{
  int i;
   AbsImpClass()
    {
     i=1230;
    }
	 public void display()
	  {
	System.out.println("display");
	}

void show()
     {
    System.out.println("This is Abstract method..");
      System.out.println("Value in show() is.."+i);
     }
}
public class MainAbs
{
  public static void main(String args[])
  {
	  AbsImpClass abs=new AbsImpClass();
	 abs.show();
	abs.display();
	abs.accept();
	/*
 	ABSClass abc=new AbsImpClass();
	abc.show();
	abc.accept();
	abc.display(); //generates an error
    */
    }
}

Output:

This is Abstract method..
Value in show() is..1230
display
non-abstract mehtod...