Constructor allowed in abstract class but not in Interface

1.9k views Asked by At

Correct me , if I am wrong

The above title draw me close to the following conclusions :

1) Constructor is , nothing else , but a concrete method with class name and no return type , not even void .

2) And , abstract class can have both concrete and abstract methods ; so having a constructor in abstract class is just like having a concrete method . It is fine , till the constructor is not tried to be invoked within that abstract class .

As , for invoking , we need to create and object , which is a concept of instantiating , that is against the protocol of abstract class . Though , this constructor can be called after extending this abstract class into a concrete class and , creating an object of concrete class .

3) Interface cannot have a constructor , because it is purely abstract . It does not support concrete methods . And , hence not even constructor

2

There are 2 answers

0
SOFe On BEST ANSWER

First point

It is a method that is automatically invoked when an object is created. Consider it as an object constructed listener.

Second point

Subclasses must call the parent constructor in the very first statement. A constructor is an initializer of the object. As I said, it can be considered as an object construction listener.

Parameters

The parameters that the constructor accepts depends on how the caller constructs the object, and for abstract class parameters, how the subclass forwards params with super(...) is actually a part of the class definition of the subclass on how it extends the superclass. This also explains why super(); is not necessary if superclass constructor does not accept arguments.

Third point

Abstract classes are classes.

Abstract classes are classes too; don't discriminate them. A class is something that contains class fields and class methods; that's the very basic definition of a class, hence instances of a class are called objects. An abstract class satisfies that; just that some of its methods are not actual methods, but they are still methods from the caller's perspective. For instance, the caller does not need to know whether a method is abstract or not when calling it.

C++ comparison

If we look at C++, (which afaik is the source/inspiration/model/whatever for Java classes) we would find that class methods do not necessarily have an implementation (although if you don't, there will be an error, forgot runtime or compile time) but only the prototype. A Java abstract method is like a C++ method without a prototype. (I am not experienced with C++, so this part may be inaccurate)

This shows that, the meaning of abstract in an abstract class only means that it (i) cannot be instantiated and (ii) can contain abstract methods. An abstract method in a superclass means that this method should exist, but subclasses have to implement that themselves; and since an abstract class cannot be instantiated, it is safe to have an abstract (or if that irritates you, consider it as an empty/return null method) method in it.

What about interfaces?

On the other hand, a Java interface basically defines a list of methods that are expected to be present in the implementing class, but the interface itself is virtually not a class.

Why no constructor methods?

Why do you even want that? You do not instantiate an object from the interface's name.

If you are thinking about limiting arguments in the constructors, such as the registerSubclass(Class<? extends ThisSuperclass>) kind of register-types API, where you may have code like this:

abstract class Handler{
    public static <T> void registerHandler(Class<T extends Handler> clazz){
        Constructor<T> c = clazz.getConstructor(Event.class); // say, you have to construct subclasses to handle an event
        // a lot of try-catch trouble
    }
}

However, so far this feature does not exist in Java, and you have to check the constructors yourself (at least to my knowledge).

What about default methods?

Default methods are added in Java 8, where interfaces can have concrete methods. I am not familiar with the concept or edge cases of this new feature, but referring to the "car" analogy from the Java docs on interfaces, if interface Car is the customer's demand on what you need to produce so that they will buy your car, default methods in interfaces are like "I want air conditioning in the car, but since it may be difficult for you, I give you this air conditioning system I invented; you may make another air conditioning system though".

Why no default constructor in interfaces?

As I mentioned, constructors are like object constructed listener, so it is not a method from this perspective, but part of the class's structure (like the extends clause, the annotations, or other modifiers of the class). Interfaces define the methods required from you. A default method is just a dummy method, mostly useful for backwards compatibility in interfaces. But it is very unreasonable for a default constructor, because it isn't a method in this sense.

Disclaimer: I have never properly learnt Java from tutorials, so my understanding comes from my own observation for more than 2 years, but so far this seems to correctly explain Java classes. In practical programming, these concepts do not really matter as long as you get the semantics right, although it is easier to get the semantics right if you understand the concepts.

1
lazy rabbit On

If you are asking why or why not a abstract or interface class can(cannot) have a constructor then this might help :) and then this.