Why are constructors not inherited in java?

后端 未结 12 1174
被撕碎了的回忆
被撕碎了的回忆 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:00

    Only fields, methods, and nested classes are the membe of any class not Constructors. A subclass inherits all the members like (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.

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

    In simple words, a constructor cannot be inherited, since in subclasses it has a different name (the name of the subclass).

    class A {
       A();
    }
    
    class B extends A{
       B();
    }
    

    You can do only:

    B b = new B();  // and not new A()
    

    Methods, instead, are inherited with "the same name" and can be used.

    As for the reason: It would not have much sense to inherit a constructor, since constructor of class A means creating an object of type A, and constructor of class B means creating an object of class B.

    You can still use constructors from A inside B's implementation though:

    class B extends A{
       B() { super(); }
    }
    
    0 讨论(0)
  • 2020-11-27 15:06

    you can't inherited constructors but you can inherit initialized value in constructor like

    class test1 {
        int a,b;
        test1(){
            a = 100;
            b = 20;
        }
    }
    
    class test2 extends test1 {
        test2(){
            // do something
        }
    }
    
    class test {
        public static void main(String[] args) {
            test2 t = new test2();
            int a = t.a;
            int b = t.b;
            System.out.println(a+b);
        }
    }
    
    0 讨论(0)
  • 2020-11-27 15:10
    • A constructor may only be called with new. It cannot be called as a method.
    • The constructor name is identical to the class name.

    So inheritance is practically not possible as such. However in a construct one might call other constructors.

    • In the same class using this(...);
    • Of the extended class using super(...);

    Example

    class A {
        A() { }          // Constructor
        A(int a) { }     // Constructor
        A(boolean c) { } // Constructor
    }
    class B extends A {
        B() {
            this(3, 7);
        }
        B(int a) {
            super();
        }
        B(String b) {
            super(7);
        }
        B(int a, int c) { // Calls super() implicitly
        }
    }
    A a = new B(8):
    

    There unfortunately is no possibility to use A's constructor for a boolean:

    B b = new B(true): // ERROR
    

    The language designes could have implemented such a thing as:

    Generate for every public constructor in the base class, a constructor with the same signature if such a constructor is not defined already. Call super with the same parameters. Call this() if there is a default constructor.

    That seems a bit bloating the code. And is not simply a pointer in a virtual method table, by which method inheritance/overriding works.

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

    No, constructors will not be inherited to subclass, eventhough its a non-static member it will not be inherited to subclass because constructors will not be loaded inside the object, it is used to create the object. Constructors are like a non-static initializer

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

    Constructors are not members of classes and only members are inherited. You cannot inherit a constructor. That is, you cannot create a instance of a subclass using a constructor of one of it's superclasses.

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