1. Home >
  2. Apps >
  3. Groups >

What is need of abstract class in java ?

Question asked by WARDHAMAN BEDMUTHA in #Java on Jul 23, 2015
WARDHAMAN BEDMUTHA
WARDHAMAN BEDMUTHA · Jul 23, 2015
Rank D2 - MASTER

Abstract class is a class in java which can not be instantiated. In simple terms you can not create object of that class.

Why do we need of Abstract Class then ?

Lets consider we have one parent Class Board and subclass like WhiteBoard and BlackBoard.

proxy.php?image=http%3A%2F%2Fblob:http%253A%2F%2Fwww.crazyengineers.com%2Fea052790-d0ac-4b49-b141-2bcbc96eb615&hash=8d1fcf383d0e9b2698d5a8f97ca8cf4a 17652-upload_2015-7-24_10-30-2.png

Board B =new WhiteBoard() ----- This is valid

WhiteBoard=new WhiteBoard() ---- This is valid

BlackBoard =new BlackBoard() -- This is valid

Board B =new Board() -- what type of object will be Board?What will be its color, length and width?

Will it make any sense to create object of type Board ?

Simply No.We will unable to know type of object created from Board.So in order to avoid creation of Object of Board we can mark this class as Abstract.

Abstract classes are the classes which can not be instantiated.

In above example, Board should be declared as Abstract while designing.

WhiteBoard and BalckBoard are the concrete classes.

Concrete classes are the classes which can be instantiated.

Posted in: #Java
manishsvkumarsingh
manishsvkumarsingh · Jul 26, 2015
Rank E1 - BEGINNER
GOOD CONCEPT @wardhaman
Divisha Madupalli
Divisha Madupalli · Feb 12, 2020
Rank C1 - EXPERT

Java has an abstract class a abstract method where that method can have or do not have any implementation in it. It is not compulsory for the method to implement a body. abstract keyword is used to create an abstract class or an abstract method.

An abstract class is mostly used to provide a base for subclasses to extend and implement the abstract methods and override or use the implemented methods in abstract class. 

public abstract class Person 

{

        private String name;    

 private String gender;

        public Person(String nm, String gen)

{         this.name=nm;   

      this.gender=gen;  

   }        //abstract method   

 public abstract void work();      

  public String toString(){ 

        return "Name="+this.name+"::Gender="+this.gender;    

}

   public void changeName(String newName) 

{         this.name = newName;

     } 

    } 

public class Employee extends Person {

    private int empId;    

    public Employee(String nm, String gen, int id) {

        super(nm, gen);

        this.empId=id;

    }

    public void work() {

        if(empId == 0){

            System.out.println("Not working");

        }else{

            System.out.println("Working as employee!!");

        }

    }

    public static void main(String args[]){

        //coding in terms of abstract classes

        Person student = new Employee("Dove","Female",0);

        Person employee = new Employee("Pankaj","Male",123);

        student.work();

        employee.work();

        //using method implemented in abstract class - inheritance

        employee.changeName("Pankaj Kumar");

        System.out.println(employee.toString());

    }

}

The purpose of abstract classes is to function as base classes which can be extended by subclasses to create a full implementation. For instance, imagine that a certain process requires 3 steps:

  1. The step before the action.
  2. The action.
  3. The step after the action.

If a class has an abstract method, the whole class must be declared abstract. Not all methods in an abstract class have to be abstract methods. An abstract class can have a mixture of abstract and non-abstract methods.

Subclasses of an abstract class must implement (override) all abstract methods of its abstract superclass. The non-abstract methods of the superclass are just inherited as they are. They can also be overridden if needed.


You must log-in or sign-up to reply to this post.

Click to Log-In or Sign-Up