Class name clashes in Java:
Since import-on-demand seems to be both ef?cient and convenient, you may be wondering why there is a need for the alternative of importing speci?c named classes.
First, this can be useful as documentation when you want only one or two classes from a particular package - it makes it easier for someone reading the program to see where these classes come from.
More importantly, it can be helpful in case of a class name clash - two classes with the same name in different packages. For example, both the java.util package and the java.sql package have a class called Date. Suppose we have a program that uses the java.sql Date class and some other library classes from each package. What will happen if we write the followingfi
import java.util.*;
import java.sql.*;
...
Date d1 = new Date(DATE_IN); //Date is ambiguous
We get an ambiguous name, which causes a compilation error - the system cannot decide which Date class you want. You can get round this by always using the fully quali?ed name java.sql.Date, but this can be tedious.
For example, if you want only the ArrayList class from java.util but you want several classes from java.sql, you could de?ne your imports as follows to avoid a clash:
import java.util.ArrayList;
import java.sql.*;
Perhaps more surprisingly, you can also write:
import java.util.*; import java.sql.*; import java.sql.Date;
This means that the java.sql de?nition of Date is used instead of the
java.util de?nition. The speci?c import of the Date class hides (takes precedence over) any import-on-demand de?nition of the same name. This approach has the advantage that you can use any other classes from these two packages (as long as they do not have clashing names).
If you de?ne and use your own class called Date, this will hide any Date class in packages you import using the import-on-demand facility.
If you need to use two classes from different packages with the same simple name, you will have to use the fully quali?ed class names throughout your program, as follows:
import java.util.*;
import java.sql.*;
...
java.sql.Date d1 = new java.sql.Date(DATE_IN); java.util.Date d2 = new java.util.Date(); System.out.println("SQL date: " + d1.toString()); System.out.println("util date: " + d2.toString());
This resolution or avoidance of name clashes is an important reason for the use of packages. It is very useful for teams of programmers collaborating on development of large systems - different programmers can develop separate packages and may use some identical names for classes or other items speci?c to a package, without it causing a name clash.
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your Class name clashes in Java homework and assignments? Live Class name clashes in Java experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Class name clashes in Java homework help, java assignment help and Class name clashes in Java projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours