Java Abstract Classes
Abstract Classes is used for general purpose, This class are not create any instance. But this class can be possible to inherited by other class. We can define two type of method in this abstract class.
Abstract methods: This is special method of abstract class, They are provided function prototype. In this class abstract function are not define body or any statement. That function definition are implemented by all inherit class.
abstract class dataCollection{
//abstract method
abstract void setTtem(int data);
abstract int countItem();
//Nonabstract method
void info(){
//logic here
}
}
class Person extends dataCollection{
//defination of abstract method
void setTtem(int data){
//logic here
}
int countItem(){
int record=0;
//logic here
return record;
}
}
abstract keyword are used to declare abstract class and method. When we are define abstract method so that are not contain body and end with semicolon.
Nonabstract methods: This is an normal function of this class which can be inherited by other class. And function are implemented by abstract class.
abstract class Interview{
boolean selectionStatus;
Interview(){
selectionStatus=false;
}
abstract void aboutYourSelf(); //abstract method
//non abstract methods
void appointment(boolean status){
selectionStatus=status;
}
void result(){
if(selectionStatus == true){
System.out.println("Welcome, You are selected");
}else{
System.out.println("Better luck next time");
}
}
}
class Person extends Interview{
String name;
int age;
String qualification;
Person(String name,int age,String qualification){
this.name = name;
this.age = age;
this.qualification = qualification;
}
//Implement abstract method of Interview Class
void aboutYourSelf(){
System.out.println("My name is : "+name);
System.out.println("My age is : "+age);
System.out.println("My qualification is : "+qualification);
appointment(true); //call Interview class method (Base class)
}
public static void main(String[] args) {
Person candidates = new Person("Foo",12,"Btech Software Engineer");
candidates.aboutYourSelf();
candidates.result();
}
}
Output
My name is : Foo
My age is : 12
My qualification is : Btech Software Engineer
Welcome, You are selected
In this example Interview is an abstract class, And that are contain two type of methods abstract(aboutYourSelf) and non abstract (appointment,result). abstract method must be implemented by inherit class. In this program abstract method are implementing by derived class (Person).
We cannot create a instance but we can be create a reference of abstract class. base class reference is capable to hold the instance (memory) of all derived class. This process is called dynamic binding. See this example.
abstract class Fruits{
//abstract method
abstract void taste();
void about(){
System.out.println("Fruit is very delicious");
}
}
class Lemon extends Fruits{
//display taste of lemon
void taste(){
System.out.println("Taste of Lemon is sour");
}
}
class Mango extends Fruits{
//display taste of Mango
void taste(){
System.out.println("Mango is Generally sweet, Raw mangoes taste is sour");
}
public static void main(String args[]){
//Create a reference of abstract class
Fruits reference;
reference=new Mango();
reference.taste();
reference.about();
reference=new Lemon();
reference.taste();
reference.about();
}
}
Output
Mango is Generally sweet, Raw mangoes taste is sour
Fruit is very delicious
Taste of Lemon is sour
Fruit is very delicious
Initially reference of Fruits class is hold the instance of Mango class.

We are know about derived class instance are capable to access base class method so in this example reference.taste() are execute mego class method and after that reference.about() are executing base class function.
Similar way Fruits Class reference variable are capable to hold Derived class Lemon reference.

This mechanism are implemented by polymorphism. And dynamic binding is also know as late binding. So function calling execution is decided when running of program in this case. Dynamic binding method are work to little slower as compared to static binding method. but this mechanism are very useful of most of cases.
Note that in above example reference variable is capable to hold only single reference of derived class instance. When we are assign new instance reference to that base class reference. Then this are work only this newly instance.
Better way we can create a function and pass that instance reference to derived class. See this example.
abstract class Fruits{
//abstract method
abstract void taste();
void about(){
System.out.println("Fruit is very delicious");
}
}
class Lemon extends Fruits{
private String flavors;
Lemon(String flavors){
this.flavors=flavors;
}
//display taste of lemon
void taste(){
System.out.println("Lemon, That tastes are " + flavors);
}
}
class Mango extends Fruits{
private String flavors;
Mango(String flavors){
this.flavors=flavors;
}
//display taste of Mango
void taste(){
System.out.println("Mango, is Generally "+flavors+", Raw mangoes taste is sour");
}
//Accepting any instance reference of derived class
void checkTaste(Fruits ref){
System.out.print("Your favorite fruit is ");
ref.taste();
ref.about(); //execute base class method
}
public static void main(String args[]){
Mango mango=new Mango("sweet");
Lemon lemon=new Lemon("sour");
mango.checkTaste(lemon); //passing the instance reference
mango.checkTaste(mango);
}
}
Output
Your favorite fruit is Lemon, That tastes are sour
Fruit is very delicious
Your favorite fruit is Mango, is Generally sweet, Raw mangoes taste is sour
Fruit is very delicious
In this example we are create two instance of Derived class (lemon and mongo).

Similar way we can be pass the other derived class reference to the checkTaste() function. In this example that are ref.taste() method are executes the Lemon class method.
There is possible to create an array of abstract class reference, which are capable to hold the instance of derived class. We know about the array that is combination of similar type of elements. In this case each element is store and hold the reference of derived class instance.
abstract class Fruit{
//abstract method
abstract void taste();
}
class Orange extends Fruit{
private String flavors;
Orange(String flavors){
this.flavors=flavors;
}
//display taste of lemon
void taste(){
System.out.println(flavors);
}
}
class Mango extends Fruit{
private String flavors;
Mango(String flavors){
this.flavors=flavors;
}
//display taste of Mango
void taste(){
System.out.println(flavors);
}
public static void main(String args[]){
//Create array of Fruits types
Fruit[] variety = new Fruit[4];
//Create and assign derived class instance reference
variety[0] = new Mango("Sweet Mango");
variety[1] = new Mango("Sour Mango ");
variety[2] = new Orange("Sweet Oranges");
variety[3] = new Orange("Sour Oranges");
//Execute methods of derived class
variety[0].taste();
variety[2].taste();
}
}
Output
Sweet Mango
Sweet Oranges

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment