An abstract class is super useful to define common methods and attributes to a set of classes. I'll try and give an example:
Imagine you are designing an application for managing staff or whatever at a certain company. You will have different kinds of users:
- employees
- guests
- managers
- directors
Even though they have different characteristics, there are many common attributes such as name, id, age and so on. Following this thread of thought, we define a super class User:
public class User { public String name; public int age; public String id; public User(String name, int age, String id) { this.name = name; this.age = age; this.id = id; } //here would be the getters and setters }
Now, the other classes would be declared as extensions of this User class (check java heritance out).
public class Employee extends User{//define specific methods} public class Manager extends User{//as above} //and so on...
Now, although you want all these subtypes of user to inherit commons and attributes, you don't want to create Users per se, you don't want anyone to be of type User, only subtypes of the class. Therefore, you don't want User to be instantiated and that's why the keyword abstract is used.
By changing the User class declaration to abstract, you are telling that you don't want this class to be instantiated and prevent having users of type User, guaranteeing the only users you'll have created are Employee, Director, etc:
public abstract class User {}