Core Java Interview Question Page 27
Modifiers
Question: What are access modifiers
Answer:
These public, protected and private, these can be applied to class, variables, constructors and methods. But if you don't specify an access modifier then it is considered as Friendly
Question: Can protected or friendly features be accessed from different packages
Answer:
No when features are friendly or protected they can be accessed from all the classes in that package but not from classes in another package
Question: How can you access protected features from another package
Answer:
You can access protected features from other classes by subclassing the that class in another package, but this cannot be done for friendly features
Question: What are the rules for overriding
Answer:
Private method can be overridden by private, friendly, protected or public methods
Friendly method can be overridden by friendly, protected or public methods
Protected method can be overridden by protected or public methods
Public method can be overridden by public method
Question: Explain modifier final
Answer:
Final can be applied to classes, methods and variables and the features cannot be changed. Final class cannot be subclassed, methods cannot be overridden
Question: Can you change the reference of the final object
Answer:
No the reference cannot be change, but the data in that object can be changed
Question: Can abstract modifier be applied to a variable
Answer:
No it is applied only to class and methods
Question: Can abstract class be instantiated
Answer: No abstract class cannot be instantiated i.e you cannot create a new object of this class
Question: When does the compiler insist that the class must be abstract
Answer:
If one or more methods of the class are abstract.
If class inherits one or more abstract methods from the parent abstract class and no implementation is provided for that method
If class implements an interface and provides no implementation for those methods
Question: How is abstract class different from final class
Answer:
Abstract class must be subclassed and final class cannot be subclassed
Question: Where can static modifiers be used
Answer:
They can be applied to variables, methods and even a block of code, static methods and variables are not associated with any instance of class
Question: When are the static variables loaded into the memory
Answer:
During the class load time
Question: When are the non static variables loaded into the memory
Answer:
They are loaded just before the constructor is called
Question: How can you reference static variables
Answer: Via reference to any instance of the class
Computer comp = new Computer ();
comp.harddisk where hardisk is a static variable
comp.compute() where compute is a method
Via the class name
Computer.harddisk
Computer.compute()
Question: Can static method use non static features of there class
Answer:
No they are not allowed to use non static features of the class, they can only call static methods and can use static data
Question: What is static initializer code
Answer:
A class can have a block of initializer code that is simply surrounded by curly braces and labeled as static e.g.
public class Demo{
static int =10;
static{
System.out.println(?Hello world');
}
}
And this code is executed exactly once at the time of class load
Where is native modifier used It can refer only to methods and it indicates that the body of the method is to be found else where and it is usually written in non java language
Question: What are transient variables
Answer:
A transient variable is not stored as part of objects persistent state and they cannot be final or static
Question: What is synchronized modifier used for
Answer:
It is used to control access of critical code in multithreaded programs
Question: What are volatile variables
Answer: It indicates that these variables can be modified asynchronously