Top 40 Object Oriented Programming [OOP] Interview Questions Answers in Java

OOP Interview questions or Object-oriented programming and design interview question are an integral part of any programming job interviews like Java, C++, or C#. Since Java is an Object-oriented programming language, it's expected from Java developers that he is good at Object-oriented analysis and design and familiar with essential OOP concepts like Abstraction, Encapsulation, and Polymorphism. Object-Oriented Programming Interview question in Java is mainly based around fundamental OOP concept and how those are implemented in Java, like Abstraction OOP concept is implemented using an interface and abstract class, Encapsulation is using private keyword, etc.


Question from OOP or object-oriented programming is also asked as part of Java design pattern question on Senior level Java interview. On Freshers and Beginners level interview, OOP Interview questions are mostly based on fundamentals only with some tricky Java questions like why Java doesn't support multiple inheritances, etc.

In this Java article, we will see some frequently asked OOS interview questions and answers. Some questions are very fundamental and some Object-oriented programming and design questions are difficult to answer but this mix helps to learn more. If you love to learn more about the OOP design principle, read 10 OOP design principles for Java programmers.

This article is divided into two sections. the first section contains interview questions from Object-Oriented Programming concepts and fundamentals, and in the second section, you will see some frequently asked OOP interview questions from Programming Job interviews that focus on Java programming language as well as some questions on Object-Oriented design patterns, and principles. 




40+ OOP Interview Questions Answers in Java

In this section, we will see interview questions from four OOP concepts like Abstraction, Encapsulation, Inheritance, and Polymorphism, and two main actors of object-oriented programming Class and Object.


Question 1. What is an abstraction in Java?
Abstraction is an Object-Oriented programming technique to generalize things, by introducing a better level of abstraction, you introduce flexibility, reuse, and better maintenance capability in your program. 

By the way, Abstraction is everywhere, for example, if you need to write a program that writes output into a file then you create a method writeToFile(), this is the lowest level of abstraction as it's very specific and no other part of code can use it.

This is OK if your application scope is limited, but suppose you may need to write to console, file, database, or a TCP socket then you might want to create an abstraction write() defined in an interface and later more specific implementation of that interface.

By introducing a higher level of abstraction, you can make other modules of code depend on this abstraction rather than specific, which means easier to introduce new targets or modify existing targets. Overall flexibility improves. 

By the way, if you are new to Object-Oriented programming then I also suggest you join a course like Java Object-Oriented Programming: OOP, OOAD, & Design Patterns to learn essential OOP concepts and patterns. This will not only help you in interviews but also in your day-to-day coding life as a programmer. 

Object Oriented Programming [OOP]  Interview Questions Answers in Java





Question 2. What is Inheritance in Java?
Inheritance is another Object-Oriented design technique, which allows code reuse. By using Inheritance, you can define functionality for several classes in one place, superclass, which is easier to change and maintain. Inheritance also helps to create a type hierarchy, which is key for writing flexible and highly maintainable software.

Without type hierarchy, it's impossible to achieve runtime Polymorphism, which is also made possible because of Inheritance. In Java, Inheritance is either extending a class or implementing an interface.

When you extend a class, you not only inherit type but also the behavior of a class, but when you inherit interface, you only inherit type. Though inheritance is good for code reuse, it's not the only option and sometimes it even leads to inflexibility. In a later section, we will read about Composition, which is an alternative of extending behavior and providing the new capability to classes.


Question 3. What is Encapsulation or data hiding in Java?
Encapsulation in Java is another fundamental OOP concept, which guards application against frequent change. By encapsulating, something which is variable and quite often changes, an object-oriented programmer limit impact of any future change in the overall system.

Since software spends more time in development than maintenance, a properly encapsulated design lets you incorporate future changes more easily, without impacting all parts of your application which is already tested and working. By limiting the impact of a change, you not only reduce testing effort but also reduce delivery time and overall quality.

That's the reason Encapsulation is one of the critical Object-oriented programming concepts to master. Several design patterns like Decorator and Factory pattern are based upon Encapsulation to provide a loosely coupled and highly cohesive solution. In order to learn about syntactical details and how Java programming language supports Encapsulation, see What is Encapsulation in Java.


Question 4. What is Polymorphism in Java?
Polymorphism is also one of the top four Object-Oriented programming and design concepts. In fact, this is the one that gives you dynamism and the ability to change something in runtime. By default, all instance methods are virtual in Java, which means by using Polymorphism you can replace an object which implements a certain interface, with another.

This means calling an instance method, which is not private or final is also resolved at runtime. This is known as method overriding in Java. Polymorphism allows one type of object to represent different types of classes in the same type of hierarchy. You can refer to what is Polymorphism in Java, method overloading, or overriding for further details in the context of the Java programming language.


Question 5. What is Interface in Java?
the interface is a way to define abstraction in Java. The interface defines a public contract between the client and the Service provider. By using an interface, you decouple "What" and "How", the interface only provides information about what or which services are provided by a class, and leaves how to implement those up to a class. 

The use of interface can lead to a loosely coupled design because the client doesn't need to know who is servicing them under the hood. In fact, programming for interface than implementation is one of the important Object-oriented design principles in object-oriented software development. To learn more about the specifics of Java interfaces, see what is Interface in Java.


Question 6. What is Class in Java?
Object-Oriented programming is all about Class and Object. A Class is an Organizational unit, which is used to represent real-world entities, which have states and behavior. For example in an Object-oriented program, a Car is a class having a state like a number of gears, seats, state e.g. running or stopped, etc.

You can also look, A class as a user-defined data type, so if want to represent a Student with name, age, enrolled courses, etc, you can create your own Student class. 

Further, you can provide methods like the enroll() to enroll in a course, which can be another class, containing details about individual courselike name, duration, fees, etc. In most simple words, view Class as a user-defined type to represent real-world entities. To learn about the specifics of the class in Java, see this post.


Question 7. What is Object in Java?
An Object is an instance of a class, having a certain state at that point. Class defines things, and Object does things. In other words class is blueprints of buildings and Objects are actual buildings, created by following those blueprints.

I have discussed a simple example of how Class and object work together here, which is worth reading to get a perspective of both Class and Object. In Java, an object is created when your program starts execution, commonly using new keywords but there are other ways to do that as well.

You can even use the Factory method pattern to create objects, a better and more object-oriented way of creating objects. Also, you can refer to this post to know the specific object in the context of the Java language itself.


8. What is the difference between abstract class and interface in Java? and When do you use abstract class over the interface in Java?
This is a really good Object-oriented programming and design question, which I not only seen in Java interviews for both junior and experienced programmers but also tend to ask quite often. It's not about syntactical differences like a class can implement multiple interfaces but only extend a class, but about the impact of those limitations in actual design and how it affects you in the maintenance phase.

If you can whip up some good examples both from your domain or general domain to back up your concept, you will likely score better than other candidates. Since this question demands a separate post in itself, I have written and explained it here in my recent post with the same timer.

Please see When to use abstract class and interface in Java, and let us know your feedback. You can even share your experience, in case you have asked the same question before.


9. You have an interface which 100's of implementation, Now you need to add one method into that interface, How will you handle this situation with minimal impact.
Create another interface with just one more interface extending the original interface.


10. How do you design something open for extension but close for modification?
This question is asked by one of my readers in my post 10 abstract class and interfaces interview Question in Java. I really liked this one, and thus included it here for better exposure.

Since the interface is effectively Immutable once published, you can approach this problem in two ways, first, extend the previous interface and create a new one, or create a separate interface and let the class which needs this new method implement a new method.

In both approaches, only classes that need a new method will be affected, but in one case it just implements one interface, while in other it implements two interfaces. You can also take a look at my original comment and few others made by intelligent readers.


11. Difference between method overriding and overloading in Java
One of the most popular Objects Oriented programming question, almost always appear in Java based roles. Well, method overriding is a case of declaring an identical method at child class and calling that method using Parent class's object.

In a method overriding a method, the call is resolved at runtime, based upon the actual object i.e. if the object is of parent class then the parent's method is called and if the object is of the child class then the child's method is called. 

On the other hand, overloading is declaring a method in the same class, but with a different method signature like one method accepting integer, while the other accepting String. System.out.println() is a prime example, which is overloaded to accept different types of arguments. I would also suggest reading Method Overloading vs Overriding in Java, for knowing the rules of overloading and overriding, and corner cases associated with them.


12. Difference between static and dynamic binding in Java
This is usually a follow-up question or previous Object-oriented programming question. Static binding and dynamic binding are mostly used to find out which method to call in response to a method call on an object like object.method()

In the case of static binding, the method is found and resolved during compile time, while in the case of dynamic binding, the method is resolved at runtime, based upon an actually available object. 

Dynamic binding along with Polymorphism gives a lot of flexibility in object-oriented design, where objects at runtime drive execution flow. Private, static, and final methods are resolved using static binding in Java, as they can not be overridden. You can also see my post on Static and Dynamic binding in Java, more explanation, and detailed code example.


13. What is the difference between the IS-A relationship and HAS-A in Java?
IS-A and HAS-A are common ways to denote Inheritance and Composition in Java. The main difference between an IS-A relationship and a HAS-A relationship is that in the case of the former, the related object becomes part of the type hierarchy, while in the latter case composed object is not part of the type hierarchy. 

For example, Mango IS-A Fruit, which can be denoted in Java by Mango extending Fruit class and becoming part of the Fruit type hierarchy. This gives Mango the flexibility to stand in place of Fruit. On the other hand Car HAS-A Engine, now Engine is not a Car and not part of Car's type hierarchy, but give you the flexibility to replace Engine object with better-performing Engine implementation at runtime.


14. Difference between Composition and Inheritance in Java
Both Composition and Inheritance are used to provide new functionality to a class. Both are also a good way to achieve code reusability in software development. Inheritance allows direct reuse by extending a class and becoming part of their type hierarchy, while Composition uses the principle of delegation to reuse already tested code. 

The main difference between Inheritance and Composition is that the former is static, while the latter is dynamic. You can not change inherited behavior at runtime, all which is available at compile time in the form of code in your sub-class will be used at runtime. 

On the other hand, if you are using Composition, you can replace composing objects with another better performer, which implements the same interface. This runtime flexibility gives Composition an edge over Inheritance, and that's why it's advised to prefer Composition over Inheritance in Java. I would also recommend reading the corresponding item in the all-time classic Effective Java by Joshua Bloch.


15. Why Java doesn't support Multiple Inheritance in Java?
When we talk about multiple inheritances in Java, we mostly talk about one class extending more than one class in Java, which is syntactically not possible. Though Java supports multiple type inheritance, by allowing a Class to implement multiple inheritances to become part of different type-hierarchy. 

The main reason for Java not supporting multiple inheritances is choice, yes it's their designer's choice to keep it simple. By the way, this article shares few more reasons why Java doesn't support multiple inheritances, you may find them more helpful.



16. How do you design the Vending Machine in Java?
This is a complete object-oriented design exercise, quite popular at various computer programming courses and various programming job interviews as well. I have written a separate set of posts to discuss this question in detail, which covers how to approach, design, and write test cases for such a problem. 

You can try it by yourself and if you get stuck then you can use my solution for some hints, I highly recommend you to try this question yourself, that's the only way to develop the thinking required to solve real-world problems using object-oriented programming techniques. 

And, if you need more such exercises for practice then you can also see Grokking the Object-Oriented Design Interview course on Educative. This is a text-based interactive course where you will find many object-oriented case studies like how to design a parking lot, car rental system, ATM, the game of chess, and even StackOverflow.  


And, if you find the Educative platform and their Grokking courses like Grokking the System Design Interview and this one then consider getting an Educative Subscription which provides access to their 250+ courses for just $14.99 per month. It's very cost-effective and great for preparing for coding interviews. 




OOP Questions and Answers from Java Interviews

This is the second section of this article and in this section, we'll see frequently asked Object-Oriented programming and design questions from Java interviewers. I have already discussed most of these questions in detail in separate articles, hence, instead of providing answers, I am linking to them. 

This servers two purposes, first you can try to answer the question without looking at answers, and later you can see the answer for more detailed learning. In some questions, I have also given hints and short answers for people who are in hurry and cannot read detailed articles.


17. Difference between Class and Object in OOP?
This is often the first question on interviews and used to weed out people who know nothing about object-oriented programming. A class is a blueprint to create objects. Objects are instances of classes with state, I mean specific values for instance variables. 


18. Difference between Abstract Class and Interface in Java? (answer)
An abstract class is a class with (usually) one or more unimplemented methods and perhaps member variables. Classes or abstract classes may extend only one other class or abstract class. 

An interface has no implementation (although this has changed in Java 8) and no member variables. Classes or interfaces may inherit/implement multiple interfaces. Internally, methods dispatch faster for a class variable than an interface variable.


19. Can you explain Liskov Substitution Principle? (answer)


20. What are SOLID Object-oriented principles? (answer)


21. Difference between private, protected, and public modifiers in Java? (answer)


22. What is constructor chaining? (answer)


23. Difference between pass by value and pass by reference? (answer)


24. Difference between abstraction and encapsulation? (answer)


25. Difference between association, composition, and aggregation? (answer)


26. Difference between generalization and specialization? (answer)


27. Can you explain Open Closed Design Principle? (answer)


28. What is the Single Responsibility Principle? What are the consequences of violating it? (answer)


29. What is an Observer design pattern? When should you use it? (answer)


30. What is the difference between hiding and shadowing in OOP? (answer)


31. Can you override a static method in Java? (answer)


32. What is the difference between state and strategy design patterns? (answer)


33. Why might you want to declare a class variable as an interface vs a class? (answer)
While answering this question try to go deep. This can really demonstrate how well they truly understand Java and OOP.


34. What is the difference between a class and an instance? (answer)


35. Difference between Coupling and cohesion? (answer)


36. Difference between Data hiding and Encapsulation? (answer)


37.  What is the difference between Factory and Abstract Factory design patterns? (answer)


38. What is the difference between Dependency injection and Factory Pattern? (answer)


39. When would you want to write an abstract class vs an interface?
You would write an abstract class when you want to provide one or more base method implementations. Some feel that abstract classes should be used for the Is-A relationship and interfaces should be used for the Kind-Of relationship.

However, in most cases, interfaces are the better choice (regardless of the Is-A/Kind-Of relationship), if you think that more than one implementation will be required. Composition is usually preferred over inheritance.


40. When would you want a method argument to take a class vs an interface?
One should prefer an interface whenever possible. A class argument may be used if only one implementation is ever available and no interface exists, yet.


That's all on this list of Object-Oriented Programming and Design Interview Questions. To be frank, most of the questions focus on the OOP concept and fundamentals, rather than actually making you design something object-oriented, but this is how those questions are asked in the Interview. 

I highly recommend you to try and actually design problems like Vending Machine, OrderBook, ATM Machine, etc to get hold of all of them together. We will discuss a couple of actual design exercises as a whole for sure. At the same time, you can use this as a refresher for your object-oriented fundamentals before going to any job interview.

Further Learning


Other Interview Questions you may like to Prepare 
  • 40+ Object-Oriented Interview Questions with Answers (questions)
  • 15 Spring Data JPA Interview Questions (list)
  • 35 Python Interview Questions for Beginners (python questions)
  • 50+ SQL and Database Phone Interview questions (SQL questions)
  • 130+ Java Interview Questions with Answers (list)
  • 17 Spring AOP Interview Questions with Answers (list)
  • 20+ JUnit Interview Questions for Java developers (questions)
  • 10 Dynamic Programming PRoblems for Coding interviews (questions)
  • 25 Spring Security Interview Questions with Answers (questions)
  • 5 Best Courses to learn Java Programming  (best courses)
  • 20 Spring MVC Interview Questions with answers (spring questions)
Thanks for reading this article so far. If you like these OOP Interview questions then please share them with your friends and colleagues. If you have any questions or feedback then please drop a note. 

No comments :

Post a Comment