satheesh
satheesh

Reputation: 1451

about same class names and interface names in a package

I am little bit confused of classes names.This is my problem ...i tried to give same class name in two different files and in the same package with default access modifier and even i tried with interfaces even then it is not showing any errors.i want to how they are actually accessed.

i dont understand how these classes are interface will be used...whether a class in a file checks for presence of class or interface (if it want to use ) first in local file and then check in outside with in the package or any thing else.i am not getting any clarity.If any one understand the trouble what i am facing,i hope will help me..... // This is InterfaceTest

package Practice;

interface t{
public void h();
public void h1();
}

abstract class InterfaceTest implements t{
public void h(){

}
public void h1(){

}
public abstract void t();
}

//This is other file InterfaceTest1

package Practice;
interface t{
public void h();
public void h2();
}
public class InterfaceTest1 {
}

//This is TestStack file

package Practice;

public class TestStack {
Test t=new Test();
public static void main(String[] args){
    TestStack t1=new TestStack();
    InterfaceTest it=new InterfaceTest();
}
}
interface t{
public void h3();
}
class Test implements t{
public void h3(){

}

public void h1(){

}
public void h2(){

}
}
class InterfaceTest{

}

These three files used in the same package but i am not getting any errors in name collision

Upvotes: 1

Views: 7211

Answers (2)

Chris Thompson
Chris Thompson

Reputation: 35598

It isn't very clear what you are saying but I'll take a stab at it - leave a comment if I'm not understanding you.

In Java, a class is identified by its fully qualified name. The fully qualified name is .classname.

For example, if a class is in the com.foo.bar package and is named MyClass, the fully qualified name would be com.foo.bar.MyClass. If you have more than one class with the same fully qualified name, you will have a collision and the JVM won't know which class to use. In order to use a class in a different package, you have to import it. You would import the above class with a statement at the top of your java file lie import com.foo.bar.MyClass or, if you wanted to import the entire package, you would use import com.foo.bar.* although that is considered bad practice. Interfaces behave in the same manner. Classes in the same package as a given class do not need to be imported. So, another class in the com.foo.bar package that wishes to use MyClass, would not need to import it.

Does that help you at all? If you can clarify your question, I can try to help you more.

Edit To address your clarification, you can only have one top level, public class per java file. If you wish to define additional public interfaces or classes in a file, they must be nested inside the top-level class. If you use a class and don't fully qualify it, the compiler will first look for a nested class with that name and then look for a class in the same package with that name. If it still can't find it, and you haven't imported it, then it will fail with a class resolution error.

Edit 2 Ah I think I understand. Are you attempting to use those classes in a different class? The compiler won't complain until it attempts to resolve the class that has a name collision. If that class isn't referenced anywhere, the compiler won't care. If you have two MyClass classes, but neither is used anywhere, then the compiler won't bother trying to resolve the class and won't notice the collision. Yes, inside of MyClass if you attempt to reference MyClass it's going to assume that you are referring to the class you are in.

Edit 3 One last try, if you have MyClass and then have another class nested inside it, MyClass1, the fully qualified name for MyClass1 is com.foo.bar.MyClass$MyClass1 because it is nested as part of MyClass

Upvotes: 4

duffymo
duffymo

Reputation: 308733

Interface and class names within a package have to be unique:

package foo;

public interface Bar
{
    void print();
}

class Bat implements Bar
{
    public void print() { System.out.println("Hi there"); }
}

You can have duplicate interface or class names if the packages are different. Fully-resolved class names must be unique.

package other;

public class Bat
{
    public void doSomething() 
    { 
        System.out.println("And now for something completely different");
    }
}

UPDATE:

The example code you present is preposterous. Bad naming conventions aside, you have interface t defined in two separate files. What made you think that you needed to copy and paste it into the second file once you had the first one? Remove interface t from the file containing the definition for InterfaceTest1.

All these are in the same package Practice. What makes you continue to define interface t again and again and again? You also have it in that TestStack definition. Please, think of some unique names if the definitions are indeed unique and your problem goes away.

Upvotes: 1

Related Questions