开发者

subclass, parent class....variable conflict?

what will happen if my subclass and the parent class use the s开发者_JAVA技巧ame variable name?

the parent class is sorta like this:

public class things{
    private things[] stuff;
}

and the subclass is something like this:

import java.utils.LinkedList;
public class listOfThings extends LinkedList<things>{
    private LinkedList<things> stuff;
}

is this allowed? or will the linked list override the array?


In this case, both variables are private. This means they only exist with the scope of the defining class and there is no overlap. If the parent class declared the variable as protected or public, you would get a compiler error about the variable already existing.

Edit:
KG answered the question for the example you gave. I gave an answer for if you actually were doing what you were asking about.


You're not extending things with this example, you are extending

LinkedList<things>

This is fundamentally different than extending things.

Therefore, as is, your code should compile.


Variables with same names are ok. private, public, static, instance, method local, block local, whatever. Java chooses the closest one. It is much much looser than methods.

JLS3#8.3: (there's only 1 restriction which is stated in the 1st sentence)

It is a compile-time error for the body of a class declaration to declare two fields with the same name. Methods, types, and fields may have the same name, since they are used in different contexts and are disambiguated by different lookup procedures (§6.5).

If the class declares a field with a certain name, then the declaration of that field is said to hide any and all accessible declarations of fields with the same name in superclasses, and superinterfaces of the class. The field declaration also shadows (§6.3.1) declarations of any accessible fields in enclosing classes or interfaces, and any local variables, formal method parameters, and exception handler parameters with the same name in any enclosing blocks.

If a field declaration hides the declaration of another field, the two fields need not have the same type.

A class inherits from its direct superclass and direct superinterfaces all the non-private fields of the superclass and superinterfaces that are both accessible to code in the class and not hidden by a declaration in the class.

Note that a private field of a superclass might be accessible to a subclass (for example, if both classes are members of the same class). Nevertheless, a private field is never inherited by a subclass.

It is possible for a class to inherit more than one field with the same name (§8.3.3.3). Such a situation does not in itself cause a compile-time error. However, any attempt within the body of the class to refer to any such field by its simple name will result in a compile-time error, because such a reference is ambiguous.

// this is a valid program!
class $
{
    $ $;
    $ $($ $)
    {
        class $$ extends $
        {
            $$ $;
        }
        return new $$();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜