简体   繁体   English

Java中的继承(默认构造函数)

[英]Inheritance in Java (default constructor)

Consider there are three classes A, B and C. Class A doesn't have a constructor, ClassB has a constructor and Class C has a parameterized constructor. 考虑三个类A,B和C。类A没有构造函数,类B有构造函数,类C有参数化构造函数。 something like the example given below. 类似于下面的示例。


public class ClassA {

}

public class ClassB extends ClassA {

    public ClassB() {
        System.out.println("Default cons Class B");
    }

}

public class ClassC extends ClassB {

    public ClassC(int a, int b) {
        System.out.println("This is class C "+a+ "and"+ b );

    }
    public static void main(String args[]) {
        ClassC c = new ClassC(2,3);
    }
}

Output: 输出:

Default cons Class C 默认缺点C类

This is class C 2and3 这是C 2and3类

Question 1: 问题1:

To construct object C it constructs B and to construct B it constructs A first. 为了构造对象C,它构造了B,并且为了构造B,它首先构造了A。 Even though there isn't any default constructor in class A defined, the program works fine by construction its own default constructor and the B class calls super(). 即使在类A中没有定义任何默认构造函数,该程序也可以通过构造自己的默认构造函数来正常工作,而B类则调用super()。 I don't have an issue here however when I change the Class B something like this 我在这里没有问题,但是当我更改B类这样的东西时

public class ClassB extends ClassA {

    public ClassB(int a, int b) {
        System.out.println("This is class C "+a+ "and"+ b );
    }   
}

I am getting an error 我收到一个错误

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Implicit super constructor ClassB() is undefined. Must explicitly invoke another constructor

    at ClassC.<init>(ClassC.java:4)
    at ClassC.main(ClassC.java:10)

Why do we have to implicitly specify the super constructor ClassB(), It worked fine for the first example even though there isn't any super constructor in ClassA(). 为什么我们必须隐式指定超级构造函数ClassB(),即使在ClassA()中没有任何超级构造函数,第一个示例也可以正常工作。 I am wondering if by default the C's constructor by default calls B's unspecified constructor just like it did for Class A. 我想知道默认情况下C的构造函数是否默认像B类一样调用B的未指定构造函数。

If you specify at least one constructor in a class a default constructor is no longer created for you. 如果在一个类中至少指定一个构造函数,则不再为您创建默认构造函数。

If you do not specify which parent constructor to call in your subclass the one that takes 0 parameters is used by default. 如果未指定要在子类中调用哪个父构造函数,则默认情况下使用带有0参数的子构造函数。

Since you added a constructor to class B that takes two integer parameters you now have to call it like this from in C as there is no longer a constructor that takes no parameters in class B: 由于您向类B添加了一个构造函数并使用了两个整数参数,因此您现在必须在C中像这样调用它,因为在类B中不再有不带参数的构造函数:

public ClassC(int a, int b) {
    super(a, b);
    System.out.println("This is class C "+a+ "and"+ b );
}

Because to create an instance of C, the JVM calls the constructors of the class hierarchy. 因为要创建C的实例,所以JVM会调用类层次结构的构造函数。

So when you create C , you actually first call the constructor of A , then of B , then of C . 因此,当您创建C ,实际上首先要调用A的构造函数,然后是B ,然后是C Implicitly, super() is called if nothing else is specified. 隐式地,如果未指定其他任何内容,则将调用super()

If you create a constructor that takes parameters explicitly, then that overrides the default constructor 如果您创建一个显式接受参数的构造函数,则它将覆盖默认构造函数

public B() {
    //default constructor
}

Which means Java won't be able to invoke it for you implicitly as it does not exist, so you have to call it yourself with 这意味着Java将无法为您隐式调用它,因为Java不存在,因此您必须自己使用

public C(int a, int b) {
    super(a, b);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM