Master java skills

Abstract classes in Java

An abstract class is the one which is considered to have some kind of abstraction and therefore cannot be instantiated. It means, we cannot create objects of an abstract class. Abstract classes are another way of achieving abstraction in java.

To declare an abstract class, we need to use ‘abstract’ keyword just before the class name. Such as, in the below example, Player is an abstract class.

public abstract class Player {
    //some code here
}

Some important points about abstract classes

  1. Abstract classes use abstract keyword just before their name.
  2. Abstract classes cannot be instantiated (objects cannot be created from abstract class). However, they can have constructors.
  3. Abstract classes can have zero or any number of abstract methods.
  4. An abstract class can have all its methods as abstract.
  5. Abstract classes can have variables as well as constants.
  6. Abstract classes can have combination of any number of abstract and non-abstract methods.
  7. Abstract classes can have final and static methods also.

When to use abstract class

Now, since we have understood a little about abstract classes, the question arises when to use an abstract class. So, when we know some behviour of a class well in advance and some behaviour is not well known, in those scenarios, we should use abstract classes. Let’s understand this with one real world example

A player is someone who plays some sort of sports. So, some of the behaviour of a player is already known such as a player plays, a player is a fit person, a player represents the nation he playes for etc. Whereas some of the behaviour is not clear such as which sports he plays. What equipment he needs. All these things are known as the player becomes specific to a game.

Player Class

package com.javatrainingschool;

public abstract class IndianPlayer {

	public static final String COUNTRY = "India";
	
	private int age;
	
	private boolean isFit;
	
	public void representsCountry() {
		System.out.println("Indian Player represents " + COUNTRY);
	}
	
	public void passIndiaFitnessTest() {
		if(age < 30) {
			this.isFit = true;
		}
	}
	
	public abstract void play();
}

Next, we will create one more class IndianCricketer which extends IndianPlayer. It will have all the public, protected behaviour of IndianPlayer class and will override abstract method of IndianPlayer class.

package com.javatrainingschool;

public class IndianCricketer extends IndianPlayer {

	private String name;
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public void play() {
		System.out.println(this.name + " plays cricket for India");
	}
	
	public static void main(String[] args) {
		
		IndianCricketer indianCricketer = new IndianCricketer();
		indianCricketer.setName("Rohit Sharma");
		
		indianCricketer.play();
		indianCricketer.representsCountry();
	}
}
Output :

Rohit Sharma plays cricket for India
Indian Player represents India