In the event you search for Java interview questions for two to 4 years skilled programmer, one can find numerous questions primarily based upon OOP fundamentals like Inheritance and Encapsulation however as you achieve extra expertise, you will notice questions primarily based on object-oriented evaluation and design like designing and coding a merchandising machine or implement a coffeemaker in Java.
These questions are tougher and require not solely a real understanding of OOP fundamentals but additionally about SOLID design rules and patterns.
30 OOPS Interview Questions in Java with Solutions
On this article, I’m going to share with you some OOPS concept-based Java interview questions that I’ve collected from associates and colleagues they usually have seen in varied Java interviews on completely different firms. They’re principally requested at first a couple of rounds like on screening spherical or on the telephonic spherical.
If you’re a senior Java developer then you definitely already know the solutions to this query and I counsel you apply extra on object-oriented evaluation and design abilities i.e. methods to do code towards a specification. If you’re a more energizing and junior Java developer with 2 to three years of expertise then you could revise these questions, study if you do not know to do effectively in your Java Job interviews.
It is one of many oldest OOPS idea questions, I’ve seen it 10 years in the past and nonetheless see it now. When we now have a number of strategies with the identical title however completely different performance then it is known as methodology overloading. For instance. System.out.println() is overloaded as we now have a 6 or 7 println() methodology every accepting a special sort of parameter.
2. What’s the methodology overriding in OOP or Java? (reply)
It is one of many magic of object-oriented programming the place the tactic is chosen primarily based upon an object at runtime. To ensure that methodology overriding, we’d like Inheritance and Polymorphism, as we’d like a way with the identical signature in each superclass and subclass. A name to such a way is resolved at runtime relying upon the precise object and never the sort o variable. See the reply for a extra detailed dialogue.
3. What’s the methodology of hiding in Java? (reply)
While you declare two static strategies with the identical title and signature in each superclass and subclass then they cover one another i.e. a name to the tactic within the subclass will name the static methodology declared in that class and a name to the identical methodology is superclass is resolved to the static methodology declared within the super-class.
4. Is Java a pure object-oriented language? if not why? (reply)
Java will not be a pure object-oriented programming language e.g. there are a lot of issues you are able to do with out objects e.g. static strategies. Additionally, primitive variables should not objects in Java. See the reply for a extra detailed clarification.
5. What are the principles of methodology overloading and overriding in Java? (reply)
One of the crucial essential guidelines of methodology overloading in Java is that the tactic signature ought to be completely different i.e. both the variety of arguments or the kind of arguments. Merely altering the return sort of two strategies is not going to end in overloading, as a substitute, the compiler will throw an error.
6. The distinction between methodology overloading and overriding? (reply)
A number of variations however a very powerful one is that methodology overloading is resolved at compile-time and methodology overriding is resolved at runtime. The compiler solely used the category info for methodology overloading, however it must know the thing to resolved overridden methodology calls. This diagram explains the distinction fairly effectively, although:
7. Can we overload a static methodology in Java? (reply)
Sure, you possibly can overload a static methodology in Java. You’ll be able to declare as many static strategies of the identical title as you want supplied all of them have completely different methodology signatures. See the reply for a extra detailed clarification and code instance.
8. Can we override the static methodology in Java? (reply)
No, you can’t override a static methodology as a result of it isn’t bounded to an object. As a substitute, static strategies belong to a category and are resolved at compile time utilizing the kind of reference variable. However, Sure, you possibly can declare the identical static methodology in a subclass, which is able to end in methodology hiding i.e. if you happen to use the reference variable of sort subclass then the brand new methodology can be known as, however if you happen to use the reference variable of superclass then the outdated methodology can be known as.
9. Can we stop overriding a way with out utilizing the ultimate modifier? (reply)
Sure, you possibly can stop the tactic overriding in Java with out utilizing the ultimate modifier. Actually, there are a number of methods to perform it e.g. you possibly can mark the tactic as non-public or static, these can’t be overridden.
10. Can we override a non-public methodology in Java? (reply)
No, you can’t. Because the non-public methodology is just accessible and visual inside the category they’re declared, it isn’t attainable to override them in subclasses. Although, you possibly can override them contained in the interior class as they’re accessible there.
11. What’s the covariant methodology overriding in Java? (reply)
Within the covariant methodology overriding, the overriding methodology can return the subclass of the thing returned by the unique or overridden methodology. This idea was launched in Java 1.5 (Tiger) model and it is very useful in case the unique methodology is returning a basic sort like Object class, as a result of, then through the use of the covariant methodology overriding you possibly can return a extra appropriate object and forestall client-side typecasting. One of many sensible use of this idea is once you override the clone() methodology in Java.
12. Can we alter the return sort of methodology to subclass whereas overriding? (reply)
Sure, you possibly can, however solely from Java 5 onward. This function is named covariant methodology overriding and it was launched in JDK 5 launch. That is immensely useful if the unique methodology return super-class just like the clone() methodology returns a java.lang.Object. By utilizing this, you possibly can straight return the precise sort, stopping client-side type-casting of the outcome.
13. Can we alter the argument checklist of an overriding methodology? (reply)
No, you can’t. The argument checklist is a part of the tactic signature and each overriding and overridden strategies will need to have the identical signature.
14. Can we override a way that throws runtime exception with out throws clause? (reply)
Sure, there is no such thing as a restriction on unchecked exceptions whereas overriding. However, within the case of a checked exception, an overriding exception can not throw a checked exception which comes increased in sort hierarchy e.g. if the unique methodology is throwing IOException then the overriding methodology can not throw java.lang.Exception or java. lang.Throwable.
15. How do you name a superclass model of an overriding methodology in a subclass? (reply)
You’ll be able to name a superclass model of an overriding methodology within the subclass through the use of the tremendous key phrase. For instance to name the toString() methodology from java.lang.Object class, you possibly can name tremendous.toString().
16. Can we override a non-static methodology as static in Java? (reply)
Sure, you possibly can override the non-static methodology in Java, no downside on them however it shouldn’t be non-public or last 🙂
17. Can we override the ultimate methodology in Java? (reply)
No, you can’t override a last methodology in Java, the ultimate key phrase with the tactic is to stop methodology overriding. You employ the ultimate when you don’t need subclass altering the logic of your methodology by overriding it attributable to safety causes. That is why the String class is last in Java. This idea can also be used within the template design patterns the place the template methodology is made last to stop overriding.
18. Can we now have a non-abstract methodology inside an interface? (reply)
From Java 8 onward you possibly can have a non-abstract methodology contained in the interface, previous to that it was not allowed as all methodology was implicitly public summary. From JDK 8, you possibly can add static and default strategies inside an interface.
19. What’s the default methodology of Java 8? (reply)
The default methodology, also called the extension methodology is a brand new sort of methodology which you’ll add to the interface now. These strategies have implementation and are supposed for use by default. By utilizing this methodology, JDK 8 managed to supply frequent performance associated to lambda expression and stream API with out breaking all of the shoppers who implement their interfaces. In the event you take a look at Java 8 API documentation one can find a number of helpful default strategies on key Java interfaces like Iterator, Map, and so forth.
20. What’s an summary class in Java? (reply)
An summary class is a category that’s incomplete. You can not create an occasion of an summary class in Java. They’re supplied to outline default habits and ensured that consumer of that class ought to adore to these contract which is outlined contained in the summary class. To be able to use it, you could prolong and implement their summary strategies. BTW, in Java, a category could be summary with out specifying any summary methodology.
21. What’s an interface in Java? What’s the actual use of an interface? (reply)
As an summary class, the interface can also be there to specify the contract of an API. It helps the OOP abstraction idea because it defines solely summary habits. It should inform that your program will give output however how is left to implementers. The true use of the interface to outline varieties to leverage Polymorphism. See the reply for a extra detailed clarification and dialogue.
22. The distinction between Summary class and interface? (reply)
In Java, the important thing distinction is that the summary class can include a non-abstract methodology however the interface can not, however from Java 8 onward interface may also include static and default strategies which might be non-abstract. See the reply for a extra detailed dialogue as I’ve described a variety of factors there.
23. Can we make a category summary with out an summary methodology? (reply)
Sure, simply add summary key phrase on the category definition and your class will grow to be summary.
24. Can we make a category each last and summary on the similar time? (reply)
No, you can’t apply each last and summary key phrases to the category on the similar time as a result of they’re precisely the other of one another. A last class in Java can’t be prolonged and you can’t use an summary class with out extending and making it a concrete class. As per Java specification, the compiler will throw an error if you happen to attempt to make a category summary and last on the similar time.
25. Can we overload or override the principle methodology in Java? (reply)
No, since major() is a static methodology, you possibly can solely overload it, you can’t override it as a result of the static methodology is resolved at compile time without having object info therefore we can not override the principle methodology in Java.
26. What’s the distinction between Polymorphism, Overloading, and Overriding? (reply)
This can be a barely tough OOP idea query as a result of Polymorphism is the actual idea behind each Overloading and Overriding. Overloading is compiled time Polymorphism and Overriding is Runtime Polymorphism.
27. Can an interface prolong a couple of interface in Java?
Sure, an interface can prolong a couple of interface in Java, it is completely legitimate.
28. Can a category prolong a couple of class in Java?
No, a category can solely prolong one other class as a result of Java does not help a number of inheritances however sure, it could actually implement a number of interfaces.
29. What’s the distinction between abstraction and polymorphism in Java? (reply)
Abstraction generalizes the idea and Polymorphism means that you can use completely different implementations with out altering your code. This diagram explains the abstraction fairly effectively, although:
8 Object-Oriented Design Precept and sample Interview Questions
Now let’s have a look at some OOPS idea questions primarily based on the SOLID design rules and GOF design patterns that benefit from the OOPS idea mentioned right here.
Technique sample means that you can introduce a brand new algorithm or new technique with out altering the code which makes use of that algorithm. For instance, the Collections.kind() methodology which types the checklist of the thing makes use of the Technique sample to check objects. Since each object makes use of a special comparability technique you possibly can examine varied objects otherwise with out altering the type methodology.
The decorator sample takes benefit of Composition to supply new options with out modifying the unique class. An excellent to-the-point query for the telephonic spherical. That is fairly clear from the UML diagram of the Decorator sample, as you possibly can see the Element is related to a Decorator.
While you want only one occasion of a category and wish that to be globally obtainable then you need to use the Singleton sample. It isn’t freed from value although as a result of it will increase the coupling between courses and makes them arduous to check. This is among the oldest design sample questions from Java interviews. Please see the reply for a extra detailed dialogue.
4. What’s the distinction between State and Technique Patterns? (reply)
Although the construction or class diagram of the State and Technique sample is similar, their intent is totally completely different. The state sample is used to do one thing particular relying upon the state whereas Technique means that you can change between algorithms with out altering the code which makes use of it.
5. What’s the distinction between Affiliation, Aggregation, and Composition in OOP? (reply)
When an object is expounded to a different object is named affiliation. It has two varieties, aggregation, and composition. the previous is the unfastened type of affiliation the place the associated object can survive individually whereas later is a stronger type of affiliation the place a associated object can not survive individually. For instance, the town is an aggregation of individuals however is the composition of physique elements.
6. What’s the distinction between Decorator, Proxy, and Adapter patterns in Java? (reply)
Once more they give the impression of being related as a result of their construction or class diagram may be very related however their intent is sort of completely different. The Decorator provides further performance with out touching the category, Proxy gives entry management, and an Adapter is used to make two incompatible interfaces work collectively.
7. What’s the 5 objects oriented design precept from SOLID? (reply)
SOLID is the time period given by Uncle Bob in his basic ebook, the Clear Code, one of many must-read books for programmers. In SOLID every character stands for one design precept:
- S for Single Accountability Precept
- O for Open closed design precept
- L for Liskov substitution precept
- I for Interface segregation precept
- D for Dependency inversion precept
8. What’s the distinction between Composition and Inheritance in OOP? (reply)
Composition permits the category to get an extra function at runtime, however Inheritance is static. You can’t change the function at runtime by substitution of a brand new implementation. See the reply for a extra detailed dialogue.
That is all about on this checklist of object-oriented programming or OOPS idea interview questions. We have now seen questions from varied OOPS ideas like Abstraction, Encapsulation, Inheritance, Composition, Aggregation, Affiliation, Class, Object, Interface, and so forth.
We have now additionally seen questions from Object-oriented design rules also called SOLID rules and GOF design patterns like Technique, State, and Manufacturing unit, that are primarily based upon each the object-oriented programming idea and OOP design precept.
However, as I mentioned, if you’re a senior Java developer then you definitely focus extra on object-oriented evaluation and design and learn to code towards a requirement utilizing all of your OOP information. You too can learn Cracking the Coding Interview, sixth Version for extra Object Oriented Programming questions.
Different Java programming and OOP tutorials Chances are you’ll like
Thanks for studying this text thus far. In the event you like an object-oriented programming tutorial then please share it with your pals and colleagues. In case you have any questions or suggestions then please drop a observe.