Previous | Home | Next |
Answer: 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.
Answer: When we have to want child class to implement the behavior of the method.
Answer: Yes, We can call a abstract method from a Non abstract method in a Java abstract class
Q.16 What is the difference between an Abstract class and Interface ? And can you explain when you are using an Abstract classes ?
Answer: 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.
Answer: 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 :
- Create an class which extends Exception:
public class greaterVlaueException extends Exception { }
- Throw an exception using a throw statement:
public class Fund { /java/. public Object getFunds() throws greaterVlaueException { if (id>2000) throw new greaterVlaueException(); /java/. } }
User-defined exceptions should usually be checked.
Answer: All predefined exceptions in Java are either a checked exception or an unchecked exception. Checked exceptions must be caught using try /java/ 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.
Answer: 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.
Answer: 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.
Answer: 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 .
Answer: 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.
Previous | Home | Next |