Updates
  • Starting New Weekday Batch for Full Stack Java Development on 15 September 2025 @ 02:00 PM to 04:00 PM
  • Starting New Weekday Batch for MERN Stack Development on 29 September 2025 @ 04:00 PM to 06:00 PM
Join Course

Java Full Stack Developer Training Center in Noida

Abstract Class

In Java an Abstract Class is a special type of class having certain important features that are very useful in Application development. Let us begin the discussion on abstract class with abstract method.

• An Abstract Method is a method which does not contain a body.
• When we declare a method without a body in Java program then we get an error at the time of compilation.

            
class JTC{
	void m1();
}
        
    test.java:2: error: missing method body, or declare abstract
        void m1();
             ^
    1 error
    error: compilation failed
            

• When we define a method without a body then we should declare that method using abstract keyword. When we declare an abstract method in a class then we should declare that class along with an abstract keyword, otherwise we will get an error at the time of compilation.

            
abstract class JTC{
	abstract void m1();
}
        

• An abstract class can contain different type of member like Instance / Static Variable, Instance / Static Block, Instance / Static Method, Constructors etc. just like a concrete class.

• We cannot create an object of an abstract class.

            
abstract class A{
	abstract void m1();
	void m2(){
		System.out.println("m2 in A class");
	}
}
class JTC{
	public static void main(String arg[]){
		A a1 = new A();
		a1.m2();
	}
}
        
    test.java:9: error: A is abstract; cannot be instantiated
        A a1 = new A();
               ^
    1 error
    error: compilation failed
            

In the above example we can see that we have an abstract class A. Class A contains an abstract method abstract void m1() and an instance method void m2(). In JTC class we are creating an object of A class and along with that object we are invoking m2().We discussed above that object of an abstract class cannot be created, so we will get an error at compile time error: A is abstract; cannot be instantiated.

Q. Why we cannot create an object of an abstract class?

Answer: - When we use an abstract keyword to define a class, that class is incompletely defined. Also, an abstract class can contain an abstract method. Due to these reasons, there is no provision for creating objects of an abstract class.

            
abstract class Details{
	int id;
	String name;
	String email;
}
class Student extends Details{
}
class Teacher extends Details{
}
class Admin extends Details{
}
class JTC{
	public static void main(String arg[]){
		/*Details d1 = new Details();
		d1.id = 101;
		d1.name = "Vivek";
		d1.email = "vivek@jtcindia.org";*/
		
		Student s1 = new Student ();
		s1.id = 101;
		s1.name = "Vivek";
		s1.email = "vivek@jtcindia.org";	

		
	}
}
        

In the above example as we can see we have Student class, Teacher class and Admin class that represent Student, Teacher and Admin categories respectively. As per the business logic we need to store certain details for all categories, so we have declared a class which is Details class for storing details of all the categories. That is the reason Student, Teacher and Admin classes extend the Details class

In the next section of the above example we have a JTC class where we are creating an object of Details class and storing the values of id, name and email along with the object of Details class, and everything may look fine but here we have a problem and that is that object creation of Details class is a senseless activity-this is because in the above program it is not possible to make out whether Vivek is a Student or Teacher or an Admin.

On the other hand, when we create an object of Student class injecting the same data into the class level variables which are id, name and email- we can now make sense as we can make out that Vivek is a Student and his id is 101 and email is vivek@jtcindia.org.

Hence in this case we declare Details class as an abstract class. Now no one can directly create an object of Details. Since Details is declared as an abstract class, it is an incomplete class and it always makes sense along with a sub class.

• When a class extends an abstract class then we should override all the abstract methods of abstract class into its sub class, or else we should declare that sub class as an abstract class.

            
class JTC {
	public static void main(String[] args) {
		Animal ani=null;
		ani=Animal_Factory.getAnimal(Animal_Factory.DOG);
		show(ani);
		ani=Animal_Factory.getAnimal(Animal_Factory.CAT);
		show(ani);
}
static void show(Animal ani){
	ani.eating();
	ani.sleeping();
	}
} 
abstract class Animal{
	public abstract void eating();
	public abstract void sleeping();
}
abstract class Dog extends Animal{
	public void eating(){
		System.out.println("Dog is eating");
	}
}
class MyDog extends Dog{
	public void sleeping(){
		System.out.println("MyDog is sleeping");
	}
}
abstract class Cat extends Animal{
	public void eating(){
		System.out.println("Cat is eating");
	}
	public void sleeping(){
		System.out.println("Cat is sleeping");
	}
}
class MyCat extends Cat{
}
class Animal_Factory{
	static final int DOG=1;
	static final int CAT=2;
	static Animal getAnimal(int a){
		Animal ani=null;
		if(a==1){
			ani=new MyDog();
		}else if(a==2){
			ani=new MyCat();
 		}
	return ani;
	}
}

        
    Dog is eating
    MyDog is sleeping
    Cat is eating
    Cat is sleeping
            

In this example we have a class called Animal class which is an Abstract class having two abstract methods- eating () and sleeping (). Dog class is extending Animal class and but in Dog class we are only overriding one method that is eating (), so we have declared Dog class an abstract class.

There is one more sub class which is MyDog and as we can see it is a sub class of Dog class and it is overriding the method sleeping () which is not overridden in Dog class.

Then the class Cat is extending Animal class and it is overriding both eating () and sleeping () method but as we know an abstract class may not contain an abstract method and so finally MyCat class is extending Cat class.

In Animal_Factory class we have a method which is getAnimal (int a) that returns different sub class objects of Animal class on the basis of Arguments, as we can see when we pass 1 then it returns an object of MyDog Class and similarly for 2 it returns MyCat class object.

And finally we have JTC class where we are processing all modules using show() method of JTC class.