Abstract Classes in Java

Abstract Classes in Java

Assume a real-life problem that you want to create a parent class in which a function is declared and now it's children classes want to take the same function that is name and arguments remain same but the body is to be different. Take a small example; In parent class let's say there is a function called greeting() which takes a string_name as an argument and then print: Hello name! but in children class you want to use this same function but want to print: Hey name!

This kind of problem is solved by Abstract classes.

In Abstract classes you can declare a body-less method and then use that function in it's children classes to give it a body.

Let's see how to create Abstract class and then abstract methods.

For creating abstract classes and methods we use abstract keyword.

public class abstractclasses {
    public static void main(String[] args) {

    }
    abstract class Parent{
        abstract void greeting();
        abstract int age(int age);
    }
}

Here we created an abstract class in which we declared one void type abstract method taking 0 arguments and an int type abstract method taking 1 argument. Let's create it's child class!

public class abstractclasses {
    public static void main(String[] args) {

    }
    abstract class Parent{
        abstract void greeting();
        abstract int age(int age);
    }
    class Chiild extends Parent{
        @Override
        void greeting() {
            System.out.println("Hello World!");
        }

        @Override
        int age(int age) {
            System.out.println("My age is "+age);
            return age;
        }
    }
}

Now you can see that we over-rided the body-less functions in child class and then gave them the body.

Some obvious points to be mentioned here:

  1. You can't create an object of Abstract classes because if you are able to do that then how will you call a body-less method for that object? But you can use them as reference variable!

  2. Constructors are allowed in abstract classes but can only be called via super in child classes.

  3. Absract constructors (when constructors are declared abstract) are not allowed, think if they were allowed then how could over-ride them in children classes? You can call the constructors by using super keyword but you can't over-ride them and as Parent abstract class don't have objects so you can't call them even.

  4. Static methods in abstract class are allowed because they don't need object (you can call them directly by the className.staticmethod)

  5. Abstract static methods are not allowed because one can't over-ride static methods (We can't over-ride static methods because they don't need object! how will Java decide which function to be called, normal one or that which is over-ridden?)

  6. If one of the method in a class is abstract then whole class needs to be abstract.

  7. final abstract class doesn't exist (final classes are not inherited).

  8. Multiple inheritance are not supported even for abstract classes.

I hope now Abstract Classes are no longer a problem!