Java Flashcards
In terms of inheritance, what is the effect of keeping a constructor private?
This prevents creating subclasses of this class. It also prevent instantiation of this class outside of the class itself.
Does the finally block get executed if we insert a return statement inside the try block of a try-catch-finally?
Yes. If the try block exits, the finally block is executed.
What is the difference between final, finally, and finalize?
Explain the difference between templates in C++ and generics in Java.
- Java generics is rooted in an idea of “type erasure”. This technique eliminates the parameterized types when source code is translated to JVM bye code.
- In C++, the compiler creates a new copy of the template code for each type.
Explain what object reflection is in Java and why it is useful.
Object reflection is a feature in Java which provides a way to get reflective information about Java classes and objects, and perform operations such as:
- Getting information about the methods and fields present inside the class at runtime.
- Creating a new instance of a class.
- Getting and setting the object fields directly by getting field reference, regardless of what the access modifier is.
Useful because:
- It helps in observing or manipulating the runtime behavior of applications.
- It can help in debugging or testsing programs, as we have direct access to methods, constructors and fields.
- We can call methods by name when we don’t know the method in advance.