Java – Abstract Classes and Methods

 

Java Abstract Method

An Abstract method in java is a method prototype without any implementation. Its implementation is provided by the subclass of the java class in which it is declared. To create an java abstract method, simply specify the modifier abstract followed by the method declaration and replace the method body by a semi-colon.

Syntax of java abstract method:

abstract datatype function name();

Example:

abstract double area();

NOTE:

1. Method must always be defined in a subclass.

2. Method declared with final keyword, can not be redefined in subclass.

Java Abstract Class

The class that contains atleast one abstract method must be declared abstract such a class known as java abstract class. Using abstract classes, we must specify the following conditions:

1. We can not use abstract classes to declare instance object directly.

2. Java Abstract method of abstract class must defined in its sub classes.

3. We can not declare Abstract Constructor.

When we are extending existing classes, we have a choice to redefine the method of super class. A super class has common features that are shared by sub class. In some cases, we find that super class cannot have any object and such of classes are called Abstract classes.

Java Abstract classes usually contain abstract method. Abstract classes are generally used to provide common interface sub classes. We know that a super class is more simple than its sub class.

Example:

	abstract class A
	{
		abstract void call(); 
	}
	
	class B extends A
	{
		void call() {
			
		}
	}
	
	class absexampleprog
	{
		public static void main(String a[])
		{	
			B ob=new B();
			ob.call();
		}
	}

Tags: