简体   繁体   English

为什么不能从构造函数是私有的类继承?

[英]Why can you not inherit from a class whose constructor is private?

为什么 Java 不允许从构造函数是私有的类继承?

Java doesn't prevent sub-classing of class with private constructors. Java 不会阻止使用私有构造函数对类进行子类化。

public class Main {
    static class A {
        private A() {
            System.out.println("Subclassed A in "+getClass().getName());
        }
    }

    static class B extends A {
        public B() {

        }
    }

    public static void main(String... ignored) {
        new B();
    }
}

prints印刷

Subclassed A in Main$B

What it prevents is sub-classes which cannot access any constructors of its super class.它阻止的是无法访问其超类的任何构造函数的子类。 This means a private constructor cannot be used in another class file, and a package local constructor cannot be used in another package.这意味着私有构造函数不能在另一个类文件中使用,包本地构造函数不能在另一个包中使用。

In this situation, the only option you have is delegation.在这种情况下,您唯一的选择就是委派。 You need to call a factory method to create an instance of the "super" class and wrap it.您需要调用工厂方法来创建“超级”类的实例并对其进行包装。

Because a class must call its super class constructor always .因为一个类必须总是调用它的超类构造函数。 If the super class constructor can't be accessed, then the sub class can't be initialized.如果无法访问超类构造函数,则无法初始化子类。

More info: JLS 8.8.10.更多信息: JLS 8.8.10。 Preventing Instantiation of a Class 防止类的实例化


Regarding Brian Roach's comments:关于 Brian Roach 的评论:

The call [to the parent class constructor] is only implicit if you don't do it explicitly and the parent has a public or protected no-arg constructor (or hasn't defined any in which case there's a default no-arg). [对父类构造函数] 的调用仅在您不显式执行并且父类具有公共或受保护的无参数构造函数(或未定义任何在这种情况下存在默认无参数的情况下)时才是隐式的。 It's required because ... that's how the language works.它是必需的,因为......这就是语言的工作方式。 Children [classes] must call [their] parent's constructor.孩子 [classes] 必须调用 [他们] 父母的构造函数。

Note that when you instantiate any class in Java, there's always a implicit call to Object constructor since it is the super class of all classes.请注意,当您在 Java 中实例化任何类时,总会隐式调用Object构造函数,因为它是所有类的超类。 It will execute its default constructor :它将执行其默认构造函数

public Object() {
}

Note from the JLS link:来自 JLS 链接的注意事项:

It is a compile-time error if a default constructor is implicitly declared but the superclass does not have an accessible constructor (§6.6) that takes no arguments and has no throws clause.如果隐式声明了默认构造函数,但超类没有可访问的构造函数(第 6.6 节),该构造函数不接受任何参数且没有 throws 子句,则会出现编译时错误。

If constructor of a class is private then child class cannot make call to super constructor.如果类的构造函数是私有的,则子类不能调用超级构造函数。 Hence inheritance would fail.因此继承将失败。

If you have a subclass, you have 2 possiblities for child class(subclass) constructors : 1. Default Constructor(No argument constructor) : In this case default constructor will automatically try to call the parent class constructor : this will fail since parent class constructor is private.如果您有子类,则子类(子类)构造函数有 2 种可能性: 1. 默认构造函数(无参数构造函数):在这种情况下,默认构造函数将自动尝试调用父类构造函数:由于父类构造函数,这将失败是私人的。 2. Parameterized Constructor : When you try to create an object for a child class which has parameterized constructor, you need to mandatorily call parent class constructor from child class constructor by either passing parameters or not passing parameters : this will also fail since parent constructor is private. 2. 参数化构造函数:当您尝试为具有参数化构造函数的子类创建对象时,您需要通过传递参数或不传递参数来强制从子类构造函数调用父类构造函数:这也会失败,因为父构造函数是私人的。

Since child class will have either default constructor or parameterized constructor and its not possible to have either of them, you cannot have a subclass for a parent class with private constructor.由于子类将具有默认构造函数或参数化构造函数,并且不可能拥有它们中的任何一个,因此您不能拥有具有私有构造函数的父类的子类。

是的,在 Luiggi 的回答中添加了一些东西,在创建单例类时使用了 Java 的这个特性,它只允许创建该类的一个实例。

This is because, When we do inheritance the job of the compiler is to make a direct or indirect relation of all the classes with the Object class by writing the super() at the very first statement of every class constructor .这是因为,当我们进行继承时,编译器的工作是通过在每个类构造函数的第一条语句中编写 super() 来使所有类与 Object 类建立直接或间接的关系。 when we make the constructor as private that means it shouldn't be accessed from outside the class but when we do inheritance compiler will implicitly write this type of statement.当我们将构造函数设为私有时,这意味着不应从类外部访问它,但是当我们进行继承时,编译器将隐式编写这种类型的语句。

class SubClassName extends SuperClassName {
  public SubClassName() {
    super();  // which will indirectly going to call the Parent class constructor from outside its scope
  }
}

暂无
暂无

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

相关问题 您可以从另一个类调用私有构造函数吗? - Can you call a private constructor from another class? Java-您可以从超类继承并拥有一个静态构造函数来引用基类而不是父类吗? - Java - Can you inherit from a superclass and have a static constructor that references the base classes as opposed to the parent class? 有没有一种方法可以注入其构造函数在micronaut中具有参数的类? - Is there a way you can inject a class whose constructor has parameters in micronaut? 为什么私有构造函数的私有内部类可以被分类? - Why can a private inner class with a private constructor be sub-classed? 为什么我能够在子类中继承和调用私有构造函数? - Why am I able to inherit & call a private constructor in a subclass? 为什么如果我将其默认构造函数声明为private,则无法将其子类化 - why a class can not be subclassed if i declare its default constructor as private 在java中,如何使用私有构造函数创建一个类,其超类也有一个私有构造函数? - In java, how do I make a class with a private constructor whose superclass also has a private constructor? 为什么我不能从特定 class 继承特定变量? - Why can't I inherit a specific variable from a specific class? 为什么我可以从内部类继承final方法? - Why can I inherit final method from inner class? 为什么您可以访问和操纵班级的私人成员 - Why can you access and manipulate private Members of your own class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM