×
☰ See All Chapters

Java abstract class

Showing Essential properties and methods of an object by hiding internal things is called as Abstraction. Abstract class is used to achieve abstraction. Abstraction is possible if and only if there is encapsulation. Example: Java is an abstraction layer over binary language.  Abstract class is user defined data type.  Abstract class is used to do partial implementation and setting the standards for the subclasses to do the implementation. A class which is declared with abstract keyword is called as abstract class. Abstract class can have all the members as normal class like constructor, blocks, variables, methods. Additionally abstract class can declare abstract methods.

abstract class ClassName{

        //members of abstract class

}  

From abstract class you can set the standards for the subclasses to do the implementation by declaring abstract methods. An abstract method is a method that is declared without any implementation (without braces, and followed by a semicolon).

access_specifier abstarct  returntype  methodname (type1 pmtr1, type2 pmtr2, type3 pmtr3..);

Example:

abstract void calculate(int a, int b);

Any number of classes can implement abstract class. To implement abstract class, implementation class has to extend the abstract class.

class ImplementationClass extends Abstractclass {

        // implementation for abstract class abstract methods

}

When an abstract class is sub classed, the subclass must override the abstract method and provide implementation for all of the abstract methods in its parent abstract class. If it does not implement abstract methods, then subclass must also be declared abstract

abstract class A {

 

        abstract int add(int a, int b);

 

        abstract int subtract(int a, int b);

}

 

abstract class B { // abstract because does not implement subtract method

 

        int add(int a, int b) {

                return a + b;

        }

 

}

 

class C extends A {

 

        int add(int a, int b) {

                return a + b;

        }

 

        int subtract(int a, int b) {

                return a - b;

        }

}

 

There are two ways to achieve abstraction in java:

    1. Using Abstract class (0 to 100%) 

    2. Using Interface (100%) 

If abstract class declares all methods as abstract methods then it comes to 100 % abstraction, if abstract class has concrete methods then it is not 100% abstraction. Abstract class may contain both concrete and abstract methods. But if a class has an abstract method, then the class should be declared as abstract. A class containing all concrete methods can still be declared as abstract. This is done generally if you do not want to create an instance of a particular class.

A Simple demonstration of abstract class

abstract class A {

        abstract void callme();

 

        void callmetoo() {// concrete methods are still allowed in abstract classes

                System.out.println("This is a concrete method.");

        }

}

 

class B extends A {

        void callme() {

                System.out.println("B's implementation of callme.");

        }

}

 

class AbstractDemo {

        public static void main(String args[]) {

                B b = new B();

                b.callme();

                b.callmetoo();

        }

}

 

Output:

B's implementation of callme.

This is a concrete method.

Abstract classes cannot be instantiated, but you can declare a reference variable of it

//Using abstract methods and classes.

abstract class Figure {

        double dim1;

        double dim2;

 

        Figure(double a, double b) {

                dim1 = a;

                dim2 = b;

        }

 

        // area is now an abstract method

        abstract double area();

}

 

class Rectangle extends Figure {

        Rectangle(double a, double b) {

                super(a, b);

        }

 

        // override area for rectangle

        double area() {

                System.out.println("Inside Area for Rectangle.");

                return dim1 * dim2;

        }

}

 

class Triangle extends Figure {

        Triangle(double a, double b) {

                super(a, b);

        }

 

        // override area for right triangle

        double area() {

                System.out.println("Inside Area for Triangle.");

                return dim1 * dim2 / 2;

        }

}

 

class AbstractAreas {

        public static void main(String args[]) {

                // Figure f = new Figure(10, 10); // illegal now

                Rectangle r = new Rectangle(9, 5);

                Triangle t = new Triangle(10, 8);

                Figure figref; // this is OK, no object is created; creating reference variable of abstract class

                figref = r;// abstract class (super class) reference is assigned with subclass object

                System.out.println("Area is " + figref.area());// since area() method is overridden area() method in

                figref = t;

                System.out.println("Area is " + figref.area());// area() method in Triangle is called

        }

}

 


All Chapters
Author