Abstract Classes and methods in java

Posted By on September 16, 2014

Download PDF
Casting an object
Java Exception Handling

A class which can not be instantiated is known as abstract class. In other words – you are not allowed to create object of Abstract class.

Abstract class declaration
Specifying abstract keyword before the class during declaration, makes it abstract. Have a look at below code:

abstract class AbstractDemo{
   public void myMethod(){
      //Statements here

Error!! – Object creation of abstract class is not allowed

abstract public class AbstractDemo{
   public void display(){
      //Code here
public class ConcreteDemo{
   public static void main(String args[])
      //Can't create object of abstract class - error!
      AbstractDemo obj = new AbstractDemo();


Unresolved compilation problem: Cannot instantiate the type AbstractEx1

Why we need an abstract class?

Let me explain this with an example. Suppose there is a class Animal. Also, there are few other classes like Cat, Dog and Horse. These classes extends Animal class so basically they are having few common habits(methods in technically) which they are inheriting from Animal class. Now, if you have understood the above example then you would have been able to figure out that creating object of Animal class has no significance as you can’t judge that the new object of Animal class will represent which animal. Hence for such kind of scenarios we generally creates an abstract class and later concrete classes extends these classes and overrides their methods accordingly.

Abstract vs Concrete

A class which is not abstract is referred as Concrete class. In the above example which I explained – Animal is a abstract class and Cat, Dog and Horse are concrete classes.

Key Points:

  1. An abstract class has no use until unless it is extended by some other class.
  2. If you declare an abstract method (discussed below) in a class then you must declare the class abstract as well. you can’t have abstract method in a non-abstract class. It’s vice versa is not always true: If a class is not having any abstract method then also it can be marked as abstract.
  3. Abstract class can have non-abstract method as well.

Abstract methods

Apart from having abstract class you can have abstract methods as well.


public abstract void display();

Points to remember about abstract method:
1) Abstract method has no body.
2) Always end the declaration with a semicolon(;).
3) It must be overridden. An abstract class must be extended and in a same way abstract method must be overridden.
4) Abstract method must be in a abstract class.

Note: The class which is extending abstract class must override (or implement) all the abstract methods.

Example of Abstract class and method

abstract class Demo1{
   public void disp1(){
     System.out.println("Concrete method of abstract class");
   abstract public void disp2();

class Demo2 extends Demo1{
   /* I have given the body to abstract method of Demo1 class
   It is must if you don't declare abstract method of super class
   compiler would throw an error*/
   public void disp2()
       System.out.println("I'm overriding abstract method");
   public static void main(String args[]){
       Demo2 obj = new Demo2();


I'm overriding abstract method
Casting an object
Java Exception Handling

Download PDF

Posted by Akash Kurup

Founder and C.E.O, World4Engineers Educationist and Entrepreneur by passion. Orator and blogger by hobby

Website: http://world4engineers.com