Answer to why method overriding is called runtime polymorphism in java is because the methods get resolved at the Run-Time. In simple words, when you execute a program, the method of which class out of many will be called, if they have overridden the method. Let’s understand by very simple scenario with code example for […]
Examples where use of Java Static method is Indispensable
Let’s see some examples where use of java static methods are mandatory in the programs. Generally, we use static method where we want to call a method without creating an object of the class. Here are examples where we cannot avoid using static methods in java and are indispensable. NOTE: Generally, this question is asked […]
Why we need double check in singleton class in java? – Answered
Answer to why we need double check locking in singleton class in Java with thread program example. Let’s understand first what does the double checking means? Consider the method getInstance() of below singleton class. You will see that there are two if conditions to check if the singleton class object is null. So, question is, […]
How to create immutable class in java – Simplest Example
We create immutable class in java by making the class fields final and private, and by providing getter setter method, and returning new object with new data on object modification Technical interview question is that, make a user defined class immutable. Write a simple immutable class and Explain it. also, demonstrate that it is immutable […]
What to choose between abstract class and interface if…
Answer to what to choose between abstract class and interface if both contain only abstract methods. This is the frequently asked technical interview question. Here I’m providing two reasons with examples. Let me elaborate the question for your better understanding. You know that interface contains only abstract methods. But, consider the restriction that abstract class […]
What is method signature in Java – Does it include return type?
What is method signature in java ? – Method signature in java includes only method name and parameters. Method return types are not included in method signature. For example, in below class method declaration only “foo(String str)” is the method signature that contains method name and parameters. Return type String should in excluded. NOTE: Overloaded […]
What is the use of volatile keyword in java?
Use of volatile keyword in Java – The volatile keyword is used to declare a variable volatile, so, the variable can be accessed directly from the memory not from intermediary cache. For example, If you declare a variable volatile, then it will be directly accessed from the memory where it resides and not from the […]
Can we overload main method in java?
Interview Question – Can we overload main method in java programs, for example in below class Sample, public static void main(String[] args) is given, that is the start point of the program. So, can we write more methods with same name i.e. main (method overloading)? Answer:Yes, we can overload main method in java program. We […]
Why to use Abstract keyword in java language?
Abstract keyword is used to create an abstract class and abstract methods in java programs. Abstract class: Abstract class in java is used as a base class in inheritance relationship. Abstract class have both non-abstract (implemented methods) and abstract (un-implemented methods) that must be overridden by sub classes.Note that If we want to have common […]
What are methods of object class in java? – with description
Methods of Object class in java with brief descriptions – The java.lang.Object class is the super class of all classes whether the class is predefined or user defined class. Object class is by default inherited whether we extend explicitly or not. Methods of object class in java : protected native Object clone() throws CloneNotSupportedException public boolean equals(Object […]