Java interview questions and answers.

 Java Interview Questions and Answers

This article will help you to prepare for the interview and get much confidence to crack. Here I have tried to put more effort into making the answers much easier with simple explanations. I have tried to add as much as good and frequently asked questions.

1. What is the difference between overloading and overriding?

    If a class is overloaded with multiple methods with the same name and different method signatures is known as overloading.

package com.codeforsolution.java.logical;

public class Calculator {

     public static int add(int a,int b) { return a+b; }

     public static int add(int a, int b, int c) { return a+b+c; }

     public static double add(double d, double e) { return d+e; }

}

                        Here we have multiple methods with name add but the parameters are different based on the number or based on the type. This concept is called overloading.

                        When a child class or sub-class provides a specific implementation of a method already provided by one of its super-class or parent classes. When a method of a child class has the same method signature and the same return type(or sub-type) as a method in its super-class, then the method in the child class is said to override the method in the parent class. This concept is called overriding.

Example:-

package com.codeforsolution.java.logical;

class Animal {

void eat() {

System.out.println("Animal eat method");
}
}

class Cat extends Animal {

@Override

void eat() {

System.out.println("Cat eat method");
}
}

public class Main {

public static void main(String[] args) {

Animal animal = new Animal();
animal.eat();
Cat cat = new Cat();
cat.eat();
}
}


Output:-
                    Animal eat method
                    Cat eat method

 Overloading occurs between the method in the same class whereas overriding occurs between parent and child classes.
 In Overloading methods, names are the same but the parameters are different whereas in Overriding method signature should be the same.
The Overloading method call is determined on the compile-time whereas the Overriding method call is determined on runtime.
Overloading method means compile time method is known as compile-time Polymorphism whereas overriding means runtime method call is known as runtime polymorphism.
Overloading will give an error on compile-time and overriding on runtime, so if anyone breaks, overriding will cause a more serious issue than overloading.
Performance-wise overloading is better because it occurs at compile-time whereas the overriding method occurs at runtime.
Private and final can be used in overloading but can’t be used in overriding.
A static method can be used in overloading but not in overriding because a static method is not a run-time polymorphism.
2. Why is the  String class Immutable?

Immutable class means once an object is created we can’t change or modify it. In Java, all wrapper classes like Integer, Float, Double, Long, Short, Byte, Char, Boolean and String.
        The main factors for keeping the String class immutable are caching, security, synchronization and performance.
                The String literal concept with string pool is important to make performance fast. Actually, whenever we are using the string literal concept to make a string object first it will check the value in the string pool and if it is already available it will not create a new object in the String pool simply it will refer to the same object. So in this case it saves a lot of JVM memory and performance is also fast due to not burden of creating new objects.

Immutability provides security. For example, the username, password, details, etc. are passed in secure login applications as the String. As String is immutable, its value can’t be changed. Otherwise, any hacker could change the referenced value which will cause serious security issues.
                Due to immutability, String is thread-safe because it won’t be changed when accessed from multiple threads and if any thread tries to change the value, then instead of modifying the same string object value it will a new String will be created in the String pool.

3. How can we create our own Custom Immutable class?
  To create an immutable class in Java. We have to follow below steps:-
We have to declare the class as final. So, it can't be extended.
We have to define all of the fields as private. So, direct access is not allowed.
Don't provide setter methods for variables.
Initialize all fields using a constructor method(it will perform deep copy).
We have to make all mutable fields final. So, that field's value can be assigned only once.
In the getter methods, do not return actual object reference instead return a copy of the object.

package com.codeforsolution.java.logical;
public final class Employee {
private final String name;
private final int age;
private final String gender;
public Employee(String name, int age, String gender) {
super();
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}

public int getAge() {
return age;
}

public String getGender() {
return gender;
}
}


Post a Comment (0)
Previous Post Next Post