Why constructors can not be inherited in java?

2020-01-24 20:04发布

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?

I have already read this link of C++

11条回答
走好不送
2楼-- · 2020-01-24 20:51

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.

查看更多
Melony?
3楼-- · 2020-01-24 20:53

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).

查看更多
贪生不怕死
4楼-- · 2020-01-24 21:01

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
查看更多
你好瞎i
5楼-- · 2020-01-24 21:03

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

查看更多
▲ chillily
6楼-- · 2020-01-24 21:03

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);
    }
}
查看更多
登录 后发表回答