More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know.
Q2. Why do we need to specify import statement ? Don't you think Java could have been designed in such a way to automatically import everything that's in the class path ?
Ans. Java could design it in such a manner but would result in ambiguity if there are multiple files with the same name. The only way to get over it to show the message when you use any such class to provide explicit import with the package prefix. The other problem could be that Java might have to change the early import to Late import and check what's being used to decide what needs to be imported as otherwise you will see errors regarding the ambiguous imports with duplicate file name.
Help us improve. Please let us know the company, where you were asked this question :
Ans. It makes application little heavy as we are importing classes that aren't required.
It creates class name conflicts as similar name classes might be available across different packages. In case of such conflicts, we will have to specify the package name with the class name at the time of it's usage.
Help us improve. Please let us know the company, where you were asked this question :
Ans. It means import all the classes and interfaces within java.util package and make them available to use within the current class or interface. This is shorthand wild card annotation for importing all classes within a particular package. This won't import the classes within the sub packages of java.util.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Abstract classes provide a mechanism of interfacing ( using abstract method ) as well as code reuse through inheritance ( extending abstract class )
Comparing to concrete class they have an advantage of providing interface which a concrete class doesn't provide.
Comparing to interfaces they have an advantage of providing code reuse through inheritance which interfaces dont provide.
Ans. One can import the same package or same class multiple times. Neither compiler nor JVM complains wil complain about it. And the JVM will internally load the class only once no matter how many times you import the same class.
Help us improve. Please let us know the company, where you were asked this question :