Top 32 OOPs Interview Questions in Java with Answers

Last Updated on February 14, 2021 by Scientech Easy

Here, we have listed the most important tricky OOPs interview questions in java with the best possible answers.

These realtime OOPs interview questions in java can be asked in any technical interviews from freshers and experienced 3 to 5 years.

If you are a developer or tester and want to crack the interview, you should prepare answers to these OOPs interview questions.

We have listed all OOPs interview questions and answers topic wise so that you do not have any problem understanding.

Frequently Asked OOPs Interview Questions in Java

OOPs Interview Questions in Java

1. What are the core OOPs concepts in Java?
Or, What are the basic principles of OOPs concept in Java?
Or, What is the main fundamental principles (main pillars) of the OOPs concepts in Java?

Ans: There are four basic principles that make a language Object Oriented. They are as follows:

Encapsulation: It is the mechanism of hiding data implementation by restricting access to public methods.

Abstraction: It is a technique by which we can hide the data that is not required to a user.

Inheritance: It is a process by which a child class acquires all the properties and behaviors of the parent class.

Polymorphism: Polymorphism means many forms. It is the technique by which we can perform a single task in different ways. It is further divided into two types – static and dynamic.

Static polymorphism is achieved using method overloading and dynamic polymorphism using method overriding.

2. What Is Object-Oriented Programming in Java?

Ans: Object-oriented programming (OOP) in Java is a programming methodology or paradigm (model) to design a computer program using classes and objects.

It is the most popular programming paradigm and widely used in the software industry today. It is an extension of procedural programming.

3. What is the difference between procedural programming and object-oriented programming?

Ans: Procedural programming means writing code without objects.

Object-oriented programming means writing code with objects which contain data in the form of fields (often known as properties in java) and functionality code in the form of methods (often known as behavior in java).

4. What is Object-Oriented Programming System (OOPs) in Java?

Ans: Writing a program using features like encapsulation, abstraction, inheritance, and polymorphism is called object-oriented programming system.


5. Name some programming languages that implement OOPs features.

Ans: Smalltalk, C++, Java, PHP, Python, etc implement OOPs features. Java is a pure object-oriented language. Smalltalk is the first truly object-oriented programming language.

C#, C++ is a procedural language with object-oriented extension.

6. What is the basic difference between Object Oriented Programming language and Object Based Programming language?

Ans: Object Oriented Programming languages like Java, Python, C++ follow concepts of OOPS like Encapsulation, Abstraction, Polymorphism, and Inheritance, etc.

Object-Based Programming languages like JavaScript, VBScript, etc follow some features of OOPS but they do not support Polymorphism and Inheritance.

7. What are some important features of OOPs concept in Java?

Ans: Some of the important striking features of OOPs concept are as follows:

  • Higher priority is focused on data rather than functions.
  • Programs are divided into the number of entities known as objects.
  • Objects communicate with each other through functions (methods).
  • Methods that operate on data of an object are closely bound together in the data structure.
  • Data is hidden in the OOP and cannot be accessed by external methods. Hence, it is safe from accidental modification.
  • Data cannot move freely out of the object.
  • New data and methods can be easily added whenever needs.

8. What are the advantages/benefits of OOPs in Java?

Ans: OOPs concept in Java offers several advantages that are not available in procedural programming like C, Pascal, etc. Some of the major and important benefits of object-oriented programming are as follows:

  • Security
  • Reusability
  • Effective communication
  • Developing complex software
  • Easily upgraded
  • Easy partition of work
  • Maintenance
  • Efficiency

9. What are applications of OOPs concepts in Java?

Ans: Due to its reusability feature, it is widely used in many areas. Some of the important application areas of OOPs are as follows:

  • Real-time systems
  • Object-oriented database
  • Graphical user interface design in the Windows operating system.
  • Artificial intelligence and expert systems
  • Parallel programming
  • CAD/CAM software and in many areas.

10. What are the key points for developing a clean Object Oriented program?

Ans: There are several key points for developing a clean object-oriented code that must be kept in mind. They are:

  1. Program to an interface (or the super type) not the implementation.
  2. Interacting of Classes should be loosely coupled among themselves.
  3. Code should implement tightly encapsulation. Avoid the use of public, static variables, and singleton design pattern wherever possible.
  4. Always reuse the code using inheritance, composition, and utility methods.
  5. Has-A relationship is better than Is-A relationship because it provides more flexibility.
  6. In the case of multi-threading applications, use immutable objects to represent the state.
  7. Make proper use of design patterns wherever possible.
  8. Use up-to-date software dependencies and make the best use of the latest technology available to us.

11. What is Encapsulation in Java?

Ans: Encapsulation is the programming technique of making fields in a class private and providing access to those fields via public methods.
In other words, encapsulation is the technique of hiding implementation details and providing methods for data access.

12. What is Data hiding?

Ans: Data hiding is the process in which the field is declared private within the class to hide so that no one can access it from outside the class.

13. How to implement Encapsulation in Java?

Ans: There are two key points thereby we can achieve or implement encapsulation in Java program.

  • Declaring the instance variable of the class as private so that it cannot be accessed directly by anyone from outside the class.
  • Provide the public setter and getter methods in the class to set/modify the value of the variable.

14. How to achieve data hiding programmatically?

Ans: By declaring data members (variables) as private, we can achieve data hiding. If variables are declared as private in the class, nobody can access them from outside the class.

The biggest advantage of data hiding in java is we can achieve security in the application.

15. In the following code, radius is declared as private in class Test, and t is an object of the class Test. Does the code cause any problems? If so, explain why?

public final class Test {
 private double radius = 1;
 
public double getArea() {
    return radius * radius * Math.PI;
  }	
public static void main(String[] args) 
{
  Test t = new Test();
  System.out.println("Radius of circle:  " + t.radius);
  System.out.println("Area of circle: " +t.getArea());
  }
}

Ans: Yes, the above code will be executed successfully. It will not cause any problem. The output is

  • Radius of circle: 1.0
  • Area of circle: 3.141592653589793

16. What is meant by tightly encapsulated class in Java?

Ans: If each variable is declared as private within the class, it is called tightly encapsulated class in Java.

17. Which of the following classes are tightly encapsulated?

class A 
{
    private int x = 20;
}
class B extends A 
{
   int y = 50;
}
class C extends A 
{
  private int z = 10;
}

Ans: Class A and Class C are tightly encapsulated classes. Class B is not tightly encapsulated class because of the non-private variable y. Anyone can access it directly from outside the class.

18. Are all classes P, Q, R tightly encapsulated in the following code? If yes how?

class P {
 int a = 10;
}
class Q extends P {
  private int b = 20;
}
class R extends Q {
  private int z = 30;
}

Ans: None of these is tightly encapsulated class because class Q is the child class of P. Non-private data member of class P by default is available in subclass Q.

Similarly, class R is the subclass of Q. The non-private data member of class Q by default is available inside the class R.

19. What is an accessor method and mutator method?

Ans: A method that is used to retrieve the value of a variable is called getter or accessor method.

A method that is used for updating or setting the value of a variable is called setter method or mutator method.

20. In the following source code, does the code cause any problem? If yes, why?

public class Student { 
  private String name; 
 public String getName() { 
    return name; 
 } 
 public void setName(String studentName) { 
   name = studentName; 
  } 
}
class EncapsulatedTest { 
 public static void main(String[] arg) { 
   Student obj = new Student(); 
    obj.name = "John";
    } 
}

Ans: The above code will generate compile-time error because variable name has been declared as private in the class Student. So, the data member from outside the class cannot be accessed directly.

21. What would happen if we do not use encapsulation in java program?

Ans: If we don’t use encapsulation in a program, fields will not be private and could be accessed by anyone from outside the class.

For more encapsulation interview questions, go to this tutorial: Java Encapsulation Interview Questions and Answers.

22. What is Inheritance in Java?

Ans: The technique of creating a new class by using an existing class functionality is called inheritance in Java. In other words, inheritance is a process where a child class acquires all the properties and behaviors of the parent class.

23. Why do we need to use inheritance?

Ans: Inheritance is one of main pillars of OOPs concept. Some objects share certain properties and behaviors. By using inheritance, a child class acquires all properties and behaviors of parent class.

There are the following reasons to use inheritance in java.

  • We can reuse the code from the base class.
  • Using inheritance, we can increase features of class or method by overriding.
  • Inheritance is used to use the existing features of class.
  • It is used to achieve runtime polymorphism i.e method overriding.

24. What is Is-A relationship in Java?

Ans: Is-A relationship represents Inheritance. It is implemented using the “extends” keyword. It is used for code reusability.

25. What is super class and sub class?

Ans: A class from where a subclass inherits features is called superclass. It is also called base class or parent class.

A class that inherits all the members (fields, method, and nested classes) from another class is called subclass. It is also called a derived class, child class, or extended class.

26. How is Inheritance implemented/achieved in Java?

Ans: Inheritance can be implemented or achieved by using two keywords:

  1. extends: extends is a keyword that is used for developing the inheritance between two classes and two interfaces.
  2. implements: implements keyword is used for developing the inheritance between a class and interface.

For more Inheritance Interview Questions for best practice, go to this tutorial: Top 50 Java Inheritance Interview Questions and Answers for Freshers and experienced

27. Can a derived class be referenced for a base class object?

Ans: No. If School is a superclass and Student is the subclass then the following reference would be wrong – Student st = new School();

28. Can a parent class be referenced for a child class object?

Ans: Yes. It is applicable in java. An example is – School sc = new Student();

29. What is the use of super keyword in Java?

Ans: super keyword is a reference variable that refers to an immediate superclass object. It is used for the following purposes:

  • To refer immediate parent class instance variable.
  • To call immediate parent class constructor.
  • To invoke immediate superclass method.

30. Why do we use this keyword in Java?

Ans: this keyword is a reference variable that refers to the current class object. It holds the reference to current class object or same class object.

There are six usages of Java this keyword. They are as follows:

1. this reference can be used to refer to the current class instance variable.
2. this keyword is used to call the non-static method of the current class.
3. this() can be used to invoke the current class constructor.

4. this keyword can be used as a parameter in the method call.
5. The keyword “this” can be used as a parameter in the constructor call.
6. It can also be used to return the object of the current class from the method.

31. Is it possible to use this() and super() both in the same constructor?

Ans: No, Java does not allow using both super() and this() together in same constructor. As per Java specification, super() or this() must be the first statement in a constructor.

32. What is the difference between super and this keywords?

Ans: Go to this tutorial: Difference between super and this keyword.

Hope that these OOPs interview questions in Java will help you to understand the level of questions asked by the interviewer. These are general OOPs interview questions that should be prepared by those who want to crack technical tests and interviews.

New Questions will be regularly updated. Please, share it on social networking sites for your friends.
All the best!!!