Search code examples
javaoopabstract-class

what happen when somebody calls super in constructor of a class that inherit from abstract class?


I'd like to know what really happens when somebody calls java super() method in the constructor of a class that inherits from an abstract class. As we know it, an abstract class cannot be instantiated but super calls the constructor of the abstract class.

Let's take an example.

public abstract class Figure { // cant be instantiated
    private type firstAttribute;
    private type secondAttribute;

    public Figure(type firstAttribute, type secondAttribute) {
        this.fisrtAttribute = fisrtAttribute;
        this.fisrtAttribute = fisrtAttribute;
    }

    protected abstract void anyMethod();
}

class Rectangle extends Figure {
    public Rectangle(type firstAttribute, type secondAttribute) {
        
        /*
         * call the abstract class constructor but we know, a 
         * constructor is used to
         * instantiate class and we can't instantiate an abstract class
         */
        super(firstAttribute, secondAttribute);
        
        // then what really happen ?
    }

     @override
     ....
}

Solution

  • The abstract base class initialised any properties you inherit in your concrete inheriting class, so you can avoid code duplication in the initialisation