logo codesdope

Abstract Class in Java


We will start this chapter by looking at an example of employees working in a firm. The firm hires only two types of employees- either driver or developer. Now, you have to develop a software to store the informations about them.

So, here is an idea - There is no need to make objects of the employee class. We will make objects of only driver or developer. Also, both must have some salary. So, there must be a common method to know about salary.

This need will be best accomplished with abstract class.

An abstract class is a class whose instances( objects ) can't be made. We can only make objects of its subclass ( if they are not an abstract ).

So, we can make 'Employee' an abstract class and 'Developer' and 'Driver' its subclasses.

Let's see an example on this, then you will be explained more about abstract class.

abstract class Employee{
  abstract int getSalary();
}
class Developer extends Employee{
  private int salary;
  public Developer(int s){
    salary = s;
  }
  int getSalary(){
    return salary;
  }
}
class Driver extends Employee{
  private int salary;
  public Driver(int t){
    salary = t;
  }
  int getSalary(){
    return salary;
  }
}
class abstract0{
public static void main(String[] args){
  Developer d1 = new Developer(5000);
  Driver d2 = new Driver(3000);
  int a, b;
  a = d1.getSalary();
  b = d2.getSalary();
  System.out.println("Salary of developer : " + a);
  System.out.println("Salary of driver : " + b);
}
}
Output
Salary of developer : 5000
Salary of driver : 3000

How to make an abstract class?

- 'abstract' modifier must be used before abstract class.

- Abstract class must contain one abstract method.
Abstract method is a method which has abstract modifier and an empty body. Abstract methods are defined in the subclasses.

Now let's go through the code
abstract class Employee - Employee is an abstract class as 'abstract' modifier is used.
Since, Employee is an abstract class so, there must be an abstract method in it. It is 'getSalary()'.
abstract int getSalary(){} - Notice that its body is left empty to be defined in the subclass and 'abstract' keyword is also used here.
In both the subclasses 'getSalary' method is defined.
You must have understood the rest of the code.

Note that

Developer d1 = new Developer(5000);
Driver d2 = new Driver(3000);

is same as

Employee d1 = new Developer(5000);
Employee d2 = new Driver(3000);

We can write any of the two codes.

Let's see one more example of abstract class.

abstract class Animals{  
   abstract void sound();
 }  
  
 class Dogs extends Animals{  
 void sound(){
  System.out.println("Dogs bark");
  }  
 }  
 class Cats extends Animals{  
 void sound(){
  System.out.println("Cats meow");
  }  
 }  
 class Monkeys extends Animals{  
 void sound(){
  System.out.println("Monkeys whoop");
  }  
 }  
 class abstract1{  
 public static void main(String[] args){  
  Animals d = new Dogs();  
  Animals c = new Cats();  
  Animals m = new Monkeys(); 
  d.sound();
  c.sound();
  m.sound();
 }  
}  
Output
Dogs bark
Cats meow
Monkeys whoop

The best way to predict the future is to create it.
-Peter Drucker


Download Our App.
BlogsDope App
Get it on Google Play
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.