Wednesday, October 5, 2016

Abstract classes and Methods

Abstract Classes and Methods in Java

A class that is declared using “abstract” keyword is known as abstract class. It may or may not include abstract methods which means in abstract class you can have concrete methods (methods with body) as well along with abstract methods ( without an implementation, without braces, and followed by a semicolon). An abstract class can not be instantiated (you are not allowed to create object of Abstract class).
Abstract class declaration
Specifying abstract keyword before the class during declaration, makes it abstract. Have a look at below code:
// Declaration using abstract keyword
abstract class AbstractDemo{
   // Concrete method: body and braces
   public void myMethod(){
      //Statements here

   // Abstract method: without body and braces 
   abstract public void anotherMethod();
Since abstract class allows concrete methods as well, it does not provide 100% abstraction. You can say that it provides partial abstraction. Interfaces are used for 100% abstraction (full abstraction)
Remember two rules:
1) If the class is having few abstract methods and few concrete methods: declare it as abstract class.
2) If the class is having only abstract methods: declare it as interface.
Error!! – Object creation of abstract class is not allowed
As discussed above, we cannot instantiate an abstract class. The following code throws an error.
abstract public class AbstractDemo{
   public void myMethod(){
   abstract public void anotherMethod();
public class ConcreteDemo{

   public void anotherMethod() { 
        System.out.print("Abstract method"); 
   public static void main(String args[])
      //Can't create object of abstract class - error!
      AbstractDemo obj = new AbstractDemo();
Unresolved compilation problem: Cannot instantiate the type AbstractEx1
Note: The class that extends the abstract class, have to implement all the abstract methods of abstract class, else they can be declared abstract in the class as well.

Why we need an abstract class?

Let me explain this with an example. Suppose there is a class Animaland there are few other classes like CatDog and Horse. These classes extends Animal class so basically they are having few common habits(methods in technically) which they are inheriting from Animal class. Now, if you have understood the above example then you would have been able to figure out that creating object of Animal class has no significance as you can’t judge that the new object of Animal class will represent which animal. Hence for such kind of scenarios we generally creates an abstract class and later concrete classes extends these classes and overrides their methods accordingly and can have their own methods as well.

Abstract vs Concrete

A class which is not abstract is referred as Concrete class. In the above example which I explained – Animal is a abstract class and Cat,Dog and Horse are concrete classes.
Key Points:
  1. An abstract class has no use until unless it is extended by some other class.
  2. If you declare an abstract method (discussed below) in a class then you must declare the class abstract as well. you can’t have abstract method in a non-abstract class. It’s vice versa is not always true: If a class is not having any abstract method then also it can be marked as abstract.
  3. Abstract class can have non-abstract method (concrete) as well.

Abstract methods

Well, we already discussed about abstract methods in the above section. Lets take few examples to understand it better.
public abstract void display();
Points to remember about abstract method:
1) Abstract method has no body.
2) Always end the declaration with a semicolon(;).
3) It must be overridden. An abstract class must be extended and in a same way abstract method must be overridden.
4) Abstract method must be in a abstract class.
Note: The class which is extending abstract class must override (or implement) all the abstract methods.
Example of Abstract class and method
abstract class Demo1{
   public void disp1(){
     System.out.println("Concrete method of abstract class");
   abstract public void disp2();

class Demo2 extends Demo1{
   /* I have given the body to abstract method of Demo1 class
   It is must if you don't declare abstract method of super class
   compiler would throw an error*/  
   public void disp2()
       System.out.println("I'm overriding abstract method");
   public static void main(String args[]){
       Demo2 obj = new Demo2();
I'm overriding abstract method

from :

No comments:

Post a Comment