Q13:What are different types of access modifiers?
Ans:There are following four types of access modifiers
public: Any thing declared as public can be accessed from anywhere.
private: Any thing declared as private can’t be seen outside of its class.
protected: Any thing declared as protected can be accessed by classes in the same package and subclasses in the other packages.
default modifier : Can be accessed only to classes in the same package.
Ans: When we have to want child class to implement the behavior of the method.
Q15: Can We call a abstract method from a non abstract method ?
Ans : Yes, We can call a abstract method from a Non abstract method in a Java abstract class
Q16: What is the difference between an Abstract class and Interface ? And can you explain when you are using an Abstract classes ?
Ans: Abstract classes let you define some behaviors; they force your subclasses to provide others. These abstract classes will provide the basic functionality of your application, child class which inherited this class will provide the functionality of the abstract methods in abstract class. When base class calls this method, Java calls the method defined by the child class. An Interface can only declare constants and instance methods, but cannot implement default behavior.
Interfaces provide a form of multiple inheritance. A class can extend only one other class.
Interfaces are limited to public methods and constants with no implementation. Abstract classes can have a partial implementation, protected parts, static methods, etc.
A Class may implement several interfaces. But in case of abstract class, a class may extend only one abstract class.Interfaces are slow as it requires extra indirection to find corresponding method in the actual class. Abstract classes are fast.
Q17: What is user-defined exception in java ?
Ans: User-defined expectations are the exceptions defined by the application developer which are errors related to specific application. Application Developer can define the user defined exception by inherited the Exception class as shown below. Using this class we can throw new exceptions for this we have use throw keyword .
Example of user define exception Java Example :
1.Create an class which extends Exception:-
public class greaterVlaueException extends Exception {
}
2.Throw an exception using a throw statement:
public class Fund {
...
public Object getFunds() throws greaterVlaueException {
if (id>2000) throw new greaterVlaueException();
...
}
}
User-defined exceptions should usually be checked.
Q18 : What is the difference between checked and Unchecked Exceptions in Java ?
Ans: All predefined exceptions in Java are either a checked exception or an unchecked exception. Checked exceptions must be caught using try .. catch() block or we should throw the exception using throws clause. If you don't, compilation of program will fail. All exceptions in RuntimeExcetption and Error class are unchecked exception.
Q19: Explain garbage collection ?
Ans: Garbage collection is an important part of Java's security strategy. Garbage collection is also called automatic memory management as JVM automatically removes the unused variables/objects from the memory. The name "garbage collection" implies that objects that are no longer needed by the program are "garbage" and this object will destroy by garbage collector. A more accurate and up-to-date metaphor might be "memory recycling." When an object is no longer referenced by the program, the heap space it occupies must be recycled so that the space is available for subsequent new objects. The garbage collector must somehow determine which objects are no longer referenced by the program and make available the heap space occupied by such unreferenced objects. In the process of freeing unreferenced objects, the garbage collector must run any finalizers of objects being freed.
Q20 : How you can force the garbage collection ?
Ans: Garbage collection automatic process and can't be forced. We can call garbage collector in Java by calling System.gc() and Runtime.gc(), JVM tries to recycle the unused objects, but there is no guarantee when all the objects will garbage collected.
Q21 : What are the static fields & static Methods ?
Ans: If a field or method defined as a static, there is only one copy for entire class, rather than one copy for each instance of class. static method cannot access non-static field or call non-static method
Q22:What are the Final fields & Final Methods ?
Ans: Fields and methods can also be declared final. Final method: A final method cannot be overridden in a subclass.
Final field: A final field is like a constant: once it has been given a value, it cannot be assigned to again.
No comments:
Post a Comment