Core JavaInterview questionsJava

Java Interview questions- part 2

This post contains part2 of Java Interview questions.

For part1 of the Java interview questions, please refer this link:

Java interview questions part 1


Here are the second set of Java interview questions.


Can constructors be inherited in Java?

No, constructors cannot be inherited in Java.

If you want to use the functionality in super class constructor, then inherit the Class and in the child class constructor, call the super class’s constructor using super().


What is difference between Deep Cloning and Shallow Cloning in Java?

In Shallow Cloning, the immediate object is copied but its contained objects are not copied, rather the new object maintains reference to the same contained objects.

Deep Cloning copies the object and any contained objects inside it.


What is the difference between an instance variable and a static variable?

Static variables are class level variables. There is only one occurrence of it per JVM per Class Loader. The static variables are initialized when class is loaded for first time.

Instance variables are related to the class instance. There is one occurrence of an instance variable for each instance of the class.


What is difference between final, finally and finalize in java?

Declaring a variable as final makes it a constant. If a class is declared final, then it cannot be extended. Final methods cannot be overridden. So, basically making something as final is similar to treating it as constant or unchangeable.

Finally is an optional construct in exception handling. It is added after the catch or try block. Finally block is used to release resources used in try block since finally block executes after the try/catch no matter what.

finalize() method helps in garbage collection. This method is invoked before GC discards an object.


When to use inner class in Java ?

Inner classes should be used when an inner class is relevant only in the context of an outer class.

UI frameworks like Swing, GWT use anonymous inner classes.


What is the difference between Aggregation and Composition?

 Aggregation is a part-whole relationship where the part can exist without the whole.

Composition is a part-whole relationship where the part cannot exist without the whole.



© 2015, All rights reserved.