Question 1: How static polymorphism and dynamic polymorphism is achieved?
Answer: Static polymorphism is a compile time polymorphism and can be achieved through overloading of methods. At the time of compilation, it is known which method will execute in response to a method call.
Dynamic polymorphism is also called run-time polymorphism. The method execution is known at run time depending upon the object being referred to . It can be achieved by overriding methods in class and in Interface implementation. As this is done dynamically so it is called dynamic polymorphism.
Question 2: Explain purpose and exact use of finally vs finalize().
Answer:
Finally:-
1) The finally block always executes when the try block exits, except System.exit(0) call.
2) Ensures that the finally block is executed even if an unexpected exception occurs.
3) Finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.
Finally:-
1) The finally block always executes when the try block exits, except System.exit(0) call.
2) Ensures that the finally block is executed even if an unexpected exception occurs.
3) Finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.
Finalize:-
1) This is used to clean up memory resources for object using finalize().
2) This is used just before an object is destroyed and can be called just prior to garbage collection.
1) This is used to clean up memory resources for object using finalize().
2) This is used just before an object is destroyed and can be called just prior to garbage collection.
No comments:
Post a Comment