Naming Conflicts Flashcards

1
Q

When does a naming conflict happen ?

A

When the class is found in multiple packages, Java gives you the compiler error:
The type Date is ambiguous

import java.util.Date;
import java.sql.*;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Give a key difference between importing a class by its full qualified name and importing a class using a wildcard !

A

If you explicitly import a class name, it takes precedence over any wildcards present.

If you have both an explicit import and a wildcard import from the same package, the explicit import will take precedence. This means that the specific class referenced will be used, and any potential conflicts with the wildcard import are resolved in favor of the explicitly imported class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to use two classes with the same name but different packages in the same code ?

A

When this happens,
you can pick one to use in the import and use the other’s fully qualifi ed class name
(the package name, a dot, and the class name) to specify that it’s special.
For example:

import java.util.Date;
public class Conflicts {
Date date;
java.sql.Date sqlDate;
}

Or you could have neither with an import and always use the fully qualified class name:

public class Conflicts {
java.util.Date date;
java.sql.Date sqlDate;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly