Abstraction through the use of Abstract Classes in Java


Abstraction in Java, the powerful function of OOP is provided with the core of Java. Use of Abstract class is explained in this article.

The Abstract Class is a specially designed to act as base class only. We know abstract class as the class which can not be instanciated. only the references of abstract class can be used in program. Basically the purpose of abstract class is to keep the code clear and organised.

This can be done in two possible ways, one by complete abstrction or by partical abstraction. To make any class abstract you need to use the "abstract" keyword. The abstract keyword tells compiler to give any class or method abstract properties. Look at the following program this shows a demo of the full abstraction.


public abstract class Abstraction {
    public abstract int abstractMethodA(int a);
    public abstract String abstractMethodB(String s);
}

class UseAbstraction extends Abstraction{
    public int abstractMethodA(int a){
        return 0;
    }
    
    public String abstractMethodB(String s){
        return "";
    }
}

If a class is abstract you must make all its method abstract too or else system won't allow you to compile. As I already told you that abstract class serves as base class in general cases. And the complete abstraction means you will Implement all the declared method in the sub class.

The partial abstraction is also simple but little bit different. This is so in such a way that you want to implement some methods in one class and other in another class. for this purpose you will have to make the subclass abstract as it is going to contain un implemented abstract method. as you may see it in following program.



public abstract class Abstraction {
    public abstract int abstractMethodA(int a);
    public abstract String abstractMethodB(String s);
}

abstract class UseAbstraction extends Abstraction{
    public int abstractMethodA(int a){
        return 0;
    }
}

class UseAbstraction1 extends UseAbstraction{
    public String abstractMethodB(String s){
        return "";
    }
}

The program contains the abstract class which is same as in earlier program. Then we want to implemennt only one method and leave the other. So we shall make the class abstract and as in this way we are going to implement only one method, that is why we call it partial abstraction. And than we can extend this class into another class to implement rest of method. And complete the abstraction later on.