简体   繁体   English

为什么如果我将其默认构造函数声明为private,则无法将其子类化

[英]why a class can not be subclassed if i declare its default constructor as private

In a class i can have as many constructors as I want with different argument types. 在一个类中,我可以使用任意数量的构造函数来使用不同的参数类型。 I made all the constructors as private,it didn't give any error because my implicit default constructor was public But when i declared my implicit default constructor as private then its showing an error while extending the class. 我将所有构造函数都设为私有,但未给出任何错误,因为我的隐式默认构造函数是public,但是当我将隐式默认构造函数声明为private时,在扩展类时显示错误。 WHY? 为什么?

this works fine 这很好

public class Demo4  {
    private String name;
    private int age;
    private double sal;

    private Demo4(String name, int age) {
        this.name=name;
        this.age=age;   
    }

    Demo4(String name) {
        this.name=name;
    }

    Demo4() {
        this("unknown", 20);
        this.sal=2000;
    }

    void show(){
        System.out.println("name"+name);
        System.out.println("age: "+age);
    }
}

This can not be inherited 这不能被继承

public class Demo4  {
    private String name;
    private int age;
    private double sal;

    private Demo4(String name, int age) {
        this.name=name;
        this.age=age;
    }

    Demo4(String name) {
        this.name=name;
    }

    private Demo4() {
        this("unknown", 20);
        this.sal=2000;
    }

    void show() {
        System.out.println("name"+name);
        System.out.println("age: "+age);
    }
}

If ANY of the constructors in the superclass are accessible, you can subclass it, just call the accessible super constructor with super(..) in your subclass's constructor. 如果超类中的任何构造函数都是可访问的,则可以对其进行子类化,只需在子类的构造函数中使用super(..)调用可访问的超级构造函数。

I would be able to subclass your second example like this: 我可以像这样子化您的第二个示例:

     super("A string");

why a class can not be subclassed if i declare its default constructor as private 为什么如果我将其默认构造函数声明为private,则无法将其子类化

The constructor in the subclass must call a super constructor (which could be implicit or explicit), in order to fully construct the object. 子类中的构造函数必须调用一个超级构造函数(可以是隐式的也可以是显式的),以便完全构造该对象。 The super constructor call chain goes all the way up till the Object class, the super class of all classes in Java. 超级构造函数调用链一直持续到Object类,即Java中所有类的超类。

If any of the super constructor is not visible to the subclass then there is no way to fully construct then object. 如果子类不可见任何超级构造函数,则无法完全构造对象。

One way to get around this is to make the constructor in the super class protected . 解决此问题的一种方法是使超类中的构造函数protected That way the super constructors are only visible to the subclasses. 这样,超级构造函数仅对子类可见。

A little bit of extra information, for default constructors there is always an implicit call to super() . 一些额外的信息,对于默认的构造函数,总会隐式调用super() Conversely for a non default constructor there should be an explicit call (if there isn't an accessible constructor in the parent class) to a constructor of the parent class that is accessible, or to constructor of the same class. 相反,对于非默认构造函数,应该显式调用(如果父类中没有可访问的构造函数)可访问的父类的构造函数或同一类的构造函数。 For example 例如

package com.test;

class A {

    private A() {
      //implicit call to java.lang.Object.super()
    }

    public A(String a) {
      //implicit call to java.lang.Object.super()
    }
}

public class B extends A {

    public B(String a, String b) {
        this(a);
    }

    public B(String a) {
        super(a);
    }

}

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

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