Core java interview Questions You'll Most Likely Be Asked (Second Edition)


Download 1.52 Mb.
Pdf ko'rish
bet46/171
Sana07.01.2023
Hajmi1.52 Mb.
#1081886
1   ...   42   43   44   45   46   47   48   49   ...   171
Bog'liq
Core JAVA Interview Questions Youll M...

Explain the import keyword.
Answer:
The import keyword is used to include other packages in your
code. You can import the standard Java packages like java.util,
etc. or you can import user defined packages.
The import keyword needs to be followed by the package
name. In order to import a specific class from a package, you
need to specify the class name as well. In order to import the
entire package, you need to use a wildcard character (*) after the
package name. The following are some examples of import:
import.java.util.List; //imports the List interface
import java.io.*; //imports all classes from the java.io
package
Explain the differences between the protected and default
access specifier
Answer:
There are several differences between the protected and default
access specifier. These are as follows:
The protected access specifier is specified via the protected
keyword while the default access occurs when no other
access specifier is used. So, the default access specifier does
not have a separate keyword.
When a member has the protected access specifier, that
member is accessible to all classes within the package as well


118.
119.
as sub–classes that are outside the package too. When a
member has the default access specifier, that member is
accessible only to classes within the package, it is not
accessible outside the package.
Is the following code snippet valid? Explain.
package demo1;
public class MyClass1 {
int a; //Line 1
}
package demo2;
public class MyClass2 {
public void doSomething() {
MyClass1 obj = new MyClass1(); //Line 2
obj.a = 5; //Line 3
}
}
Answer:
The above code is not valid and will cause a compilation error at
line 3. The class MyClass1 is in a package called demo1. It has
a field a. Since no access specifier is included explicitly, this field
has default access. The class MyClass2 is in a package called
demo2. The doSomething() method access the field a from
MyClass1 at Line 3. A field with default access is not accessible
outside the package so Line 3 will cause a compilation error.

Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   42   43   44   45   46   47   48   49   ...   171




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling