简体   繁体   中英

java define class with name Object

I created a class with name Object in a package java.lang . In some another package I created Main class. Now in main method of Main class when I write new Main() and put a dot after that I don't see equals() , wait() etc.. ie methods of actual Object class.

My question is don't you think creating an class with name Object(specially with java.lang package) should not be allowed. As Object class is by default inherited by all classes so in my case whatever class I define will automatically inherit my class instead of actual object class because class name and package name of my object class is same as that of actual object class.

This is quite an open-ended question.

Should it be allowed? Why not, if you know what you're doing, go crazy.

I agree it's somewhat dangerous theoretically, but I don't see how someone would do this by mistake. If someone actually wants to redefine the whole Object class for some reason, academical for instance, then he/she can do so at his/her own risk.

No language can fully prevent you from putting an arrow to your own knee.

I can confirm that even if you write one it wont be useful (at least With usual java classloading) The ones in java.lang take the precedence as they are already loaded by system class loader and your Object class will never be picked and calling any custom methods will only result in runtime errors(java.lang.NoSuchMethodError)

But I am not certain about the behavior when you use a custom class loader and override its loadClass method such a way that it explicitly loads your class byte but does not do a lookup in parent classloader. May be home work :)

I use this trick (having some third party same package and class name in our classpath) often to override the functionality of the third party libraries if I cannot wrap them up.
In any case, All the system classes has to be loaded into jvm before the client program (our application) to load. It is all to deal with the ClassLoader , compiler allowing it because it really don't care too much about all the classes, but while runtime, JVM classloaders will consider these things, If you look at the source code of the default ClassLoader (oracle jdk), You can see there will be a check that you should not have any package start with java.

private ProtectionDomain preDefineClass(String name,
                        ProtectionDomain protectionDomain)
    {
    ...
    if ((name != null) && name.startsWith("java.")) {
        throw new SecurityException("Prohibited package name: " +
                    name.substring(0, name.lastIndexOf('.')));
    }
...
}

Even with famous bite code instrumentation tools like javaassist will restrict this kind of behavior, they will have a check whether a class can be instrumented or not Instrumentation.isModifiableClass .
Finally, it all depends upon the ClassLoader implementation and as it has to stick with the specifications, You need to consider the order of classes from class path to load.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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