Abstract class constructor and Interface abstract

Abstract class constructor and Interface abstract

These are two different concepts which can be explained easily by a novice having a little bit knowledge of abstract classes and interfaces.

It is advised to read abstract classes before going through this topics. It gives thorough understanding of abstract classes where some concepts you may not aware of, perhaps.

Before going further let us know the difference between constructor and a method.

Abstract class constructor

1. What is the difference between a constructor and method?

A constructor is called implicitly whenever an object is created. But a method is to be called explicitly.

A constructor cannot be static but a method can be. When the following code is compiled. the compiler complains "modifier static not allowed here".

2. Can an abstract class have a constructor?

Yes, it can have and moreover can be overloaded as constructor is special type of method where return type is missing (and class name and constructor name should be same).

Following program compiles successfully and executes happily.

The above program proves an abstract class can have constructors. The same technique of "this" can be used to access another constructor as in concrete classes.

Using subclass constructor, the super class abstract methods are accessed. The principle used is subclass constructor calls superclass default constructor implicitly.

Interface abstract

3. Can an interface be abstract?

Infact, interface is an abstract class with one restriction – all methods should be abstract. With this small constraint, designers allowed multiple inheritance. Declaring an interface as abstract (not allowed by the compiler) does not carry any special meaning as interface implicitly is an abstract class.

4. Can an interface have constructors?

Sorry, not possible as constructor is nothing but a concrete (non-abstract) method that carries a body. Interface cannot have concrete methods.

What access modifiers a constructor cannot be?
"A constructor cannot be abstract, static, final, native, strictfp, or synchronized". Explained clearly in Java Constructor Properties

1. One mind teasing concept: Can you get objects of Abstract classes?
2. One more mind teasing concept: Can you get objects of Interfaces

Answer for the above two questions is "YES".

13 thoughts on “Abstract class constructor and Interface abstract

  1. Rajesh

    Declaring an interface as abstract is allowed by the compiler.
    public abstract interface Inter {
    public abstract void m1();
    public void m2();

    and there is no compilation error

  2. Prithu Yadav

    Yes a constructor can be private, to make any class as Singleton we need declare private constructor. A class who have private constructor can initialized just once.

  3. zakir

    Hello Sir,

    Your tutorial is simple, awesome, in great detail, easy to understand for novice & newbies like me, Could you please include advanced java concepts also.

    From my side there is no negative comment to make on your material, the only request i can do is, please keep the tutorial in sequential order and include advanced java concepts also.

    Thank you,

Leave a Reply

Your email address will not be published. Required fields are marked *