Top Java OOPs Interview Questions for 2021


This tutorial covers the most important Java OOP interview questions.

Table of Contents

1. What is OOPs?

OOPs is an Object-oriented programming system which is a programming technique that uses objects to define data and behavior. All functionalities and properties are accessed using the objects. This concept makes the code more flexible and increases readability.

2. What are the advantages of OOPs?

  • Reduces code redundancy
  • Improves code readability
  • Low development cost
  • Improved software quality
  • Faster product development
  • Reusability of code
  • Easy maintenance
  • Extensible which means we can easily add new features to the existing one.

3. What is the difference between procedural programming and OOP?

Procedural programmingObject oriented programming
Based on functionsBased on objects
More importance on sequence of functions executedImportance on states and behaviors of the object
Does not support encapsulationSupports encapsulation
Follows top-down approachFollows bottom-up approach
Less code reusabilityBetter code reusability
More complex to extend and modify the codeLess complex and easy to extend and modify the code

4. What are the characteristics of OOP?

  • Abstraction
  • Encapsulation
  • Inheritance
  • Association
  • Composition
  • Aggregation
  • Polymorphism

5. What is Abstraction?

Abstraction is one of the main OOPs concepts in Java. It mainly defines what kind of data needs to be visible and what needs to be hidden. In other words, it just provides the methods and not the internal functionality. For example, when we use a mobile phone, we just know its behavior but we don’t know the inner mechanism of how it works. We can use abstraction to make the application more secure. For example, when we log in to net banking and provide a username and password, the password is actually hidden.

We can use Abstraction for a class, data, or a method. To implement abstraction, we use interfaces and abstract classes.

6. What is Encapsulation?

Encapsulation is another OOPs concept in Java that binds the data and its method from external interference. This means it does not allow other classes to access or update these data or methods directly. One good example is the Java class itself which tightly binds the data and its functions within the class. To achieve this, we declare the variables or data as private variables which means we cannot access them outside the class. To access and update these data, we must use public get and set methods. Hence encapsulation helps to hide sensitive data from the user. It is also called data hiding.

One best example of encapsulation is a capsule itself (as the name suggests) inside which we have medicine that is not visible to us.

7. What is Polymorphism?

Polymorphism, as the name suggests, means having “multiple formsThis is another important OOPs concept in Java which is used commonly in developing many software applications where we can have different implementations of the same method.

Let us understand this feature of object-oriented programming in java with an example. For example, each bike type can have a different speed capacity. Slow speed bikes like TVS 50 speed capacity is slow when compared to medium speed bikes like Honda Access and this might be slow when compared to High-speed bikes like Pulsar. Hence even though all the 3 types of bikes have speed, the implementation differs from one another.

8. Define inheritance

Inheritance is a technique where the child class or subclass can directly inherit all the properties and behaviors of the parent class or superclass.

9. What is multiple inheritance? Does Java support multiple inheritance?

Multiple inheritance is the process in which the child class inherits the properties and behaviors of multiple parent classes. No, Java does not support multiple inheritace.

10. Why Java does not support multiple inheritance?

Multiple inheritance results in a diamond problem in Java. This means, if a child class inherits 2 parent classes, there will ambiguity when the same method is present in both classes. Hence the child class will not know which parent class method to call.

11. What is static binding and dynamic binding?

Static binding resolves during compile time which means the compiler knows which method to invoke during compile time. Example: Method overloading.

Dynamic binding resolves during runtime which means it knows only during execution which method to invoke based on the object created. Example: Method overriding.

12. What is Association?

Association in java describes the relationship between two classes. It establishes relationships through their objects. An association may represent one-to-one, one-to-many, many-to-one, or many-to-many relationships. It enables the HAS-A relation between the classes.

Examples:

  • A person can have only 1 driving license. This shows a one-to-one relation.
  • A person can have multiple bank accounts. This is a one-to-many relation.
  • Many employees can have a single reporting manager. This depicts a many-to-one relation.
  • Many students can have many teachers. This shows a many-to-many relation.

13. What is Aggregation?

Aggregation is also another form of Association that implements a HAS-A relationship and it supports only a unidirectional relation. This means one class has a reference to another class. For example, we can say that a person has an address. The reverse way i.e address has a person is not meaningful. This is why we say that it is a one-way relationship that is another form of association.

14. Define composition

Composition denotes a belong-to or part-of type of association. In other words, we can say, one object contains or is composed of another object.  This implements a strong type of association which means that if the inner object cannot exist if the main outer object is destroyed.

For example, we cannot have a school without students. This means a school cannot exist without students in it. Hence it establishes a composition between school and students.

15. Define a class and object

A class is a template that groups different methods and properties in a single unit.

An object is an instance of a class that represents various states and behaviors within a class. A single class can have multiple objects.

16. Is it possible to call a base class method without creating a class instance?

Yes, it is possible by using a static method or when the base class is inherited by some other subclass

17. What are the different types of inheritances?

  • Single inheritance
  • Multilevel inheritance
  • Hierarchical inheritance
  • Hybrid inheritance
  • Multiple inheritance – Java does not support

18. What is hybrid inheritance?

Hybrid inheritance is the combination of different types of inheritance like multilevel and hierarchical.

 19. Define superclass and subclass.

Superclass is also known as a parent class from which other classes can inherit the properties and methods. Eg: Shape is the base class of square.

A subclass is also known as a child class that inherits the properties and methods from the parent class. Eg: Bike is a subclass of Vehicle.

20. Differentiate between overloading and overriding

Method overloadingMethod overriding
This implements static polymorphismThis implements dynamic polymorphism
This happens during compile timeThis happens during runtime
Same methods are present in the same classSame methods are present in different classes

21. What is an interface?

An interface is an OOP concept that allows only to declare a function without any implementation. It is the responsibility of the class that implements the interface to implement the functionality of the interface method. This allows different classes to provide different implementations for the same interface method based on the requirement.

22. What is a constructor?

A constructor is a special method that has the same name as the class name. A class can have multiple constructors with a different number of parameters. The main use of a constructor is to initialize objects and values.

23. What are the different types of constructors in Java?

  • Default constructor
  • Parameterized constructor
  • No-arg constructor

24. What is the use of finalize?

Finalize is a special object method that is used to release all resources and performs memory management.

25. What is the difference between an abstract class and an interface?

Abstract classInterface
Abstract class can have both abstract and non-abstract methodsInterface can have only abstract methods. From Java 8, supports default methods
Does not support multiple inheritanceSupports multiple inheritance
Uses abstract keywordUses interface keyword
Uses extends keyword to inherit the abstract classUses implements keyword to implement the interface
It can extend another Java class and also implement interfaceIt can only extend another interface
Members can have access modifiers like private, protected, etcMembers can have only public
It can have static, non-static, final or non-final variables.It can have only static and final variables

26. Define try-catch block and finally block.

A try-catch and finally block is used to implement exception handling. The code that has the possibility throw exception is defined within the try block and the exceptions are caught in the corresponding catch block.

Finally block ensures that the statements within this block executes always irrespective of whether an exception occurs or not.

27. What is method overloading?

When there are multiple methods with the same name but different implementations, we call it a method overloading. We can implement method overloading in two different ways:

  • Different number of parameters
  • Different types of parameters.

28. Can we change the argument list of an overriding method?

No, we cannot since the overridden method should have the same argument list as the overriding method.

29. How to invoke the superclass version of the overriding method in a subclass?

We can use the super keyword to invoke the superclass version of the overriding method in a subclass.

30. Can we override a final method in Java?

No we cannot override a final method since we cannot change the logical implementations if a method is declared as final. The main idea of final method is to prevent modification.

31. Can we declare non-abstract methods inside an interface?

Yes, from Java 8 we can declare non-abstract methods, static and default methods inside an interface. Prior to Java 8, it does not allow non-abstract methods.

32. Can we overload or override the main method?

No we cannot override the main method since it is static. However, we can overload it.

33. How to achieve multiple inheritance in Java?

Java does not directly support multiple inheritance due to ambiguity and complexity of extending multiple classes. However, we can achieve this using interfaces since a single class can implement multiple interfaces at the same time. This resolves ambiguity since it is the responsibility of the implementing class to provide the implementation of the interface methods.

34. What are access modifiers?

Access modifiers define the scope or accessibility of the variables or methods of a class.  Below are the different types of access modifiers:

  • private
  • protected
  • public
  • default

35. What are the differences between static and dynamic binding?

Static BindingDynamic Binding
Also called early binding or compile-time bindingAlso called late binding or runtime binding
Method overloading is static bindingMethod overriding is dynamic binding
Uses type of class to resolve the bindingUses type of object to resolve binding
Faster executionSlow execution

36. Which OOPs concept exposes only necessary information to the calling function?

Encapsulation – implements data hiding by exposing only necessary information to the calling function.

37. Does Java support Operator overloading?

No, Java does not support operator overloading.

38. How can we achieve data abstraction?

We can achieve data abstraction by using:

  • Abstract class
  • Abstract method

39. Define exceptional handling.

Excpetional handling is a mechinsm that allows to detect and handle exceptions so that it does not abruplty interrupt the execution.

40. What are the 5 design principles for an object-oriented approach?

  • S – Single Responsibility principle
  • O – Open-closed design principle
  • L – Liskov substitution principle
  • I – Interface Segragation principle
  • D – Dependency inversion principle