OOPS concept interview questions

PS: If you like the page or have any questions, feel free to comment at end of the page.

OOPs stands for Object Oriented Programming. OOPs is a programming paradigm formulated around Objects which can hold attributes defining it behavior. The data of the objects can be modified by the methods. Concepts of OOPS include Abstraction, Polymorphism, Inheritance, Encapsulation.

The two building blocks of OOPs are Class and Object.

Class: can be defined as a collections of attributes and methods defining the behaviour of an object.

Object: Instance of a class having the values assigned to the properties of the class.Each object is said to be an instance of a particular class. Procedures in object-oriented programming are known as methods; variables are also known as fields, members, attributes, or properties.

Common terminology used in OOPs

  • Class variables - belong to the class as a whole; there is only one copy of each one.
  • Instance variables or attributes - data that belongs to individual objects; every object has its own copy of each one.
  • Member variables - refers to both the class and instance variables that are defined by a particular class
  • Class methods - belong to the class as a whole and have access only to class variables and inputs from the procedure call.
  • Instance methods - belong to individual objects, and have access to instance variables for the specific object they are called on, inputs, and class variables.

Q) What are different oops concept in java?


oops concept in java

The different OOps concepts are :

Q1) What is polymorphism?

Ans) The ability to identify a function to run is called Polymorphism. In java, c++ there are two types of polymorphism: compile time polymorphism (overloading) and runtime polymorphism (overriding).

Method overriding: Overriding occurs when a class method in a child class has the same name and signature as a method in the parent class. When you override methods, JVM determines the proper method to call at the program’s run time, not at the compile time.

Overloading: Overloading is determined at the compile time. It occurs when several methods have same names with:

  • Different method signature and different number or type of parameters.
  • Same method signature but the different number of parameters.
  • Same method signature and same number of parameters but of different type
Example of Overloading
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

Q2) What is inheritance?

Ans) Inheritance allows a Child class to inherit properties from its parent class. In Java this is achieved by using extends keyword. Only properties with access modifier public and protected can be accessed in child class.

public class Parent {
  public String parentName;  
  public String familyName;

  protected void printMyName() {
    System.out.println(“ My name is “+ chidName+” “ +familyName);
public class Child extends Parent {
  public String childName;
  public int childAge;
  protected void printMyName() {
    System.out.println(“ My child name is “+ chidName+” “ +familyName);

In a example above the child has inherited its family name from the parent class just by inheriting the class. When a child object is created the method printMyName() present in the child class is called.

Q3) What is multiple inheritance and does java support?

Ans) If a child class inherits the property from multiple classes is known as multiple inheritance. Java does not allow to extend multiple classes. The problem with the multiple inheritance is that if multiple parent classes have methods with same name, then at runtime it becomes difficult for the compiler to decide which method to execute from the child class. To overcome this problem java allows to implement multiple Interfaces. The problem is commonly referred as What is Diamond Problem.

Q) What is the difference between polymorphism and inheritance?

  • Inheritance defines parent-child relationship between two classes, polymorphism takes advantage of that relationship to add dynamic behavior in your code.
  • Inheritance encourages code reusability by allowing child class to inherit behavior from the parent class. On the other hand Polymorphism allows child to redefine already defined behaviour inside the parent class. Without Polymorphism it's not possible for a child to execute its own behaviour while represented by a Parent reference variable, but with Polymorphism it can be done.
  • Java doesn't allow multiple inheritance of classes, but allows multiple inheritance of Interface, which is actually required to implement Polymorphism. For example, a class can be Runnable, Comparator and Serializable at the same time because all three are interfaces. This makes them pass around in code e.g. you can pass an instance of this class to a method which accepts Serializable, or to Collections.sort() which accepts a Comparator.
  • Both Polymorphism and Inheritance allow Object oriented programs to evolve. For example, by using Inheritance you can define new user types in an Authentication System and by using Polymorphism you can take advantage of already written authentication code. Since, Inheritance guarantees minimum base class behaviour, a method depending upon super class or super interface can still accept an object of the base class and can authenticate it.

Q4) What is an abstraction?

Ans) Abstraction is a way of converting real world objects in terms of class. It's a concept of defining an idea in terms of classes or interface. For example creating a class Vehicle and injecting properties into it. E.g

public class Vehicle {
  public String colour;
  public String model;

Q5) What is Encapsulation?

Ans) The encapsulation is achieved by combining the methods and attribute into a class. The class acts like a container encapsulating the properties. The users are exposed mainly public methods.The idea behind is to hide how things work and just exposing the requests a user can do.

Q6) What is Association?

Ans) Association is a relationship where all object have their own lifecycle and there is no owner. Let's take an example of Teacher and Student. Multiple students can associate with a single teacher and single student can associate with multiple teachers but there is no ownership between the objects and both have their own lifecycle. Both can create and delete independently.

Q7) What is Aggregation?

Ans) Aggregation is a specialized form of Association where all objects have their own lifecycle but there is ownership and child object can not belongs to another parent object. Let's take an example of Department and teacher. A single teacher cannot belong to multiple departments, but if we delete the department teacher object will not destroy. We can think about "has-a" relationship.

Q8) What is Composition?

Ans) Composition is specialized form of Aggregation and we can call this as a "death" relationship. It is a strong type of Aggregation. Child object does not have their lifecycle and if parent object deletes all child object will also be deleted. Let's take again an example of a relationship between House and rooms. House can contain multiple rooms there is no independent life of room and any room can not belongs to two different house if we delete the house room will automatically delete.

Recommend Reading

  1. How java manages Memory?
  2. Why is it advised to use hashcode and equals together?
  3. Comparable and Comparator in java
  4. How to create Singleton class in Java?
  5. Difference between equals and ==?
  6. When to use abstract class over interface?
  7. Cap Theorem in Details
  8. Why is it recommended to use Immutable objects in Java