Abstract Classes In Java


In java, abstract classes are the one that is declared abstract. The concept of abstract classes is really complicated for beginners to understand.

here is the simple question for you guys!!
What is the meaning of abstract in English? if not sure, better google it!
you will find something like this!!

abstract  : existing in thought or as an idea but not having a physical or concrete existence.

so this pretty much covers everything that contains in abstract class. You don't need to remember according to what java says. If you just simply remember what it means in English, you will get the idea what the abstract classes really are!



The picture shown below would help to  make your understanding clear

As you can see in the picture, abstract classes are the classes which are incomplete.



Lets now understand what Java says about it...

- To make classes abstract you need to use  abstract keyword.
- You cannot instantiate the abstract classes. (In another words, you cannot create an object of abstract class. )
- Abstract classes are incomplete classes, means, any method that you define in abstract classes are without it's implementation. Lets see one example 




abstract class Person {

String personName = "";
abstract void printInfo();     

// Incomplete : Method's definition(signature is only defined)
}            

// method without implementation.

class Student extends Person{  
 
/* Its rule, If any class extends to abstract class, then that class has to implement incomplete methods 
specified in abstract class this class is subclass of Person(abstract class) 
so you have to implement this abstract method if you extend it */

    public void printInfo(){

        System.out.println("You have to implement this method when you extend to abstract class");

    }

}


 Points To Remember
  • Abstract classes are incomplete. Sub-classes (The classes that extends abstract class) must implement the "incomplete method definition". In another words, abstract classes have just method without implementation, but when you extend, that class has to provide its implementation to make it complete. 
  • You can make one or more method abstract in abstract classes. 
  • If you declare one method abstract, for example : abstract void printInfo() then you have to declare the class itself as an abstract class. 
  • You cannot create an object of abstract class. 
  • Subclasses of abstract class must provide an implementation of the abstract method of its superclass or themselves to be declared as abstract class. 
  • Important: abstract classes can have instance variables. It can also have concrete methods ( methods with implementation) but when you declare your class as an abstract, it must have atleast one abstract method. 
  • you cannot declare static and constructor methods abstract in java.
What is the purpose of abstract class ?


An abstract class's purpose is to provide an appropriate superclass from which other classes can inherit and can share a common design, also it gives you very good feature of hiding your implementation details which is known as abstraction in java. 

Lets see one example. Please look and read my comments in code carefully. It will help you understand more. Also if you want to test it by yourself, put below three pieces of program together and run in IDE of java.

This first class is to test with main method.
 
public class MyTest {

 public static void main(String[] args){
 
// you cannot create an object of abstract class. This statement will give you an error
// **MySuperClass abstClass = new MySuperClass(); ** 

 //you can create an object of subclass, because it is not an abstract class
 
 MySubClass subclass = new MySubClass();
 subclass.printInfo(); // will print the message from subclass. 

 }
}



below shown is the abstract class. you can declare abstract class using abstract keyword in java.


Notice carefully, it has just defined method, but it doesn't have implementation. Which means it is incomplete. Also its subclass's responsibility to implement those incomplete method when extend it with abstract classes.  


// abstract classes just contains the definition of method (no implementation). 
// It can have method with implementation but one method must be declared as ab abstract (without implementation).

abstract class MySuperClass {

 abstract void printInfo();
 //must have one method abstract in abstract class. You can make your method abstract by using abstract keyword.
}



below is the class, which extends with abstract class, which means that this class have to implement the incomplete methods of its superclass (abstract class).



class MySubClass extends MySuperClass {  

//since this class extends the abstract class, it must implement the abstract methods.

 @Override
 public void printInfo(){
  System.out.println("abstract method implementation - if you don't it will cause an error.");
 }

}
 



When should you use abstract classes? 

  •    You want to share code among several closely related classes.
  •    You expect that classes that extend your abstract class have many common methods or fields,        or require access modifiers other than public (such as protected and private).
  •    You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.
another example to understand.


    abstract class Bike{  
abstract void run();
}

class Honda4 extends Bike{
void run(){
System.out.println("running safely..");
}

public static void main(String args[]){
Bike obj = new Honda4();
obj.run();
}
}
The important thing is to be aware of the rules, and in what kind of situation you should use abstract class. I hope that two things are pretty clear after reading this blog.
if you guys have any questions, please leave your comment
Thanks Guys for reading! Happy Learning! 



Post a Comment

0 Comments