Java is an object-oriented programming language and you will see a lot of object-oriented programming concept questions in 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 the 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 like designing and coding a vending 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.
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 like designing and coding a vending 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.
30 OOPS Interview Questions in Java with Answers
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 the answers to this question and I suggest you practice more on object-oriented analysis and design skills i.e. how to do code against a specification. If you are a fresher and junior Java developer with 2 to 3 years of experience then you must revise these questions, learn if you don't know to do well on your Java Job interviews.
1. 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 see 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.
2. What is the method overriding in OOP or Java? (answer)
It's one of the magic of object-oriented programming where the method is chosen 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.
3. What is the method of hiding in Java? (answer)
When you declare two static methods with the 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.
4. 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.
5. 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.
It's one of the oldest OOPS concept questions, I have seen it 10 years ago and still see 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.
2. What is the method overriding in OOP or Java? (answer)
It's one of the magic of object-oriented programming where the method is chosen 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.
3. What is the method of hiding in Java? (answer)
When you declare two static methods with the 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.
4. 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.
5. 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.
6. 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:
7. 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.
8. 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 are resolved at compile time using the type of reference variable. But, Yes, you can declare the same static method in a subclass, which will result in method hiding i.e. if you use the reference variable of type subclass then the new method will be called, but if you use the reference variable of superclass then the old method will be called.
9. 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 as private or static, those cannot be overridden.
10. 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.
11. 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 a 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.
12. 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 the clone() method returns a java.lang.Object. By using this, you can directly return the actual type, preventing client-side type-casting of the result.
13. 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.
14. 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 a 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 then the overriding method cannot throw java.lang.Exception or java. lang.Throwable.
15. 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 the super keyword. For example to call the toString() method from java.lang.Object class, you can call super.toString().
16. 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 :)
17. 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.
6. 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:
7. 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.
8. 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 are resolved at compile time using the type of reference variable. But, Yes, you can declare the same static method in a subclass, which will result in method hiding i.e. if you use the reference variable of type subclass then the new method will be called, but if you use the reference variable of superclass then the old method will be called.
9. 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 as private or static, those cannot be overridden.
10. 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.
11. 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 a 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.
12. 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 the clone() method returns a java.lang.Object. By using this, you can directly return the actual type, preventing client-side type-casting of the result.
13. 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.
14. 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 a 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 then the overriding method cannot throw java.lang.Exception or java. lang.Throwable.
15. 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 the super keyword. For example to call the toString() method from java.lang.Object class, you can call super.toString().
16. 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 :)
17. 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.
18. Can we have a non-abstract method inside an interface? (answer)
From Java 8 onward you can have a non-abstract method inside the 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.
19. What is the default method of Java 8? (answer)
The default method, also known as the extension method is a new type of method which you can add to the interface now. These methods have implementation and are 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 who implement their interfaces. If you look at Java 8 API documentation you will find several useful default methods on key Java interfaces like Iterator, Map, etc.
20. 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.
21. What is an interface in Java? What is the real use of an interface? (answer)
As 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 implementers. The real use of the interface to define types to leverage Polymorphism. See the answer for a more detailed explanation and discussion.
22. The difference between Abstract class and interface? (answer)
In Java, the key difference is that the 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.
23. 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.
24. Can we make a class both final and abstract at the same time? (answer)
No, you cannot apply both final and abstract keywords to the class at the same time because they are exactly the opposite of each other. A final class in Java cannot be extended and you cannot use an abstract class without extending and making 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.
25. 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.
26. What is the difference between Polymorphism, Overloading, and Overriding? (answer)
This is a slightly tricky OOP concept question because Polymorphism is the real concept behind both Overloading and Overriding. Overloading is compiled time Polymorphism and Overriding is Runtime Polymorphism.
27. 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.
28. 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.
29. What is the difference between abstraction and polymorphism in Java? (answer)
Abstraction generalizes the concept and Polymorphism allows you to use different implementations without changing your code. This diagram explains the abstraction quite well, though:
8 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.
1. 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.
2. 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.
3. 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.
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.
4. What is the difference between State and Strategy Patterns? (answer)
Though the structure or class diagram of the State and Strategy pattern is the same, their intent is completely different. The state pattern is used to do something specific depending upon the state while Strategy allows you to switch between algorithms without changing the code which uses it.
5. 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.
6. What is the difference between Decorator, Proxy, and Adapter patterns 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 an Adapter is used to make two incompatible interfaces work together.
7. 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
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 allow 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, 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 for more Object Oriented Programming questions.
Other Java programming and OOP tutorials You may like
- My favorite courses to learn object-oriented programming
- 5 differences between Hashtable and HashMap in Java
- 5 Free Object-Oriented programming courses for beginners
- What is a factory method design pattern in Java
- Top 5 Courses to Learn Design Patterns in Java
- 5 Free Data Structure and Algorithms Courses
- Difference between HashMap and ConcurrentHashMap in Java
- 7 best OOP design pattern courses for Java developers
- 10 Java Coding Interview Questions and Answers for Java beginners.
- 5 Free Spring Framework Courses for Java Developers
- Difference between TreeSet and HashSet in Java
- 10 Free Courses to learn Java for Beginners and Experienced Programmers
- 15 Spring Boot Interview Questions with Answers
- 15 Microservice Interview Questions with Answers
- 10 Microservice Design Patterns and Principles with Examples
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 the JAVA Interviews course on Udemy. It's completely free and you just need a free Udemy account to join this course.
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
ReplyDelete- 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
That would be less about Java and more about advanced programming algorithms, data structures, etc.
DeleteRegarding Vending machine design, you can check How to design Vending Machine in Java.
DeleteAlso, 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
Very nice collection!
ReplyDeletegood blog
ReplyDeleteits very good effort. it help me a lot
ReplyDeleteQ: Can we override a non-static method as static in Java?
ReplyDeleteA: No..compiler show error....
right...non static to static and viceversa also not possible
DeleteBut we can make it as final
DeleteJust think like that:- static means compile time and overriding is run time polymorphism. so you cannot merge them. or you can learn as, "when calling of a method is decide at compile time know as overloading or static polymorphism" same as "when calling of a method decide at run time we call it run time polymorphism or dynamic polymorphism or overriding". Hope this helps
DeleteWell said, thanks for adding value.
DeleteIt's very good efforts it's really help me lots
ReplyDeleteThanks @Boopendara, glad that you like this OOP concepts questions.
Deletethanks, Great Collection
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHow they derived the concept of oop?
ReplyDeletepls.answer
I think, 1 answer is wrong. I'm pasting question along with answer as follows:
ReplyDeleteCan 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 :)
no non-static methods can not be overridden as static method.
DeleteThe compiler giver below error msg :
"This static method cannot hide the instance method from InheritanceTest"
Yes , You can not override non-static method as a Static.(JVM)
DeleteYes guys, you are right, it's not possible to override an instance method as static method in Java. Thanks for pointing that out.
Deletenice
ReplyDeleteCan we override a non-static method as static in Java? (answer)
ReplyDeleteYes, 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
Yes Ajay, you are right, it's not possible to override a non-static method as static in Java. Thanks for pointing that out.
DeleteCan we override a non-static method as static in Java?
ReplyDeleteYou 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.
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.
DeleteCan we change the argument list of an overriding method? (answer)
ReplyDeleteNo, 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.
Overall good collection... but now a days in interview they ask programs...so u can add that type of questions...Once again thank you🤗
ReplyDeletewe can not override non static method as static
ReplyDeleteCan we override a non-static method as static in Java? (answer)
DeleteNo, it throw an compile time error -
This static method cannot hide the instance method from Super Class
Can we override a non-static method as static in Java? (answer)
ReplyDeleteYes, 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.
Good Catch :)
DeleteThanks for the collection, veery helpful
ReplyDeleteGlad that you find these OOP interview questions useful.
DeleteWe can not override a static method in Java.
ReplyDeleteCan we override a non-static method as static in Java? (answer)
ReplyDeleteYes, 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.
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?
DeleteCan we override a non-static method as static in Java?
ReplyDeleteNo we can't it will compilation error
good, that's correct.
Deleteit will be great if you write the answer to the point rather than writing some story, thats not cool and time waste click and opening each answer in different page ! NOT COOL
ReplyDeletenice article
ReplyDelete