Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

Abstract Class

We know that once a class is declared, we can declare any number of objects belonging to that class. When a method is written in the class, then it is available to all the objects of it's class. For example consider a class TestCube that contains a method cube() that calculates the cube value of a given number.
If we create three objects to this class, all the three objects get the copy of this method. Let's see the program.

Example:
class TestCube
{
	void cube(int a)
	{
		System.out.println("cube="+(a*a*a));
	}
}
class Test
{
	public static void main(String args[])
	{
		TestCube ob1=new TestCube();
		TestCube ob2=new TestCube();
		TestCube ob3=new TestCube();

		ob1.cube(2);
		ob2.cube(3);
		ob3.cube(4);
	}
}
Output

cube=8
cube=27
cube=64

here in the above program we observed, that the requirement of all the objects is same that means to calculate the cube value.

But sometimes the requirement of the objects will be different and entirely dependent on the specific object only.

For example, let in the above program if the first object wants to calculate square value, the second object wants the square root value and the third object wants cube value by defining one calculate() method , then we can perform these tasks by implementing the concept of abstract class.

  • Abstract class is a class containing some abstract methods.
  • • The method having no body is called as abstract method. It contains only the method header.
  • • Both abstract class and method must be declared by using the keyword abstract.
  • • Since abstract class have no body, so it is not possible to estimate the total memory required to create object, so JVM can't create objects to an abstract class.
  • • We should create cub classes and all the abstract methods should be implemented in the sub classes.

Let's see a program where an abstract class Simple having one abstract method which is implemented in various sub classes.

Example:
import java.lang.*;
abstract class Calculation
{
	abstract void calculate(int a);
}
class A extends Simple
{
	void calculate(int a)
	{
		System.out.println("square="+(a*a));
	}
}
class B extends Simple
{
	void calculate(int a)
	{
		System.out.println("cube="+(a*a*a));
	}
}
class C extends Simple
{
	void calculate(int a)
	{
		System.out.println("square root="+Math.sqrt(a));
	}
}
class AbstractDemo
{
	public static void main(String args[])
	{
		A t1=new A();
		B t2=new B();
		C t3=new C();

		t1.calculate(2);
		t2.calculate(3);
		t3.calculate(9);
	}
}
Output

square=4
cube=27
square root=3.0


Points to remember:

  • Abstract classes are not Interfaces. They are different, we will study this when we will study Interfaces.
  • • An abstract class must have an abstract method.
  • Abstract classes can have Constructors, Member variables and Normal methods.
  • Abstract classes are never instantiated.
  • • When you extend Abstract class with abstract method, you must define the abstract method in the child class, or make the child class abstract.

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext