Sunday, November 24, 2019

Top Xxx Oop Concept Interview Questions Answers Inwards Coffee - Object Oriented Programming

Java is an object-oriented programming linguistic communication in addition to y'all volition run across a lot of object oriented programming concept questions on Java interviews. The classic questions similar divergence betwixt interface in addition to abstract cast are ever at that spot but from the lastly couplet of years to a greater extent than sophisticated questions based upon advanced pattern principles in addition to patterns are equally good asked to banking enterprise jibe OOP noesis of the candidate. Though, Object oriented programming questions are to a greater extent than pop on Java interviews for 1 to three years experienced programmers. It makes sense equally well, equally these are the programmers who must know the OOP basic similar Abstraction, Inheritance, Composition, Class, Object, Interface, Encapsulation etc.

If y'all await for Java interview questions for 2 to four years experienced programmer, y'all volition detect lots of questions based upon OOP fundamentals similar Inheritance in addition to Encapsulation but equally y'all gain to a greater extent than experience, y'all volition run across questions based on object-oriented analysis in addition to pattern e.g. code a vending pattern machine or implement a coffeemaker inward Java.

These questions are to a greater extent than hard in addition to require non solely truthful agreement of OOP exchange but equally good nearly telephonic round.


If y'all are senior Java developer in addition to then y'all already know answers to this inquiry in addition to I propose y'all practise to a greater extent than on object-oriented analysis in addition to pattern science i.e. how to do code against a specification. If y'all are fresher in addition to junior Java developer alongside 2 to three years sense in addition to then y'all must revise these questions, larn if y'all don't know to do good on your Java Job interviews.


What is method overloading inward OOP or Java? (answer)
It's 1 of the oldest OOPS concept questions, I conduct maintain seen it 10 years agone in addition to however sees it now. When nosotros conduct maintain multiple methods alongside the same advert but dissimilar functionality in addition to then it's called method overloading. For example. System.out.println() is overloaded equally nosotros conduct maintain half dozen or seven println() method each accepting a dissimilar type of parameter.


What is method overriding inward OOP or Java? (answer)
It's 1 of the magic of object oriented programming where the method is chose based upon an object at runtime. In lodge for method overriding, nosotros demand Inheritance in addition to Polymorphism, equally nosotros demand a method alongside the same signature inward both superclass in addition to subclass. Influenza A virus subtype H5N1 telephone telephone to such method is resolved at runtime depending upon the actual object in addition to non the type o variable. See the reply for to a greater extent than detailed discussion.


What is method hiding inward Java? (answer)
When y'all declare 2 static methods alongside same advert in addition to signature inward both superclass in addition to subclass in addition to then they cover each other i.e. a telephone telephone to the method inward the subclass volition telephone telephone the static method declared inward that cast in addition to a telephone telephone to the same method is superclass is resolved to the static method declared inward the super class.


Is Java a pure object oriented language? if non why? (answer)
Java is non a pure object-oriented programming linguistic communication e.g. at that spot are many things y'all tin post away do without objects e.g. static methods. Also, primitive variables are non objects inward Java. See the reply for a to a greater extent than detailed explanation.



What are rules of method overloading in addition to overriding inward Java? (answer)
One of the most of import rules of method overloading inward Java is that method signature should live dissimilar i.e. either the divulge of arguments or the type of arguments. Simply changing the render type of 2 methods volition non outcome inward overloading, instead, the compiler volition throw an error. On the other hand, method overriding has to a greater extent than rules e.g. advert in addition to render type must live same, method signature should equally good live same, the overloaded method cannot throw a higher exception etc. See the reply for a amount listing of rules related to method overloading in addition to overriding inward Java.


The divergence betwixt method overloading in addition to overriding? (answer)
Several differences but the most of import 1 is that method overloading is resolved at compile fourth dimension in addition to method overriding is resolved at runtime. The compiler solely used the cast information for method overloading, but it needs to know object to resolved overridden method calls. This diagram explains the divergence quite well, though:

oriented programming linguistic communication in addition to y'all volition run across a lot of object oriented programming concep Top xxx OOP Concept Interview Questions Answers inward Java - Object Oriented Programming


Can nosotros overload a static method inward Java? (answer)
Yes, y'all tin post away overload a static method inward Java. You tin post away declare equally many static methods of the same advert equally y'all wishing provided all of them conduct maintain dissimilar method signatures. See the reply for to a greater extent than detailed explanation in addition to code example.


Can nosotros override static method inward Java? (answer)
No, y'all cannot override a static method because it's non bounded to an object. Instead, static methods belong to a cast in addition to resolved at compile fourth dimension using the type of reference variable. But, Yes, y'all tin post away declare the same static method inward a subclass, that volition outcome inward method hiding i.e. if y'all utilization reference variable of type subclass in addition to then novel method volition live called, but if y'all utilization reference variable of superclass than old method volition live called.


Can nosotros forestall overriding a method without using the concluding modifier? (answer)
Yes, y'all tin post away forestall the method overriding inward Java without using the concluding modifier. In fact, at that spot are several ways to attain it e.g. y'all tin post away grade the method private or static, those cannot live overridden.


Can nosotros override a private method inward Java? (answer)
No, y'all cannot. Since the private method is solely accessible in addition to visible within the cast they are declared, it's non possible to override them inward subclasses. Though, y'all tin post away override them within the inner cast equally they are accessible there.


What is covariant method overriding inward Java? (answer)
In covariant method overriding, the overriding method tin post away render the subclass of the object returned past times master copy or overridden method. This concept was introduced inward Java 1.5 (Tiger) version in addition to it's real helpful inward instance master copy method is returning full general type similar Object class, because, in addition to then past times using covariant method overriding y'all tin post away render to a greater extent than suitable object in addition to forestall customer side type casting. One of the practical utilization of this concept is inward when y'all override the clone() method inward Java.



Can nosotros alter the render type of method to subclass piece overriding? (answer)
Yes, y'all can, but solely from Java five onward. This characteristic is known equally covariant method overriding in addition to it was introduced inward JDK five release. This is immensely helpful if master copy method render super-class e.g. clone() method render java.lang.Object. By using this, y'all tin post away straight render the actual type, preventing client-side type casting of the result.


Can nosotros alter the declaration listing of an overriding method? (answer)
No, y'all cannot. The declaration listing is component subdivision of the method signature in addition to both overriding in addition to overridden method must conduct maintain the same signature.


Can nosotros override a method which throws runtime exception without throws clause? (answer)
Yes, at that spot is no restriction on unchecked exception piece overriding. On the other hand, inward the instance of checked exception, an overriding exception cannot throw a checked exception which comes higher inward type hierarchy e.g. if master copy method is throwing IOException than overriding method cannot throw java.lang.Exception or java.lang.Throwable.


How do y'all telephone telephone superclass version of an overriding method inward sub class? (answer)
You tin post away telephone telephone a superclass version of an overriding method inward the subclass past times using super keyword. For instance to telephone telephone the toString() method from java.lang.Object cast y'all tin post away telephone telephone super.toString().


Can nosotros override a non-static method equally static inward Java? (answer)
Yes, y'all tin post away override the non-static method inward Java, no occupation on them but it should non live private or concluding :)


Can nosotros override the concluding method inward Java? (answer)
No, y'all cannot override a concluding method inward Java, concluding keyword alongside the method is to forestall method overriding. You utilization concluding when y'all don't desire subclass changing the logic of your method past times overriding it due to safety reason. This is why String cast is concluding inward Java. This concept is equally good used inward template pattern pattern where template method is made concluding to forestall overriding.



Can nosotros conduct maintain a non-abstract method within interface? (answer)
From Java 8 onward y'all tin post away conduct maintain a non-abstract method within interface, prior to that it was non allowed equally all method was implicitly world abstract. From JDK 8, y'all tin post away add together static in addition to default method within an interface.


What is the default method of Java 8? (answer)
Default method, equally good known equally extension method are novel types of the method which y'all tin post away add together on the interface now. These method has implementation in addition to intended to live used past times default. By using this method, JDK 8 managed to render mutual functionality related to lambda expression in addition to stream API without breaking all the clients which implement their interfaces. If y'all await Java 8 API documentation y'all volition detect several useful default method on key Java interface similar Iterator, Map etc.


What is an abstract cast inward Java? (answer)
An abstract cast is a cast which is incomplete. You cannot create an instance of an abstract cast inward Java. They are provided to define default behaviour in addition to ensured that customer of that cast should adore to those contract which is defined within the abstract class. In lodge to utilization it, y'all must extend in addition to implement their abstract methods. BTW, inward Java, a cast tin post away live abstract without specifying whatsoever abstract method.


What is an interface inward Java? What is the existent user of an interface? (answer)
Like an abstract class, the interface is equally good at that spot to specify the contract of an API. It supports OOP abstraction concept equally it defines solely abstract behavior. It volition tell that your computer programme volition give output but how is left to implementors. The existent utilization of the interface to define types to leverage Polymorphism. See the reply for to a greater extent than detailed explanation in addition to discussion.


The divergence betwixt Abstract cast in addition to interface? (answer)
In Java, the key divergence is that abstract cast tin post away comprise non-abstract method but the interface cannot, but from Java 8 onward interface tin post away equally good comprise static in addition to default methods which are non-abstract. See the reply for to a greater extent than detailed intelligence equally I conduct maintain described a lot of points there.

answer)
Yes, simply add together abstract keyword on the cast Definition in addition to your cast volition acquire abstract.


Can nosotros brand a cast both concluding in addition to abstract at the same time? (answer)
No, y'all cannot apply both concluding in addition to abstract keyword at the cast same fourth dimension because they are just reverse of each other. Influenza A virus subtype H5N1 concluding cast inward Java cannot live extended in addition to y'all cannot utilization an abstract cast without extending in addition to acquire inward a concrete class. As per Java specification, the compiler volition throw an fault if y'all endeavour to brand a cast abstract in addition to concluding at the same time.


Can nosotros overload or override the primary method inward Java? (answer)
No, since main() is a static method, y'all tin post away solely overload it, y'all cannot override it because the static method is resolved at compile fourth dimension without needing object information thus nosotros cannot override the primary method inward Java.



What is the divergence betwixt Polymorphism, Overloading, in addition to Overriding? (answer)
This is slight tricky OOP concept inquiry because Polymorphism is the existent concept behind on both Overloading in addition to Overriding. Overloading is compiled fourth dimension Polymorphism in addition to Overriding are Runtime Polymorphism.


Can an interface extend to a greater extent than than 1 interface inward Java?
Yes, an interface tin post away extend to a greater extent than than 1 interface inward Java, it's perfectly valid.


Can a cast extend to a greater extent than than 1 cast inward Java?
No, a cast tin post away solely extend approximately other cast because Java doesn't back upwards multiple inheritances but yes, it tin post away implement multiple interfaces.


What is the divergence betwixt abstraction in addition to polymorphism inward Java? (answer)
Abstraction generalizes the concept in addition to Polymorphism allow y'all to utilization dissimilar implementation without changing your code. This diagram explains the abstraction quite well, though:

oriented programming linguistic communication in addition to y'all volition run across a lot of object oriented programming concep Top xxx OOP Concept Interview Questions Answers inward Java - Object Oriented Programming


Object Oriented pattern regulation in addition to pattern Interview Questions

Now let's run across approximately OOPS concept questions based on the SOLID pattern principles in addition to GOF pattern patterns which bring payoff of OOPS concept discussed here.

What occupation is solved past times Strategy pattern inward Java? (answer)
Strategy pattern allows y'all to innovate novel algorithm or novel strategy without changing the code which uses that algorithm. For example, the Collections.sort() method which sorts the listing of the object uses the Strategy pattern to compare object. Since every object uses dissimilar comparing strategy y'all tin post away compare diverse object differently without changing kind method.


Which OOP concept Decorator pattern Pattern is based upon? (answer)
Decorator pattern takes payoff of Composition to render novel features without modifying the master copy class. Influenza A virus subtype H5N1 real practiced to-the-point inquiry for the telephonic round. This is quite clear from UML diagram of Decorator pattern, equally y'all tin post away run across the Component is associated alongside Decorator.



When to utilization Singleton pattern pattern inward Java? (answer)
When y'all demand simply 1 instance of a cast in addition to wants that to live globally available in addition to then y'all tin post away utilization Singleton pattern. It's non costless of toll though because it increases coupling betwixt classes in addition to makes them hard to test. This is 1 of the oldest pattern pattern questions from Java interviews. Please run across the reply for to a greater extent than detailed discussion.


What is the divergence betwixt State in addition to Strategy Pattern? (answer)
Though the construction or cast diagram of State in addition to Strategy pattern is same, their intent is completely different. State pattern is used to do something specific depending upon state piece Strategy allows y'all to switch betwixt algorithms without changing the code which uses it.


What is the divergence betwixt Association, Aggregation, in addition to Composition inward OOP? (answer)
When an object is related to approximately other object it called association. It has 2 forms, aggregation, in addition to composition. the one-time is the loose cast of association where the related object tin post away endure individually piece afterwards is a stronger cast of association where a related object cannot endure individually. For example, the urban pith is an aggregation of people but is the composition of trunk parts.



What is the divergence betwixt Decorator, Proxy in addition to Adapter pattern inward Java? (answer)
Again they await similar because their construction or cast diagram is real similar but their intent is quite different. Decorator adds additional functionality without touching the class, Proxy provides access command in addition to Adapter is used to brand 2 incompatible interfaces piece of work together. 


What is the five objects oriented pattern regulation from SOLID? (answer)
SOLID is the term given past times Uncle Bob inward his classic book, the Clean Code, 1 of the must-read books for programmers. In SOLID each grapheme stands for 1 pattern principle:
south for Single Responsibility Principle
O for Open unopen pattern principle
L for Liskov exchange principle
I for Interface segregation principle
D for Dependency inversion principle

This is approximately other non bad OOPS concept inquiry because it tests what matters, both of them are real of import from a cast pattern perspective. Though both Composition in addition to Inheritance allows y'all to reuse code, formerly is to a greater extent than flexible than later. Composition allows the cast to acquire an additional characteristic at runtime, but Inheritance is static. You tin post away non alter the characteristic at runtime past times exchange novel implementation. See the reply for to a greater extent than detailed discussion.


That's all nearly inward this listing of object oriented programming or OOPS concept interview questions. We conduct maintain seen questions from diverse OOPS concept e.g. Abstraction, Encapsulation, Inheritance, Composition, Aggregation, Association, Class, Object in addition to Interface etc.

We conduct maintain equally good seen questions from Object oriented pattern principles equally good known equally SOLID principles in addition to GOF pattern patterns similar Strategy, State, in addition to Factory, which are based upon both object oriented programming concept in addition to OOP pattern principle. But, equally I said, if y'all are senior Java developer in addition to then y'all focus to a greater extent than on object-oriented analysis in addition to pattern in addition to larn how to code against a requirement using all your OOP knowledge. You tin post away equally good read Cracking the Coding Interview, sixth Edition to for to a greater extent than Object Oriented Programming questions.

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

No comments:

Post a Comment