Top 30 OOP Concept Interview Questions Answers in Java - Object Oriented Programming

Java is an object-oriented programming language and you will see a lot of object-oriented programming concept questions on Java interviews. The classic questions like the difference between an interface and abstract class are always there but from the last couple of years more sophisticated questions based upon advanced design principles and patterns are also asked to check OOP knowledge of the candidate. Though, Object-oriented programming questions are more popular on Java interviews for 1 to 3 years experienced programmers. It makes sense as well, as these are the programmers who must know the OOP basics like Abstraction, Inheritance, Composition, Class, Object, Interface, Encapsulation, etc.

If you look for Java interview questions for 2 to 4 years experienced programmer, you will find lots of questions based upon OOP fundamentals like Inheritance and Encapsulation but as you gain more experience, you will see questions based on object-oriented analysis and design e.g. code a vending design machine or implement a coffeemaker in Java.

These questions are more difficult and require not only a true understanding of OOP fundamentals but also about SOLID design principles and patterns.



OOPS Concept Interview Questions in Java

In this article, I am going to share with you some OOPS concept based Java interview questions that I have collected from friends and colleagues and they have seen in various Java interviews on different companies. They are mostly asked at first a few rounds like on screening round or on the telephonic round.


If you are a senior Java developer then you already know answers to this question and I suggest you practice more on object-oriented analysis and design skill i.e. how to do code against a specification. If you are fresher and junior Java developer with 2 to 3 years experience then you must revise these questions, learn if you don't know to do well on your Java Job interviews.


What is method overloading in OOP or Java? (answer)
It's one of the oldest OOPS concept questions, I have seen it 10 years ago and still sees it now. When we have multiple methods with the same name but different functionality then it's called method overloading. For example. System.out.println() is overloaded as we have a 6 or 7 println() method each accepting a different type of parameter.


What is the method overriding in OOP or Java? (answer)
It's one of the magic of object-oriented programming where the method is chose based upon an object at runtime. In order for method overriding, we need Inheritance and Polymorphism, as we need a method with the same signature in both superclass and subclass. A call to such a method is resolved at runtime depending upon the actual object and not the type o variable. See the answer for a more detailed discussion.


What is the method of hiding in Java? (answer)
When you declare two static methods with same name and signature in both superclass 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 superclass is resolved to the static method declared in the super-class.


Is Java a pure object-oriented language? if not why? (answer)
Java is not a pure object-oriented programming language e.g. there are many things you can do without objects e.g. static methods. Also, primitive variables are not objects in Java. See the answer for a more detailed explanation.



What are the rules of method overloading and overriding in Java? (answer)
One of the most important rules of method overloading in Java is that the method signature should be different i.e. either the number of arguments or the type of arguments. Simply changing the return type of two methods will not result in overloading, instead, the compiler will throw an error. On the other hand, method overriding has more rules e.g. name and return type must be the same, method signature should also be the same, the overloaded method cannot throw a higher exception, etc. See the answer for a full list of rules related to method overloading and overriding in Java.


The difference between method overloading and overriding? (answer)
Several differences but the most important one is that method overloading is resolved at compile-time and method overriding is resolved at runtime. The compiler only used the class information for method overloading, but it needs to know the object to resolved overridden method calls. This diagram explains the difference quite well, though:

Object Oriented Programming Interview Questions and Answers


Can we overload a static method in Java? (answer)
Yes, you can overload a static method in Java. You can declare as many static methods of the same name as you wish provided all of them have different method signatures. See the answer for a more detailed explanation and code example.


Can we override the static method in Java? (answer)
No, you cannot override a static method because it's not bounded to an object. Instead, static methods belong to a class and resolved at compile time using the type of reference variable. But, Yes, you can declare the same static method in a subclass, that will result in method hiding i.e. if you use the reference variable of type subclass then new method will be called, but if you use the reference variable of superclass than old method will be called.


Can we prevent overriding a method without using the final modifier? (answer)
Yes, you can prevent the method overriding in Java without using the final modifier. In fact, there are several ways to accomplish it e.g. you can mark the method private or static, those cannot be overridden.


Can we override a private method in Java? (answer)
No, you cannot. Since the private method is only accessible and visible inside the class they are declared, it's not possible to override them in subclasses. Though, you can override them inside the inner class as they are accessible there.


What is the covariant method overriding in Java? (answer)
In the covariant method overriding, the overriding method can return the subclass of the object returned by the original or overridden method. This concept was introduced in Java 1.5 (Tiger) version and it's very helpful in case the original method is returning general type like Object class, because, then by using the covariant method overriding you can return a more suitable object and prevent client-side typecasting. One of the practical use of this concept is when you override the clone() method in Java.



Can we change the return type of method to subclass while overriding? (answer)
Yes, you can, but only from Java 5 onward. This feature is known as covariant method overriding and it was introduced in JDK 5 release. This is immensely helpful if the original method return super-class like clone() method return java.lang.Object. By using this, you can directly return the actual type, preventing client-side type-casting of the result.


Can we change the argument list of an overriding method? (answer)
No, you cannot. The argument list is part of the method signature and both overriding and overridden methods must have the same signature.


Can we override a method that throws runtime exception without throws clause? (answer)
Yes, there is no restriction on unchecked exceptions while overriding. On the other hand, in the case of checked exception, an overriding exception cannot throw a checked exception which comes higher in type hierarchy e.g. if the original method is throwing IOException than the overriding method cannot throw java.lang.Exception or java.lang.Throwable.


How do you call a superclass version of an overriding method in a subclass? (answer)
You can call a superclass version of an overriding method in the subclass by using super keyword. For example to call the toString() method from java.lang.Object class, you can call super.toString().


Can we override a non-static method as static in Java? (answer)
Yes, you can override the non-static method in Java, no problem on them but it should not be private or final :)


Can we override the final method in Java? (answer)
No, you cannot override a final method in Java, the final keyword with the method is to prevent method overriding. You use the final when you don't want subclass changing the logic of your method by overriding it due to security reasons. This is why the String class is final in Java. This concept is also used in the template design patterns where the template method is made final to prevent overriding.



Can we have a non-abstract method inside an interface? (answer)
From Java 8 onward you can have a non-abstract method inside interface, prior to that it was not allowed as all method was implicitly public abstract. From JDK 8, you can add static and default methods inside an interface.


What is the default method of Java 8? (answer)
The default method, also known as the extension method is new types of the method which you can add on the interface now. These method has implementation and intended to be used by default. By using this method, JDK 8 managed to provide common functionality related to lambda expression and stream API without breaking all the clients which implement their interfaces. If you look at Java 8 API documentation you will find several useful default methods on key Java interface like Iterator, Map, etc.


What is an abstract class in Java? (answer)
An abstract class is a class that is incomplete. You cannot create an instance of an abstract class in Java. They are provided to define default behavior and ensured that client of that class should adore to those contract which is defined inside the abstract class. In order to use it, you must extend and implement their abstract methods. BTW, in Java, a class can be abstract without specifying any abstract method.


What is an interface in Java? What is the real user of an interface? (answer)
Like an abstract class, the interface is also there to specify the contract of an API. It supports the OOP abstraction concept as it defines only abstract behavior. It will tell that your program will give output but how is left to implementors. The real use of the interface to define types to leverage Polymorphism. See the answer for a more detailed explanation and discussion.


The difference between Abstract class and interface? (answer)
In Java, the key difference is that abstract class can contain a non-abstract method but the interface cannot, but from Java 8 onward interface can also contain static and default methods that are non-abstract. See the answer for a more detailed discussion as I have described a lot of points there.

Java Object Oriented Programming questions for experienced


Can we make a class abstract without an abstract method? (answer)
Yes, just add abstract keyword on the class definition and your class will become abstract.


Can we make a class both final and abstract at the same time? (answer)
No, you cannot apply both final and abstract keyword at the class at the same time because they are exactly opposite of each other. A final class in Java cannot be extended and you cannot use an abstract class without extending and make it a concrete class. As per Java specification, the compiler will throw an error if you try to make a class abstract and final at the same time.


Can we overload or override the main method in Java? (answer)
No, since main() is a static method, you can only overload it, you cannot override it because the static method is resolved at compile time without needing object information hence we cannot override the main method in Java.



What is the difference between Polymorphism, Overloading, and Overriding? (answer)
This is a slight tricky OOP concept question because Polymorphism is the real concept behind on both Overloading and Overriding. Overloading is compiled time Polymorphism and Overriding are Runtime Polymorphism.


Can an interface extend more than one interface in Java?
Yes, an interface can extend more than one interface in Java, it's perfectly valid.


Can a class extend more than one class in Java?
No, a class can only extend another class because Java doesn't support multiple inheritances but yes, it can implement multiple interfaces.


What is the difference between abstraction and polymorphism in Java? (answer)
Abstraction generalizes the concept and Polymorphism allows you to use different implementation without changing your code. This diagram explains the abstraction quite well, though:

Java OOP Concepts Interview Questions and Answers


Object-Oriented design principle and pattern Interview Questions

Now let's see some OOPS concept questions based on the SOLID design principles and GOF design patterns that take advantage of the OOPS concept discussed here.

What problem is solved by the Strategy pattern in Java? (answer)
Strategy pattern allows you to introduce a new algorithm or new strategy without changing the code which uses that algorithm. For example, the Collections.sort() method which sorts the list of the object uses the Strategy pattern to compare objects. Since every object uses a different comparison strategy you can compare various objects differently without changing the sort method.


Which OOP concept Decorator design Pattern is based upon? (answer)
The decorator pattern takes advantage of Composition to provide new features without modifying the original class. A very good to-the-point question for the telephonic round. This is quite clear from the UML diagram of the Decorator pattern, as you can see the Component is associated with a Decorator.



When to use the Singleton design pattern in Java? (answer)
When you need just one instance of a class and want that to be globally available then you can use the Singleton pattern. It's not free of cost though because it increases the coupling between classes and makes them hard to test. This is one of the oldest design pattern questions from Java interviews. Please see the answer for a more detailed discussion.


What is the difference between State and Strategy Patterns? (answer)
Though the structure or class diagram of State and Strategy pattern is the same, their intent is completely different. The state pattern is used to do something specific depending upon state while Strategy allows you to switch between algorithms without changing the code which uses it.


What is the difference between Association, Aggregation, and Composition in OOP? (answer)
When an object is related to another object is called association. It has two forms, aggregation, and composition. the former is the loose form of association where the related object can survive individually while later is a stronger form of association where a related object cannot survive individually. For example, the city is an aggregation of people but is the composition of body parts.



What is the difference between Decorator, Proxy and Adapter pattern in Java? (answer)
Again they look similar because their structure or class diagram is very similar but their intent is quite different. The Decorator adds additional functionality without touching the class, Proxy provides access control and Adapter is used to make two incompatible interfaces work together. 


What is the 5 objects oriented design principle from SOLID? (answer)
SOLID is the term given by Uncle Bob in his classic book, the Clean Code, one of the must-read books for programmers. In SOLID each character stands for one design principle:
S for Single Responsibility Principle
O for Open closed design principle
L for Liskov substitution principle
I for Interface segregation principle
D for Dependency inversion principle

OOP Java Interview Questions for 2 years experienced


What is the difference between Composition and Inheritance in OOP? (answer)
This is another great OOPS concept question because it tests what matters, both of them are very important from a class design perspective. Though both Composition and Inheritance allows you to reuse code, formerly is more flexible than later. Composition allows the class to get an additional feature at runtime, but Inheritance is static. You can not change the feature at runtime by substitution of a new implementation. See the answer for a more detailed discussion.


That's all about in this list of object-oriented programming or OOPS concept interview questions. We have seen questions from various OOPS concepts like Abstraction, Encapsulation, Inheritance, Composition, Aggregation, Association, Class, Object, and Interface, etc.

We have also seen questions from Object-oriented design principles also known as SOLID principles and GOF design patterns like Strategy, State, and Factory, which are based upon both the object-oriented programming concept and OOP design principle.

But, as I said, if you are a senior Java developer then you focus more on object-oriented analysis and design and learn how to code against a requirement using all your OOP knowledge. You can also read Cracking the Coding Interview, 6th Edition to for more Object Oriented Programming questions.


Further Learning
SOLID Principles of Object-Oriented Design
Absolute Introduction to Object-Oriented Programming in Java
Java - Object-Oriented Programming [For Absolute Beginners]


Other Java programming and OOP tutorials You may like

Thanks for reading this article so far. If you like an object-oriented programming tutorial then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.

P. S. - If you are serious about learning object-oriented programming and looking for a free online course to start with then you can also check this FREE Object Oriented Programming (OOPs) for JAVA Interviews course on Udemy. It's completely free and you just need a free Udemy account to join this course. 

36 comments:

  1. Replies
    1. Thanks, glad to hear that you like this list of Java Object Oriented Programming question. Would you like to add any more on this list? let me know what you find missing and what kind of question you would like to see more, thanks

      Delete
  2. These are really basis Object Oriented Programming questions, anyone with just a couple of years experience and even a computer science graduates can can answer these question. I am looking for more challenging, tough and really difficult object oriented design questions which will test your knowledge and skill and not the definitions. For example how would you go about designing
    - an e-commerce site like Amazon or FlipKart
    - how does Google work? Can you implement solution like that
    - Design an electronic trading system
    - design a washing machine etc

    ReplyDelete
    Replies
    1. That would be less about Java and more about advanced programming algorithms, data structures, etc.

      Delete
    2. Regarding Vending machine design, you can check How to design Vending Machine in Java.

      Also, I have shared a lot of system design questions which is like the questions you have mentioned in the list, you can check them here

      Delete
  3. its very good effort. it help me a lot

    ReplyDelete
  4. Q: Can we override a non-static method as static in Java?

    A: No..compiler show error....

    ReplyDelete
    Replies
    1. right...non static to static and viceversa also not possible

      Delete
    2. But we can make it as final

      Delete
  5. It's very good efforts it's really help me lots

    ReplyDelete
    Replies
    1. Thanks @Boopendara, glad that you like this OOP concepts questions.

      Delete
  6. This comment has been removed by the author.

    ReplyDelete
  7. How they derived the concept of oop?
    pls.answer

    ReplyDelete
  8. I think, 1 answer is wrong. I'm pasting question along with answer as follows:

    Can we override a non-static method as static in Java? (answer)
    Yes, you can override the non-static method in Java, no problem on them but it should not be private or final :)

    ReplyDelete
    Replies
    1. no non-static methods can not be overridden as static method.
      The compiler giver below error msg :
      "This static method cannot hide the instance method from InheritanceTest"

      Delete
    2. Yes , You can not override non-static method as a Static.(JVM)

      Delete
    3. Yes guys, you are right, it's not possible to override an instance method as static method in Java. Thanks for pointing that out.

      Delete
  9. Can we override a non-static method as static in Java? (answer)

    Yes, you can override the non-static method in Java, no problem on them but it should not be private or final :)

    I think, the Answer given in this website(http://www.java67.com/) is wrong.
    Refer http://docs.oracle.com/javase/tutorial/java/IandI/override.html : Saying


    Superclass Instance Method Superclass Static Method
    Subclass Instance Method Overrides Generates a compile-time error
    Subclass Static Method Generates a compile-time error Hides

    ReplyDelete
    Replies
    1. Yes Ajay, you are right, it's not possible to override a non-static method as static in Java. Thanks for pointing that out.

      Delete
  10. Can we override a non-static method as static in Java?

    You answered it Yes. But it's not true. Both methods need to be either static or non-static. You can't remove staticness of a method in sub class.

    ReplyDelete
    Replies
    1. Yes, you are right, Java doesn't allow you to override a non-static or instance method as static, I'll further check and correct. Thanks for pointing that error guys. well done.

      Delete
  11. Can we change the argument list of an overriding method? (answer)
    No, you cannot. The argument list is part of the method signature and both overriding and overridden method must have the same signature.

    Can you please explain the answer what do you mean by argument list

    in overriding we can change the order of argument , number of argument then what is
    this argument list.

    ReplyDelete
  12. Overall good collection... but now a days in interview they ask programs...so u can add that type of questions...Once again thank you🤗

    ReplyDelete
  13. we can not override non static method as static

    ReplyDelete
    Replies
    1. Can we override a non-static method as static in Java? (answer)
      No, it throw an compile time error -
      This static method cannot hide the instance method from Super Class

      Delete
  14. Can we override a non-static method as static in Java? (answer)
    Yes, you can override the non-static method in Java, no problem on them but it should not be private or final :) (WRONG ANS)


    we can not override non static method as static .
    it'll give compilation error.

    ReplyDelete
  15. Thanks for the collection, veery helpful

    ReplyDelete
    Replies
    1. Glad that you find these OOP interview questions useful.

      Delete
  16. We can not override a static method in Java.

    ReplyDelete
  17. Can we override a non-static method as static in Java? (answer)
    Yes, you can override the non-static method in Java, no problem on them but it should not be private or final :)

    Either publish correct answer or remove it from the page.

    ReplyDelete
    Replies
    1. Hello Surendra, what is your point here? are you saying answer is wrong? Did you tried that on Eclipse or any other IDE? are you getting any error?

      Delete