Top Java OOPs Interview Questions

While you go for interview the most asked questions from core java both for fresher and experienced java programmers.

  1. What is Object Oriented Programming? What are the advantages of OOPs?

    Object Oriented Programming is a technique which combines data and operations/behavior into a single unit called object.  Object-oriented programming helps modelling complicated systems of real world into manageable software solutions. In OOPS programming programs are organized around objects rather than actions and logic.

    The following are advantages of OOPs.
    Encapsulation
    Inheritance
    Polymorphism
    – Abstraction

  2. What is Abstraction?

    In object-oriented programming, abstraction is the technique through, a programmer hides all but the relevant data about an object in order to reduce complexity and increase efficiency. Basically by this programmer hides the internal details and shows only relevant information to the rest of the world.

  3. How to achieve Abstraction?

    Abstraction can be achieved using abstract class and interface. If you have partial implementation then go for abstract class. Before Java 8, it was not possible to provide non-abstract method(method with definition) within the interface.
    From Java 8 you can define default and static methods with the interface.

  4. What is Encapsulation?

    Encapsulation is one of the four fundamental OOP concepts. The other three are inheritance, polymorphism, and abstraction. It is a mechanism of wrapping the data (variables) and methods together as a single unit.

  5. How to achieve Encapsulation?

    Encapsulation can be achieved by using access modifiers private,public,protected. You can restrict the access as per your need. Like we can make data member private to be accessible through class methods only. No one can access private data members outside the class.

  6. What is Polymorphism?

    It is derived from 2 greek words poly and morphs. The word “poly” means many and “morphs” means forms. So polymorphism means many forms.

    Polymorphism is one of the major feature that allows us to perform a single operation in different ways. For example, we can send text or MMS message through mobile phone with single method sendMessage().

  7. What is Inheritance?

    Inheritance is one of the major feature of OOPs which facilitates the code reusability. Through inheritance subclass inherits the data and methods from its parent class. Java supports only single inheritance means a class can extend only single class. Java does not support multiple inheritance.

  8. What are access modifiers allowed at class level?

    final, abstract and public modifiers are allowed at class level.

  9. Why Java does not support Multiple Inheritance?

    Java does not support multiple inheritance due to Diamond Problem. In case of multiple inheritance, suppose class X has two subclasses Y and Z, and a class W has two super classes Y and Z. If a method present in X is overridden by both Y and Z but not by W then from which class W will inherit that method Y or Z? This problem is known as diamond problem.

  10. What is Static Binding and Dynamic Binding?

    Static or early binding is resolved at compile time. Method overloading is an example of static binding. Dynamic or late or virtual binding is resolved at run time. Method overriding is an example of dynamic binding.

  11. What is Method hiding?

    When you declare two static methods with same name and signature in both super class and subclass then they hide each other i.e. a call to the method in the subclass will call the static method declared in that class and a call to the same method is super class is resolved to the static method declared in the super class.

  12. What are “IS-A” and “HAS-A” relationships?

    When a class B extends class A then it is called IS-A relationship where B extends the properties and methods of its parent class. If class keeps the reference of some other class as its member then it is called HAS-A relationship. Like Class B has class A reference as member.

  13. What is Method Overloading and Overriding?

    Method overloading is the process by which we can define more than one method with different signature within the class. It is done at compile time. Declaring a method in subclass which is already present in the parent class is known as method overriding. Overridden method must have same method signature as in parent class.

  14. What are the rules for Method Overloading?

    To overload methods you must change the signature of methods. Signature of method contains types of arguments and order of arguments. Following are the rules to overload a method.
    -Change the method signature
    -Method can’t be overloaded by changing the return type of method.

  15. What are the rules of Method Overriding?

    A overridden method should have same signature. The return type of overridden method can be covariant added in Java 5 which facilitates to change the return type of the overriding method in the subclass. However the return type in subclass method must be a sub-type of super class method return type.

  16. Can method be overloaded by changing the return type?

    -No, you should atleast change the types of arguments or order of arguments.

  17. Can overridden method be overloaded?

    Yes. Overridden method can be overloaded.

  18. Can we override static method in Java?

    No, you cannot override a static method because it’s not bounded to an object. Static methods belong to a class and resolved at compile time using the type of reference variable. But, you can declare the same static method in a subclass, that will result in method hiding.

  19. Can we override a static method as instance method?

    No, instance method cannot override the static method.

  20. Can we override a private method in Java?

    No. Since the private method is only accessible and visible inside the class only, They cannot be overridden in subclasses. You can override them inside the inner class as they are accessible there.
  21. How to prevent method overriding?

    The final keyword is used to prevent method overriding. We can make method final and its can’t be overridden by child class.

  22. How to stop class inheritance?

    We can make class final to stop inheritance means no subclass can be created for final class.

  23. Can we overload final method?

    Yes. We can overload final method but can’t override them.

  24. What is default constructor?

    Constructor without any arguments is called default constructor. Compiler will add default constructor if there is no constructor inside a class.

  25. What is Constructor overloading?

    Within a class we can have default and parameterized constructors. If you don’t define any constructor, compiler automatically create a default constructor for that class. But while you define parameterized constructor compiler will not add default constructor. So you should add default constructor also while defining parameterized constructors.

  26. Why java 8 introduces default method in an interface?

    It was added primarily to let you add methods to existing interfaces that are already in use without breaking everyone’s code, but also to share method implementations across classes implementing the same interface.
    We can create default method using default keyword. Read more.

  27. Why java 8 introduces static method in an interface?

    Static method was added in Java 8 to restrict the concrete class to override the implementation.
    If you define static method in concrete class with the same name it will hide the interface static method.

    Note:- If you annotate overriden method in concrete class using @Override then compiler time error for no method is in SuperType. As static method from interface is not visible for their implementation class.

    interface A{
     static void test(){
      System.out.println("hello");
     }
    }
    
  28. What are the sequence of execution of instance block,static block and constructor?

    First static block get executed while loading class. Each instance block get placed at the top of constructor method and constructor method get executed.

     
    package com.startwithjava; 
    class X{ 
    static{ 
    System.out.println("X static block"); 
    }
    { 
    System.out.println("Instance block 1"); 
    } 
    public X(int x){ 
      System.out.println("X constructor()"); 
    } 
    
    {
     System.out.println("Instance block 2"); 
    } 
    
    } 
    
    public class ConstructorOverloading { 
    public static void main(String[] args) { 
     X x= new X(10); 
     } 
    } 
    

    Output

    X static block
    Instance block 1
    Instance block 2
    X constructor()
    
  29. What is Functional Interface?

    Functional Interface is added in Java 8 to support lambda expression. Functional interface can have only one abstract method. Java 8 provide @FunctionalInterface annotation for which give compile error while you define more than one abstract method in functional interface. Read More

  30. What is Output of following?

    package com.startwithjava;
    class A{
    public int x=10;
    public void m1(){
    System.out.println("A m1()");
    }
    }
    class B extends A{
    public int x=20;
    public void m1(){
    System.out.println("B m1()");
    }
    }
    public class OverloadingExample {
    
    public static void main(String[] args) {
    A b = new B();
    System.out.println(b.x);
    b.m1();
    }
    }
    
    

    Output

    10
    B m1()
    
  31. What will be output of following?

    package com.startwithjava;
    class A{
    public int x=10;
    public void m1(){
    System.out.println("A m1()");
    }
    }
    class B extends A{
    public int x=20;
    public void m1(String name){
    System.out.println("Name="+name);
    }
    }
    public class OverloadingExample {
    
    public static void main(String[] args) {
    A b = new B();
    System.out.println(b.x);
    b.m1("Abc");
    }
    
    }
    

    Output

    As "b" is reference of class A that's why method m1(String) is not visible.
    As parent class could not access the child class methods.
    
  32. What will be output of following?

    package com.startwithjava;
    
    class X{
    static{
    System.out.println("X static block");
    }
    {
    System.out.println("Instance block 1");
    }
    public X(){
    System.out.println("X constructor()");
    }
    {
    System.out.println("Instance block 2");
    }
    }
    class Y extends X{
    static{
    System.out.println("Y static block");
    }
    public Y(){
    System.out.println("Y constructor()");
    }
    {
    System.out.println("Instance block 3");
    }
    {
    System.out.println("Instance block 4");
    }
    }
    public class ConstructorOverloading {
    
    public static void main(String[] args) {
    X y= new Y();
    }
    
    }
    

    First static block get executed for both parent and sub class. Then instance blocks are placed under constructor method. So parent class constructor invoked first and then sub class constructor invoked.

    Output

    X static block
    Y static block
    Instance block 1
    Instance block 2
    X constructor()
    Instance block 3
    Instance block 4
    Y constructor()
    
  33. What will be output for the following?

    package com.startwithjava;
    
    public class MethodOverloading {
    public static void m1(int x,double y){
    System.out.println("int,int");
    }
    public static void m1(double x,double y){
    System.out.println("double,double");
    }
    public static void m1(double x,int y){
    System.out.println("double,int");
    }
    public static void main(String[] args) {
    m1(10,10);
    }
    
    }
    
    

    Output

    The compiler will complain for “The method m1(int, double) is ambiguous for the type MethodOverloading”. As we see there are two ambiguous methods m1(int,double) and m1(double,double).

  34. What are output of following?

    package com.startwithjava;
    
    public class MethodOverloading {
    public static void m1(String s){
    System.out.println("string");
    }
    public static void m1(StringBuffer sb){
    System.out.println("stringbuffer");
    }
    public static void m1(Object object){
    System.out.println("object");
    }
    public static void main(String[] args) {
    m1(null);
    }
    
    }
    

    Output

    Compile time error for ambiguous methods, m1(String) and m1(StringBuffer). Where there is overloaded methods with child and parent as arguments then in this case compiler choose the most specific method(child class argument).

    As here are three methods m1(Object),m1(String) and m1(StringBuffer), compiler first looks for for most specified method but there are two sub classes String and StringBuffer for Object class. No inheritance there between String and StringBuffer and null can hold by any reference type that why there is ambiguity issue.

  35. What is output for following?

    interface A1{
         default void test(){
            System.out.println("A test()");
          }
    }
    interface B1 {
       default void test(){
           System.out.println("B test()");
        }
       default void test(String msg){
         System.out.println("B test(msg)");
        }
    }
    class C1 implements A1,B1{
    public static void main(String args[]) {
     C1 c1 = new C1();
     c1.test();
     c1.test("Testing");
     }
    }
    

    Output:

    As we know that Java 8 introduces default methods in interface. As there are two default methods in interface A1 & B1 to C1.
    So in this case compiler will raise an error “Duplicate default methods named test with the parameters () and () are inherited from the types B1 and A1”.

    To resolve this either we need to define test() method in C1 class or need to remove that method from anyone interface A1 or B1.

  36. What will be output for following?

    interface A1{
      default void test(){
        System.out.println("A test()");
      }
      
    }
    interface B1 extends A1{
    	default void test(){
    	    System.out.println("B test()");
    	 }
    	 default void test(String msg){
    	    System.out.println("B test(msg)");
    	 }
    }
    class C1 implements B1{
    public static void main(String args[]) {
     C1 c1 = new C1();
     c1.test();
     c1.test("Testing");
     }
    }
    

    Output:
    The program will execute successfully and prints below messages.

    B test()
    B test(msg)
    
    

Leave a Reply