Why are constructors not inherited in java?

后端 未结 12 1175
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 14:24

I am a beginner in java programming language, recently I have studied that constructors can not be inherited in java, Can anyone please explain why<

相关标签:
12条回答
  • 2020-11-27 15:17

    What you are talking about is Java language level. If constructors were inherited, that would make impossible to make class private. As we know method visibility can't be downgraded. Object class has a no argument constructor and every class extends Object, so in case of constructor inheritance every class would have a no argument constructor. That breaks OO principles.

    Things are different on bytecode level. When object is created, two operators are called:

    1. new - allocates memory for object
    2. invokespecial - calls constructor on newly allocated piece of memory

    We can modify bytecode so that memory is allocated for Child class and constructor is called from Parent class. In this case we can say that constructors are inherited. One notice if we don't turn off byte code verification, JVM will throw an exception while loading class. We can do this by adding -noverify argument.

    Conclusion:

    1. Constructors are not inherited on language level due to OO principles
    2. Constructors are inherited on bytecode level
    0 讨论(0)
  • 2020-11-27 15:18
    1. If constructors were inherited in child class, then child class would contain a parent class constructor which is against the constraint that constructor should have same name as class name.
    2. However, parent class constructor can be called from child class constructor using super().

    An example code to demonstrate the concept.

    class Base{
       public void Base()
       {
           System.out.println("Inside Base class constructor");
       }
    }
    class Derived{
       public void Derived()
       {
           super();
           System.out.println("Inside Derived class constructor");
       }
    class mainClass{
       public static void main(String args[])
       {
           Derived obj = new Derived();
       }
    
    
    Output:
    Inside Base class constructor
    Inside Derived class constructor
    
    0 讨论(0)
  • 2020-11-27 15:21

    Simple answer I observed, You cannot invoke or use constructors of parent class in child class directly but methods of parent class you can use directly in child class.

    In case you have method in child class with same name as in parent class at that time only you need to use "super" keyword to invoke parent class method resolve call ambiguity.

    "To invoke" parent class constructor in child class you always need "super" keyword. So parent class constructors are "not directly available" like parent class methods in child class so we can say constructors can not be inherited.

    0 讨论(0)
  • 2020-11-27 15:21

    Syntactic limitations can often be circumvented should there be any reason for a feature in a conceptual way. With this, I believe, the real reason for not supporting inheritance of constructor is not due to syntactic limitations but rather due to semantics.

    Conceptually inheritance provides a mechanism to acquire (or inherit) a behavior and most likely without writing any piece of code since its purpose is to provide code-reuse. For a child class, it makes no case to inherit the behavior of initialization of its parent class. After all, an inherited behavior finds its best use when an external caller can use it without knowing who (in the parent chain) actually has implemented it. As you can see, a caller hardly has any business knowing how a parent class is initialized via its child class, there is no discernible reason for supporting inheritance for a (parent class’s) constructor.

    0 讨论(0)
  • 2020-11-27 15:22

    No as stated by Mikhail; constructors are not inherited. You cannot inherit a constructor from superclass into your subclass. However when an object is instantiated with the "new" operator in java, that object inherit all constructors from it subclass to it superclass(parent) even including those in abstract class(since they are also super class).

    0 讨论(0)
  • 2020-11-27 15:24

    Reason mentioned in docs of Inheritance

    A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

    You can refer docs of Providing Constructors for Your Classes

    0 讨论(0)
提交回复
热议问题