Core Java Interview questions-part1

1) what are static blocks or static initalizers in Java ?

Static blocks or static initializers are used to initalize static fields in java.

we declare static blocks when we want to intialize static fields in our class.

Static blocks gets executed exactly once when the class is loaded .

Static blocks are executed even before the constructors are executed.

2)  How to call one constructor from the other constructor ?

With in the same class if we want to call one constructor from other we use this() method. Based on the number of parameters we pass appropriate this() method is called.

Restrictions for using this method :

1) this must be the first statement in the constructor

2)we cannot use two this() methods in the constructor

 3)  What is method overriding in java ?

If we have methods with same signature (same name, same signature, same return type) in super class and subclass then we say

subclass method overrides  superclass.method.

When to use overriding in java

If we want same method with different behaviour in superclass and subclass then we go for overriding.

When we call overridden method with subclass reference subclass method is called hiding the superclass method.

4) What is super keyword in java ?

Variables and methods of super class can be overridden in subclass . In case of overriding , a subclass object call its own variables and methods. Subclass cannot access the variables and methods of superclass because the overridden variables or methods hides the methods and variables of super class.

But still java provides a way to access super class members even if its members are overridden.  Super  is used to access superclass variables, methods, constructors.

Super can be used in two forms :

1)      First form is for calling super class constructor.

2)      Second one is  to call super class variables,methods.

Super if present must be  the first statement.

5)  Difference between method overloading and method overriding in java ?

Method Overloading Method Overriding
1) Method Overloading occurs with in the same class Method Overriding occurs between two  classes superclass and subclass
2)  Since it involves with only one class inheritance is  not involved. Since method overriding occurs between superclass and subclass inheritance is involved.
3)In overloading return type need not be the same 3) In overriding return type must be same.
4) Parameters must be different when we do overloading   4) Parameters must be same.
 5) Static polymorphism can be acheived using method  overloading  5) Dynamic polymorphism can be acheived using method overriding.
6) In overloading one method can’t hide the another 6) In overriding subclass method hides that of the superclass method.

4 thoughts on “Core Java Interview questions-part1

    • Hi Pallavi,

      We use super() only to call super class constructor.If in the same class if we want to call one constructor from another we use this .

      For reference see this java doc. If you are not clear when to use this and super just reply to this comment

    • though This() method we can call same class constructor. Please find sample code:

      class A{
      A(){
      this(10); // calling arg contructor of A from default contructor
      System.out.println(“constructor from A()”);
      }
      A(int x){
      System.out.println(“Contructor from A(int)”);
      }
      }
      public class Const {
      public static void main(String[] args) {
      A b1=new A(); // OutPut: Contructor from A(int)
      // Constructor from A()
      }
      }

Leave a Reply

Your email address will not be published. Required fields are marked *