Java – Abstract Class

ABSTRACT CLASS:

Every class contain its own variables and methods. Each and every object uses those variables and methods. Suppose if the object needs its own way to perform operations, then we need abstract class.

Abstract class is a class which are declared abstract and cannot be instantiated. We cannot create object to an abstract class. We need subclasses which extents the abstract class to use it.

Consider you run a website where you teach how to play cricket, football, hocket etc. You are asking the learners to create a profile. Then you teach them the rules and regulations of the game and how to play the game.

class Sportsmen{

void createProfile{

}

void howToPlay{

}

}

But all game doesn’t have same rules and same playing methods. So the body of the methods howToPlay() is left empty and is called abstract method.

abstract method is a method which has no body. Then we create subclass called CricketPlayer to use the abstract method.

class CricketPlayer extends Sportsmen{

void createProfile{

}

void howToPlay{

//code to teach cricket

}

}

class FootBallPlayer extends Sportsmen{

void createProfile{

}

void howToPlay{

//code to teach football

}

}