100 Core Java Interview Questions and Answers - Part 4

Here are top selected core java interview questions asked in high level interviews, these questions may be helpful for both freshers or experienced professional going for interview.

31) How to prevent a method from being overridden in Java?

To prevent a method from being overridden in java we have declare the method as final, if a method is declared as final that a subclass cannot override it.

32) What if a class is declared as final in Java?

If we declare a class as final in java, than it cannot be extended by other class. A abstract class cannot be declared as final.

33) What is the difference between final, finally and finalize in Java ?

?final? is a modifier which can be applied to a class, variable or a method. A final class can not be subclassed , a final method can not be overridden and a final variable can not be changed once created. finally is a exception handling block which is executed regardless the exception is being raised or not in try catch blocks. finalize() is a method from Object class which is being called by the garbage collector to give a final chance to unused objects to release their activity before removed from the memory.

34) What if a method is declared as static in Java?

We declare a method as static in java, when we need to access that object even before the creation of the object of that class, static methods can be called with the name of class only.

35) What if a variable is declared as static in Java?

If a variable is declared as static, than it is assumed as a class level variable. All the instances of that class will share a common copy of thet variable that means of one instance changed the value of a static variable than the change is being reflected to all other instances of the class.

36) Can we declare a local variable as static in Java?

No, static variables are class level variables we cannot declare a local variable as static. If we do so, it will give a compile time error.

37) What is an Abstract class in Java? And why do we need it?

An abstract class is a declared with abstract modifier, we cannot instantiate an abstract class, so abstract class must be extended by some other class to make sense. Abstract class can have both abstract and concrete methods in it. Abstract class is useful in obtaining abstraction in own program.

38) Can an interface implement another interface in Java ?

No, not possible. In java one interface extends another interface and not implement that.