为什么Java构造函数必须是公共的或受保护的,才能将类扩展到其包之​​外? [英] Why does a Java constructor have to be public or protected for a class to be extended outside its package?

查看:285
本文介绍了为什么Java构造函数必须是公共的或受保护的,才能将类扩展到其包之​​外?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的 ProtectedConstructor.java 源代码:

  package protectCon; 

public class ProtectedConstructor {
public int nothing;
ProtectedConstructor(){
nothing = 0;
}
}

以下是 UsingProtectedCon .java 来源:

  package other; 

import protectcon.ProtectedConstructor;

public class UsingProtectedCon extends ProtectedConstructor {// ** Line 4 **
public static void main(String ... a){
}
}

当我编译 UsingProtectedCon.java 时,我收到错误在上面显示的第4行。它说ProtectedConstructor()不公开;所以不能在包外访问。



但是,由于我的课程是公开的,所以我不应该将它扩展到包外。我无论如何都没有创建它的任何实例。



现在,如果我将 ProtectedConstructor 类的构造函数设为 public protected 然后代码编译正常,没有错误。



<那么为什么即使构造函数必须是 public protected ,也不仅仅是默认访问权限?

解决方案

如果你想扩展一个类在它的包之外,它必须有一个 public的构造函数 protected 因为在Java中,每个构造函数都必须从其超类中调用构造函数。



因此,每个没有<$ c $的构造函数都会隐含 super()调用c> this()或显式调用 super()作为其第一个语句。如果你没有指定一个构造函数,Java将添加一个默认的无参数构造函数,所以实际上你的代码如下所示:

  public class UsingProtectedCon extends ProtectedConstructor {
public UsingProtectedCon(){
super();
}

public static void main(String ... a){
}
}

所以换句话说你的代码无法编译,因为默认构造函数中对 super()的调用不能解决。


The following is my ProtectedConstructor.java source code:

package protectCon;

public class ProtectedConstructor{
    public int nothing;
    ProtectedConstructor(){
        nothing = 0;
    }
}

And following is the UsingProtectedCon.java source:

package other;

import protectcon.ProtectedConstructor;

public class UsingProtectedCon extends ProtectedConstructor{   //**Line 4**
    public static void main(String... a) {  
    }
}

When I compile UsingProtectedCon.java, I get error at Line 4 shown above. It says that ProtectedConstructor() is not public ; so cannot be accessed outside package.

However, since my class is public, shouldn't I be able to extend it outside package. I am anyway not creating any instance of it.

Now, if I make the constructor of ProtectedConstructor class as public or protected then the code compiles fine with no error.

So then why is it necessary even for the constructor to be public or protected, and not just have default access?

解决方案

If you want to extends a class outside its package it must have a constructor that is public or protected because in Java every constructor must call a constructor from its superclass.

Because of this there is an implied super() call in every constructor which does not have this() or an explicit call to super() as its first statement. And if you don't specify a constructor at all Java will add a default parameterless constructor, so in effect your code looks like this:

public class UsingProtectedCon extends ProtectedConstructor {
    public UsingProtectedCon() {
        super();
    }

    public static void main(String... a) {   
    }
}

So in other words your code is failing to compile because the call to super() in the default constructor cannot be resolved.

这篇关于为什么Java构造函数必须是公共的或受保护的,才能将类扩展到其包之​​外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆