 Hello everyone, in this video I will be sharing you Java interview question and answers which I have encountered in my career and how I have answered them when I have encountered those questions and if you are an experienced guy 101% there are chances that you will encounter these questions in all of your interviews. So let us start with the first question that is what are solid principles. So for that question how do I answer the solid principles or the object related object oriented design concepts relevant to software development solid is an acronym for five design principles like a stands for single responsibility principle over open closed principle and for list code substitution principle I for integral interface segregation and deferred dependency inversion principle and after that I will explain what is sing what does it mean with single responsibility principle like each class according to single responsibility principle each class should be responsible for single part or single functionality in the system and open closed circuit principle which state that software component should be open for extension but not for modification then comes list code substitution principle object of a superclass should be replaceable with the object of its subclass without breaking the system and the interface segregation means like no clients or no class should be forced to depend on method that is it that it does not use and fifth one dependency inversion like how high level models should not depend on low level models or both like vice versa at low level model should not depend on the high level models everything should be dependent on abstraction so this is how I answer this question then comes there there might be few people who who may counter question this like where exactly in your application you have used it so you have to select or if the same question is asked me like where I have you used this principle like or any one of the principle they can ask where have you used single responsibility like I then at the time I explain them like taking example of any of my service class that is related to customer so all the customer related things we do that in that customer in that customer service class like there are departments if at all department services there so only department related operations will perform there so I will give such kind of example and there can be a complete counter question saying have you ever seen that solid principles getting break in your application yes there might be chances I have seen particularly in my applications that in many classes we we unnecessarily implement few interfaces like we we need only few methods from that interface but that interface will be having 10-15 methods and in the implementation classes in them also in the implementation methods will be directly writing one logger or I will directly writing one what I can say I comment saying that this method will never be called like that things we can see so that time this gets break so this is how I answer how what are solid principles like coming to the next question the next question is what are oops concepts or what are the main oops concepts so I usually go with four things like a abstraction encapsulation polymorphism and inter inheritance so there are association composition aggregation as well but mostly I go with this four only like what is inherited inheritance is a process where one class acquires the properties of another encapsulation in Java is a mechanism of wrapping up the data and code together as a single unit and here there can be a counter question like how do we how do we get achieve encapsulation so by using private variables and public setters and getters we will we achieve the encapsulation then comes abstraction what is abstraction abstraction is a methodology of hiding the implementation details from the user and only providing the functionality to the user so what abstraction actually means like we are hiding the what I can say the complex logic from the end user simply we can see that then comes polymorphism polymorphism is nothing but the ability of a variable or function or object to take multiple forms like depending upon the object that it is acting the method performs the same method acts differently there are two types of polymorphism again like dynamic polymorphism and static polymorphism so dynamic polymorphism like at the compile time itself you know what will happen which method will get executed but in dynamic polymorphism it is a different case like the runtime on what object the method is getting called that method will get executed so this was about the oops concept and you can give the definition of class object everything comes under this I usually prefer going with these four things like inheritance encapsulation abstraction and polymorphism so mostly our interiors are happy with coming to the next question next question is difference between comparator and comparable in Java so one of the most important question to check whether you have had some experience on this or not so firstly coming to the difference so basically these two interfaces are used to use for sorting so if at all we want to go for natural sorting we go with comparable and if at all we want to have a customized ordering or sorting we go with comparator and comparator what I can have compare method which take two arguments method arguments are comparable is having comparative method and comparable interface is in Java utility and comparable interface is from Java dot lang package when I have to like this is one of the like I said this is one of the major difference I say whenever whenever I have to answer this question whenever I have access to a class then I go with comparable if it I am not having access to the class directly then or else if at all I am reading a class from from jar I am getting that class then I will go with comparator so because whenever a class extend implements comparable in the same in the same class we need to have that we need to order the comparative method and we need to give our logic but coming to comparator we can have a separate class and by mentioning the class type we can have different comparator class altogether so this is how I answer why coming to the next question what is the difference between interface and abstract class in Java so before Java 8 there were huge differences between comparator between interface and abstraction like interface in interface we used to have only abstract method but in abstract class we used to have both abstract and concrete methods so now with the Java 8 what the difference has mostly erased the what I can say the difference is minimized now but still there are few things that remains like still a class can extend only one class but it can implement different interfaces multiple interfaces and what I can say we can we cannot we still don't have constructive inside interface what we can have constructive inside abstract class and we can if at all there is a main method I think we can run even abstract class but that is not possible with the interface so these are the differences that I see whenever I am whenever I have encountered this question so this is about the differences coming to the next question difference between fail safe and fail fast iterators in Java so as the name suggests fails fast and fail safely fail fast fail immediately so what does that mean so whenever it fails like a fail fast iterator throws concurrent modification exception as soon as it detects any change on the collection object that we are iterating or that we are traversing this is because it internally maintains a mod count so number of objects that are present if at all anything is getting added or anything is getting removed at that time this mod count will change and which in turn that says that suppose there is some problem in that iterator that I am you are traversing currently and immediately it will throw concurrent modification exception but fail safe works differently it travels over the like a copy of the collection object it doesn't traverse on the original collection but it travels on the copy so fail stage works like we can say like fail fast iterator works on the live collection live object but whereas fail safe works on the copy of the object so this is how we can answer this and you like we use fail safe iterator when we are not bothered about collection to be modified during iteration as fail fast iterator will not allow that so if at all there is a scenario like even though the objects are changing in the inside the collection and if we don't want to throw an exception at that time we will go with fail safe iterator it just by having copy of there is my method so we can use that method and say that do that coming to the next question can we override a private or static methods in java so to be honest this is a bit tricky question private to be honest we cannot because the scope itself will not come into the child class so we cannot override state forward answer but static method it's a bit tricky so whenever we are having this same static method inside child class so basically we can have that but what happens here is it will over like what it will hide the parent class object parent class method so that is one more important interpretation like what high like what is method hiding comes into between method hiding also it's the same thing like we are having a both static method both inside a parent class and inside child class and child class will hide the parent class static method so as said we cannot override private methods as the scope itself is not accessible the scope itself is not to override so the next question is what is an association aggregation and composition so this mostly this mainly comes under oops concepts only but still this this is being asked as a theoretical question you can see so this is how we can explain so association is a relationship where all the objects have their own life cycle and there is no owner let us take an example of teacher and student like multiple students can associate with a t single teacher and a single student can associate with multiple teachers but there is no ownership between the objects and both have their own life cycles the relationship can be one to one one to many many to one one like that so that is what is association and coming to aggregation aggregation is nothing but a specialized form of association where all the objects have their own life cycle but there is an ownership and child object cannot belong to another parent object so example is there like department and teacher we can take and we can explain that coming to composition composition also a specialized form of aggregation and it is like the child object cannot sustain without the parent or we can take house and rooms example a room cannot individually survive if at all there is a house then there are rooms if there is no house the rooms will also not be present so this is how we can this is how I answer so next question is how do you override when do you override hash code and equals method most of that we directly say whenever we use hash map we override hash code and equals method but to be honest we can it depends basically like whenever necessary we we override this especially if you want to do an equality check based upon business logic rather than object equality like if at all I am going to say like two employee objects are same only if their employee ID is same so I will override hash code and equals method in the equals method I will write the logic like if at all these two uh uh content is same on the IDs then these two objects should that equals method should return to one we are saying these two objects are equal and one more point to add is also like overriding both this method is a must if you are if you use them as a key in hash like this is the most this is the first answer that people give like uh whenever we are using uh something as a key then we go for overriding hash code and equals method like as part of equals hash code contract in java we we when we override equals we must override hash code as well so but this is the answer that I give coming to the next question like what is the impact like what is the performance impact if it all we are not overriding hash code method so basically uh hash collision there are high chances that hash collision will happen like as per my knowledge a poor hash function will result in the frequent collision in hash map it eventually increase the time of adding an object or getting an object on the hash map but in if at all we are using java 8 this is minimized like after a certain threshold instead of linker list uh what we have like we use binary tree now so so which will like what what you can say like the performance a bit uh is increased even though a poor hash code method is written as it is using tree uh tree binary tree so it won't have that much impact coming to the next question how does substring inside string work so this is one of the important interview question if you are having really two to like two to four years of experience candidates so how does it works so basically substrings create a new object out of source string by taking a portion of original string uh the reason why they ask this question is uh related to java 1.7 this used to work in a different way so whenever we used to do a substring method uh it used to get like like what i what we can say like hold the reference of the original character array which means that even a substring of like two to three character can prevent one gb character array from garbage collection like uh like by holding a strong reference there so that was the problem in string substring method so after this was resolved in 1.7 uh like now a new object is created that is a drawback but still uh like the above mentioned problem is resolved like holding the strong references removed from the original character array so this is a what substring method next question is what is try with resource in java seven java like basically in java this is one of the feature that was introduced in java seven so before java seven there was no auto resource management like we should explicitly loot the resource whichever we are using so usually it was done in the finally block of tricast statement this approach used to cause memory leak at times when we forgot to close the resource but from java seven uh we can we can create resources into a tri block and use it java takes care like basically it you know the resources are implementing auto closeable interface so that will take care of closing the resource automatically so that is more please try with the resource instead of uh uh we concentrating on the closing of resource the framework automatically takes care of that explain about class loader in java one of the important question again java class loader is a program that loads bytecode program into memory when we want to access any class we can create our own class loader by extending class loader class and overriding class load class string name method like basically overloading class load class method we can override we can have our own class loader so after that basically there will be a counter question like what are the different types of class loader so coming to different types of class loader first one is boost up class loader it loads jdk internal classes typically loads rt.jr and other core classes then comes extension class loader it loads classes from jdk extension directory usually java homelift ext whatever present in that in this directory so those are loaded as part of this then comes system class loader it loads classes from the current class path that can be set while invoking a program using class path command and options so these are some questions that i wanted to share as part of this series and there are many to come like as the video is becoming big so i thought of sharing it in bits and pieces and thank you for watching i hope you like this video or you this video might help you in checking your interviews thank you for watching