PS: If you like the page or have any questions, feel free to comment at end.
Q) What is polymorphism?
Ans) The ability to define a function in multiple forms is called Polymorphism. In java, c++ there are two types of polymorphism: compile time polymorphism (overloading) and runtime polymorphism (overriding).
Method overriding: happens when a child class implements the method with the same signature as a method in a parent class. When you override methods, JVM determines the proper methods to call at the program's runtime, not at the compile time.
Method overloading: happens when several methods have same names but different number or type of parameters. Overloading is determined at the compile time.
int add(int a,int b)
float add(float a,int b)
float add(int a ,float b)
void add(float a)
int add(int a)
void add(int a) //error conflict with the method int add(int a)
class BookDetails {
String title;
setBook(String title){}
}
class ScienceBook extends BookDetails {
setBook(String title){} //overriding
setBook(String title, String publisher,float price){} //overloading
}
Q) What is the difference between final, finally and finalize() in Java?
Ans) final - A final variable acts as a constant, a final class is immutable and a final method cannot be overriden in a child class.
finally - finally keyword is used with try-catch block for handling exceptions. The finally block is optional in try-catch block. The finally code block is always executed after try or catch block is completed. The finally block is mainly executed to close the resources or clean up objects used in the try block. For e.g. Closing a FileStream, I/O stream objects, Database connections, HTTP connections are generally closed in a finally block.
finalize() - This is the method of Object class.It is invoked before an object is discarded by the garbage collector, allowing it to clean up its state. Should not be used to release non-memory resources like file handles, sockets, database connections etc because Java has only a finite number of these resources and you do not know when the garbage collection is going to kick in to release these non-memory resources through the finalize() method.
Q4) What is the difference between HashMap and HashTable?
Ans) Both collections implements Map. Both collections store value as key-value pairs. The key differences between the two are:
Q) What is the difference between abstract class and interface ?
Ans) A class is called abstract when it is declared with the keywordabstract
. Abstract class contains at least one abstract method. It can also contain n numbers of concrete method. An interface can only contain abstract methods.
Q) What is the difference between equals() and == ?
Ans) == operator is used to compare the references of the objects.
public boolean equals(Object o) is the method provided by the Object class. The default implementation uses == operator to compare two objects which mean by default the objects address are compared.
But since the method can be overriden like for String class. equals() method can be used to compare the values of two objects.
String str1 = "MyName";
String str2 = "MyName";
String str3 = new String(str2);
if (str1 == str2) {
System.out.println("Objects are equal")
}else{
System.out.println("Objects are not equal")
}
if(str1.equals(str2)) {
System.out.println("Objects are equal")
} else {
System.out.println("Objects are not equal")
}
Output:
Objects are not equal
Objects are equal
String str2 = "MyName";
String str3 = str2;
if (str2 == str3) {
System.out.println("Objects are equal")
}else{
System.out.println("Objects are not equal")
}
if (str3.equals(str2)) {
System.out.println("Objects are equal")
} else {
System.out.println("Objects are not equal")
}
Output:
Objects are equal
Objects are equal
Q) What is the use of synchronized keyword?
Ans) synchronized keyword can be applied to static/non-static methods or a block of code. Only one thread at a time can access synchronized methods and if there are multiple threads trying to access the same method other threads have to wait until current thread finishes the execution and release lock. Synchronized keyword provides a lock on the object and thus prevents race condition. E.g.
public void synchronized method(){}
public void synchronized staticmethod(){}
public void myMethod(){
synchronized (this){
//synchronized keyword on block of code
}
}
Recommend Reading