Transcript for:
Java for Enterprise Applications: Key Interview Questions

Hello friends I asked Chad jpt which is the most popular language for making Enterprise level big applications and in the result Java is at the top so if you want to join big MNC or Enterprise level companies then Java is the right choice for you now the question is how do you crack Java interviews for those companies before moving on here are some students who shared their success stories with me in the comment section so whenever you get the success or offer kindly share in comments for now you can just like And subscribe that is the great motivation for me to make more content on cracking interviews and helping helping more candidates in getting the jobs okay for that I'm sharing the top 30 interview questions which are like the foundation and the minimum eligibility criteria which you must know before attending any Java interview okay so here in the first chapter we will cover seven questions on Java Basics like jdk JRE jvm then in the second chapter questions about control statements strings and arrays next question from classes objects access a specifier this keyword and getter Setter methods then questions from inheritance sorry inherit and polymorphism and finally questions on encapsulation and abstraction so if you are experienced in Java then these questions will look a bit easier for you right but then do you remember this complete process of jdk J jvm jit or do you remember this use of the static keyword or do you know how the string and string Builder are stored differently in Java memory now most importantly do you know which concept you will use while building application for example when to use the fs when to use the tary operator and when to use the S cases in real application okay so these kinds of questions I will cover in this top 30 questions maybe you know 20% 30% of the answer but sometimes we know the wrong answer so this video will remove all your confusions and make your found your foundation very very strong Rock Solid okay so let's start with the first chapter here and as you already know here we will see questions on jdk JRE jvm main method bite code data types Etc so let's start with the first question what is Java here is the simplest definition of java Java is a high level objectoriented programming language high level language because when you compile a Java language program then first it will be converted into bite code which is a Intermediate Language and bite code will further converted into machine or native code which is a lowlevel language okay in the upcoming question I will explain the process of this converting Java to bite code then to machine code in detail but now you know Java is a high level language right then Java is objectoriented also because Java uses objects and classes to structure the data and functions okay and in Java programs we can easily apply objectoriented Concepts like encapsulation exraction polymorphism and inheritance that's why Java is an objectoriented programming language so all these Concepts objectoriented Concepts we will explore in upcoming questions in detail what are jdk JRE and jbm how a Java program compiled or executed let's understand with the help of a simple diagram suppose you have a very basic Java program first code. Java okay now before running this program if there is any syntax error here then Java will automatically show it to you right that that is called compile time error checking which will happen automatically even before running this program now the question is how does it automatically happens this is done by a kit which we call the Java development kit or jdk which we installed just before running the Java programs okay so what happens inside this jdk kit we will have many tools for executing Java programs and one of those tools is the Java compiler tool and this Java compiler tool is used to compile the above Java program okay meaning Java compiler tool of jdk is responsible for checking and showing any type any kind of compile time errors in the Java program and also it convert the Java program to bite code let me tell you how suppose this is your code Java code and suppose you are also using some Java class libraries inside it like string class array list class okay then all those libraries will be present inside the JRE so it will contain all the J uh libraries code J is what J is Java runtime environment and it contains libraries and other things also and it is also present inside the jdk only okay now listen carefully when you are writing the Java code then the Java compiler side by side compiling and converting your code plus the library code which it is getting from the JRE if you are using any library in your code then only then that code will be converted to bite code by the Java compiler and that is also called the compile time process this is whole compile time process before running the program remember until now we have not run this program and bite code is already already generated by the Java compiler now when you press the Run program button or run command after that it will be runtime okay what will happen next JD key will have the J right and JRE will have the jvm inside it and jvm will contain the jit compiler okay which will finally execute and convert the bite code into the native code or the machine code you can say which is the ultimate code which your computer and Hardware can understand okay so this conversion of bit bite code to native code machine code is called run time okay if there is any error then it is called runtime error and in the runtime great that is the whole story and yes apart from jit compiler jvm also do memory management thread handling Etc and yes remember one thing jvm is present inside JRE and JRE is present inside jdk meaning you only need jdk to install on your system to run the Java programs so that is the whole process that happens in the background when you writing the Java program and after that you are running okay also here I have written the simplest description definition of jdk then JRE then jvm then jit so basically whatever I have explained all this is here in the written format so that later if you want to refresh or revise all these Concepts then rather than listening to the complete video again just have a look for 3 4 seconds and your revision will be done that's a faster way of revision just before the interviews right hi friends here is a quick notification these 30 questions are part of my full top 200 interview questions course in this course I will similarly share 200 questions from these 20 topics in a step by step manner so that even if you are new to Java still you will understand all the questions and there is a bonus chapter also which will include Java programming questions also this PP and the PDF book of 200 questions from which I'm teaching ing this is available for download the link is in the descriptions many students were able to crack the interviews my Maya courses and I am sure you will also what are the main features and advantages of java now I will show you five main features of java and if you are experienced in Java then you can easily understand them but if you are new in Java then revisit these features and advantages again after some time okay the first advantage of the Java language is that Java is platform independent meaning Java programs can run on any operating systems platforms like Windows Linux Etc okay the second feature is that Java is object oriented meaning Java supports objectoriented Concepts like a feature like encapsulation inheritance polymorphism and abstraction okay third feature is Java is robust and secure robust because Java can a have automatic memory management concept okay you do not have to remove the memory and all so that is done by Java itself and secure Java is yes Java have many features for security next feature is Java supports multi-threading yes and the last Advantage is Java has Rich standard libraries Rich standard libraries means for example suppose you want to perform sorting filtering on in your application then Java has many in inbuilt classes and functions for these common functionalities okay so inbuilt means Java already have some classes and functions inside it you just have to call those functions to use them okay that is inbuilt all right whatever now I have explained I have written here in very simplest terms and that will help you in revision and that is the answer of this question now yes one more thing this is like a very high level idea but in upcoming questions we will discuss more questions on what is multi multi threading how Java implements multi-threading what are the standard libraries many many standard libraries we will discuss then most importantly the objectoriented concepts which is very important from interview point of view that also we will cover in very very detail okay what is main method in Java what is the role of public static and void in it this is a coding question so let's see the code here is our very first basic program in Java class I will explain later okay in upcoming questions let's focus on this main method see we already know inside jdk we have jvm jvm right jvm is what jvm is a program only which is created by the Java creators okay now listen carefully whenever you execute any program the jvm program will first executed and like we call methods in our normal Java program similarly the jvm program will call this main method from inside it okay so jvm program will call the main method that's how this main method will be executed got it in a way you can also say that the main method in Java is the first method to be executed by the jvm or you can say that main method is the entry point for a Java program okay now the second part of the question is what is the role of public static and wide okay these keywords see if you are new to Java new to these keywords then I will explain them in detail in upcoming question but here I will give you a very short description of these keywords okay so public is an access specifier meaning by using this public keyword now your main method will be accessible from outside even outside this first code class and and that is the reason why the jvm program which is outside this first class first code class is still able to access your main method that is the reason okay then this see this main method can't be accessible if this is not public suppose I do it private private means now this main method is not accessible outside this first coll class and if I will try to run this so see at the bottom here you will found main method not found in first code okay because main method is not accessible by the jvm clear the second keyword is static which makes this main methods as a static method meaning you can call this main method directly from inside the jvm without even creating the object of this first class code first code class okay so that is the purpose of a static and then we have void which means main method is not going to return anything that's it again don't worry if you are new to this keywords I will cover in deta detail this static also in detail in upcoming questions all right great now I have copied all the code here and here also I have posted the description of public keyword then static keyword then what is the purpose of wi and main method all I already explained but this can help you in Faster revision before the interviews right what is Java bite code what is high level and lowlevel code all the time we are saying Java is converted to bite code white code but what is this bite code see here is our Java code okay then with the help of the Java compiler tool inside the jvk jdk this Java code will be converted into this kind of pite code so see how complex is this bite code okay this is the same thing bite code and this is complex that's why we never write bite code code directly okay we write simple Java program and then it is the responsibility of jdk to convert that into bite code okay now the simplest definition of bite code is here Java bite code is a platform independent intermediate code generated by the Java compiler simple right now what is high level and lowlevel code so you already know the answer Java is the high level bite code is the intermediate level and Native machine code is the low lowlevel code okay great I hope now you are feeling more more confident about the Java program ex execution right what are variables and data types what are the types of data types remember one thing the primary respon responsibility of any software or web application is to process the data by the help of function for example in employee management application we basically manage and manipulate the data only of employees with the help of function that is the purpose of software application now let me show you the code and of for variables here is an empty main method okay so like this first we can declare the uh any variable and then we can initialize the value of that variable like this okay so now this declaration and initialization can be done in a single line and that is how like this okay that is how we declare and initialize in a same line okay now we can print these variables count one and count two and if I will run the code run Java program so here in the out put you will find this value 100 and2 200 okay so data and now what is data type these are variables now what is data type data type for example this int Define the type of the variable in short variable and data types together are used to store and handle data in your program okay great now the same code and the definition I have printed here in the book itself now how many types of data types are there see here are the two main categories of data types primary data types and reference data types but these are categories like uh electronics is a category but the actual products are TV fridge mobile these are actual products similarly here these bite int float class array these are the actual data types but they are categorized into two categories primitive and reference data type and we will explore the differences between this primitive and reference data types in upcoming questions which is a very important question what are the differences between primitive and reference data types very important question so if you we have a primitive data type like this then in the memory the variable X and its value 10 will be directly stored at this address in the memory stack like this okay but if we have a reference data type like this a string in our Java program then it will be stored something like this here first the variable then the value reference will be stored at some this address in the stack memory but the value is not here that will be in the Heap memory at this address location which is basically the reference okay that means we are maintaining two types of memory for reference data types one is a stack and another is a heap now let's conclude the differences between them first difference is primitive data types store actual values directly in the memory okay whereas reference data types store references of the value or object and the actual values in the heat memory different memory okay next difference is primitive data types mostly hold single value single okay integer single value like above okay right in the stack memory but the reference data types can hold multiple values for example a single variable array can in a stack can hold multiple values inside the heat memory okay so that is the advantage of reference data type okay so great then next difference is oh yes Prim primitive data types have fixed storage once you decided it is final okay for example in have a fixed storage whereas a reference data types have variable storage sizes meaning you can add multiple values in the Heap memory anytime corresponding to a single variable in stack like an array okay so this is also an advantage why we have this complex M memory management for reference types okay last difference is primitive data types are stored in the stack memory you already know whereas reference data types are stored in the Heap memory with the their references actually stored in the stack great that is the differences between the Primitive and non-primitive T types all are written here so that later when you are revising then you can revise them in 2 three seconds only from the book all right congrats for completing the first chapter now here is one piece of advice from advice from my experience always try to elaborate the answer with definition and try to relate it with your own project examples okay for example I used in interface or inheritance for creating the base classes so speak like this in interviews please don't give one line answers all right now in this chapter we will cover questions about control statements break continue loops and conditional statements then string string Builder and arrays so let's start with the first question of this chapter what are control statements in Java let me explain with an example suppose you start the Java program with a simple variable initialization I equal to 0 now after that you are checking a condition using the if condition whether I is greater than Z if I is greater than Z is true then flow 1 is the right flow and and this and one will be executed and if I is not greater than zero then flow two is the flow and and two and two will be executed okay and you know the result because I equal to Z therefore the second flow will be executed the and two will be executed right so basically this if condition is an example of control statement because the definition of control statement is control statement manages the flow of execution in a program meaning the program will executed this side or this side that will be decided by the control statements okay now listen carefully in your Java program or application which flow to choose and which flow not to choose is decided by the control statement and same as this if condition we have multiple other control statements which help us to decide this okay so here are the three categories one is conditional statement then there are looping statements and branching statements these are the categories inside the conditional statements we have the F Els we already know then we have the switch statement and then we have the tary operators okay then inside the looping statement we have the for Loop y Loop doy Loop and for each Loop so as I said these are the categories conditional is different looping is different and branching under the branching statement we have the break keyword and the continue and the written keyword Okay so in the upcoming questions I will cover all these control statements in separate question okay by using all these control statement you can decide the flow of execution in your application great and yes one more thing actually this tary operator and for are not actually the control statements basically for example this stary operator is used to simplify the if else condition only tary op operator is an operator which I already explained but I don't want to conf confuse you so consider tary operator also a control statement and for each is also used to simplify the for Loop only but you can also consider it as a separate control statement in Java great that is the answer of the this question and in upcoming question we will discuss and see all these control statements in detail when to use which type of conditional statements in real world application so this is the actual very important and very good interview question how you will decide which conditional statement is good for you in your application at what moment okay so here I will compare the code of all them side by side okay first we have the if else condition code here here and then we have the tary operator code here and finally we have the switch statement code here so now we will first start by understanding the F El condition when to use it in our application now here you can see IFL condition can handle any type of complex condition meaning this if keyword and inside that you have multiple conditions here okay multiple and multiple or operators logical operators can be handled inside the if condition okay and inside this curly braces you can have multiple lines of statements can be executed for in the if or else if or else block okay so the conclusion is that if else is good for complex different and multi line execution all right and the benefit of Fels is ifls cover all the kinds of scenario so it's like it will cover everything for any condition all right now the second type of conditional statement is this tary operator right here is the code you can see here one thing the tary operator have three operants right 1 two and three which I already explained also so here first is this condition okay so this I'm directly putting is zero but basically it is rning the true so it's a condition only it can be a condition also here okay and the second and third operant basically are the result which we which we are here are a single value results and are very simple 10 and 20 so the point is tary operators are good for single condition and single value evaluations or results okay the Turner can handle only if and else you cannot handle the else if block there will be only two possibilities okay also like in if block you have multi- lines of execution multi- lines of but in ternary operator that is not possible only single value result multi-line will be very dirty code right but the benefit of Turner operator is Turner operator will give a very short on line Syntax for simple conditional statements okay otherwise you have to write three four lines of the code in the F's condition for writing this same thing right great now the third type of conditional statement is this switch statement and and here is the sample code let's understand using a switch case is a good idea if this variable day value is a single value okay single value and it has consistent because you have to consistently compare it with these cases consistent mean like this is here two only it should not be like changing for example in the if else block if the left side can anything if is different then Els if is different left side then many El if it will be different but in switch case the left side value is always be the same the day value will be the same right side one two that can be different all right the second point is that switch case can efficiently manage multi-line executions also like the fs but Turner operators cannot finally the conclusion is switch Cas is better for same left side values all right and the benefit of switch case statement is it looks more cleaner and more readable and more structured as comparison to the fs condition so basically you can do the same thing using a FSE condition but you can see the difference you know switch case looks more is structured right and also switch cases are generally used more with enums which I will definitely explain in the upcoming question but yes that is the comparison and now you know when to use what kind of conditional statement in your application at what moment right what is a string Builder what is the difference between a string and a string Builder this is a very important question from interview point of view I will explain a string Builder by comparing it with string okay see both and string and string Builder are inbuilt classes provided by Java now I will show you the code side by side and show you the differences here is the code of the string class okay and here what we are doing we have a variable string literal Str Str then we are modifying Ying this St Str by adding the St Str plus happy so basically we are adding some text to the existing string okay now the value of the Str Str will be interview happy okay similarly on the right side we have the string Builder example here string Builder object s SP first we are assigning it to interview and then to this object we are appending or adding happy Means again the final value of the SP object will be interview happy now the question is what is the difference okay the difference is a string is immutable and a string Builder is mutable so what is immutable and mutable see string is immutable because its value can't be changed now from outside you will say that okay the value has been changed right but but but internally inside the Java memory the story is different here if you see inside the memory the first variable St Str has a different location okay and the second variable St Str will be have a different location all right meaning every time you modify this s Str very variable inside the memory a new variable variable will be created okay so that also means that once a string is created it cannot be changed and that's why we call string is immutable they can't be changed all right now let's see how the string Builder is mutable now see string also this SBI object is also modifying the existing object right but but here if you see in the memory the variable present at the same location only the value is updated to the new value interview happy no new object is created no separate location is required so that is why string Builder is mutable mutable means things which can be modified okay so that is the difference between them and whatever I have explained I have written here in the very simple terms so that later you can easily revise and now in the upcoming question I will explain when to use a string and when to use a string builder in your real real applications okay what is an array why do we need arrays in real applications so why we need arrays see in application one very important task is to manage and hand handle lots of lots of data suppose this is the data in your application okay but this data is unstructured because normally we have to hold each data and every piece of data in multiple variables okay some are integer types double types and different data types are there now to make this data is structured and well organized like this Java provide us with a data type called arrays see like this so now listen carefully with the help of the arrays we can group The related data together okay the related data will be in a group in a single array so now there are only four or five arrays which are enough to maintain or organize a huge amount of data right so this is well managed and well organized way okay so that is the benefit of array also and yes you can put one category of data in one array for example this is the array example of the arrays this fruits this is a category now in the array elements you will be have apple banana orange and the array index will be 012 so what is array index index is like the location of the element in an array okay index generally starts from zero and then it is 1 2 3 4 and so on depending on the number of elements you have in a single array okay so great that is the brief idea of an array and finally here is the definition an array is a data type that allows you to store multiple values in a single variable okay okay and here is the use of array array are used to store related data in a structured way array also enables sequential access via indexes okay now in upcoming question I will show you how to declare initialize and Implement array which is very simple great and congratulations on completing the second chapter where there is a will there is a way believe me or ask any experienced person in interviews are the best way of learning and best way of earning also okay so now in this CH third chapter we will cover questions about oops main Concepts classes objects access specifier this keywords and getter Setter methods basically all the members of a class okay so let's let's start with the first question what is oops what are the main concepts of oops so by using oops concept we can create Clean and Clear structure for the softwares and web applications okay now the question is what are those concepts of oops and how they provide clear structure so here are the oops Concepts first is the object then classes then inheritance then polymorphism then abstraction and encapsulation so oops stands for objectoriented programming and objectoriented means it is a way to create software application around the objects only okay in the upcoming question we will start with the object and then we will cover all these oops Concepts one by one in separate questions and most important we will explore when to use these Concepts in real applications okay in which situation why they are so important okay before that here is the definition of oops oops is stand for object-oriented Pro programming which means oops is a way to create software around objects second point is oops provide a clear structure for software and web application okay I'm writing it here in the book so that later you can easily and quickly revise it it from here what are access specifiers what are the public and private specified again a very important question here is the table and we have four types of access specifiers public protected default and private right now I will explain you about the only the public and private only and default and protected in upcoming question so let's first see the code example quickly here we have the student class with two two Fields one is marked as public which is name and one is marked as private which is City Field okay remember access specifi can be used with Fields class names functions and Constructors also okay so now I will try to access these fields from other class so we have a science student class here and in inside that we have a get student function okay to access the fields of student class first we will create the object of a student class student object okay now listen carefully when I will try to access and print the public field which is name of a student class via the student object then that will work completely fine and that is aible accessible because public members are accessible inside their own class and even outside their own class okay outside the class okay but now I will try to access the private field city of the student class by this student object and this is showing the error you can see the red underline here that is the compile time error and because it is not accessible okay private fields are not accessible outside the class okay so outside the student class you cannot access this city field okay all right great that is the whole story now the same code I have pasted here also and here is the definition of access specifier access specifier are keywords used to set the access level for classes variables or Fields methods and and construct Constructors now if you see in this table then you can see that public members are accessible within their own class and in out within the package and within the subass everywhere everywhere what is subass that I will tell you later but public members are accessible everywhere whereas the private field is only and only accessible within the same class not everywhere not anywhere else all right that is the answer now in the upcoming question I will explain you the default access specifier which is very interesting what is the role of this keyword in Java when should we use it very important question and by the end of this video I will clear all your confusion let's understand with the help of the code here is a employee class and inside that we will first declare the private field experience of the employee okay e stands for experience now then in our main method we will create the object of the employee class which is EMP and then by using this EMP object we can set the value of this experience something like this EMP do experience equal to 10 but but but this way of setting X field value is not recommended because it violates encapsulation concept of oops I will explain you later in a separate question how it violates but but here let's focus on this keyword okay so first we will comment this approach okay and instead of directly assigning the field value like this we will call the setter method of this employee op object this set experience is the setter method and we will pass the value as the argument here okay then we will Define this Setter method like this above in the employee class okay and here in the parameter we will receive the argument which we are passing from here all right clear now inside this Setter method we will set the field this we will write this code this. experience equal to experience basically setting the field value okay now listen very carefully here this to experience will rep this to experience will represent this class field value okay because this keyword refers to the current instance of the class meaning if you put this keyword dot in front of anything that point to the current class fields okay now you may ask why we are using this keyword and not directly using just the experience field the reason here is we are receiving the experience in parameter also right so this experience is different from the class field above experience all right this experience parameter we are receiving from here okay so that's why why we use this T experience to point to the class field now this seter method purposes is to class uh set this class field experience value which is received from the parameter experience that's how we set the class field values by using these Setter methods and not by directly assigning them like this got it now finally we can then print the value EMP do experience okay and yes getting experience like this is getting we are getting experience and this is also not recommended and in the upcoming question I will separately cover the getter method also quickly but for now we are getting like this only and now if I run the application then here at the bottom we will get the 10 as the result as the employee experience all right now you know the role of this keyword now the same code I have pasted in my book also like this and I have pointed out how this do experience is pointing to the class field experience okay so that later when you revising you should immediately get the idea finally here is the definition of this keyword this keyword refers to the current denance of the class and here is the use of this keyword this keyword differentiates between instance class field variables and and and the parameter which is received from the you know from the main method so that differentiate this key differentiate between them okay otherwise if you will here write exp equal to exp then both will be like parameters so that will be confusing that's why this to experience for setting this experience referring this class field because this experience class field belongs to this employee class all right that's the advant that's the purpose of the this keyword what are Gutter and Setter methods I already explained Setter method but if you missed that then let's quickly conclud conclude and then explore the getter methods okay let's go to the code so here is our employee class and first we will declare one experience integer field private field like this here exp stands for experience then we have the main method okay and inside that we are creating the object of the employee class the object name is EMP okay then we can set the field value something like this but as I told you this is not recommended okay it violates the encapsulation concept of oops which I will explain later to set the value of above experience field first we will call the set experience method like this from the object and then we will pass the experience as an argument here now we will Define this set experience method something like this inside the employee class okay sorry so that is called the setter method and here in the brackets it will receive the parameter which is passed from here and then inside this do experience equal to experience we'll set the field value this okay from this parameter value which I already explained when I explained the purpose of the this keyword all right now similarly to get the value of this now until now we have just setting the value of the experience field now to get the value we will Define a method get experience like this which is the getter method all right and then from here we are returning the experience which we already set by using the set experience method all right so when we are returning to the main method we can call the employee. getet experience here and this will receive the experience which was written from the get experience method above all right if I run the program then you will receive the five in the output basically this will so rather than directly EMP do experience rather than directly doing this basically we have to use the getter method get experience method so that is the story of the getter and Setter Brothers now in the code I have mentioned all the code and then I have written the definition of the getter methods which are used to retrieve the values of private field of a class Setter method used to modify or set the value of private field of a class now you may ask why we are doing all this complex stuffff why we can't set Fields directly and yes I will answer this basically the advantages of using the getter and Setter methods in the upcoming question okay all right if you are here that means you are serious about Java interviews in the future remembers interviews are not like exams which happens once a year and you are failing or something like that you are you will not get another chance so there are thousands of companies and you are missing them if you are not giving interviews okay so just start giving interviews all right so in this chapter we will cover questions on inheritance types of inheritance multiple inheritance types of polymorphism and finally what is the difference between overloading and overriding okay so let's start with the first question what is inheritance and when to use inheritance in real applications very important and very frequently used Concept in real applications Let's Learn by one real example suppose one company wants to create one employee management system application like this okay now the company has two types of employees one is permanent employees and temporary employees for that in the application they have created two classes one is permanent employee class with Fields experience and functions get experience set experience calculate salary and do fun okay and another class is for temporary employees temporary employee class field experience some functions are there which is similar to permanent employee class but this do work is a different function okay so only few functions are different and most of the functions you can see are same in permanent employee and temporary employee class so great now the question is why to have the duplicate functions with similar code in multiple classes all right so to REM remove these duplicate functions from these classes we will use the concept of inheritance now listen carefully like this a base class employee class will be created and all the common fields like experience was common and the function common function get experience set experience and calculate salary so all these common functions will be placed in this parent class employee now definitely I will show you in the upcoming question the real code but let's understand the concept so after setting up this uh employee class we will set the parent child relationship between the class uh parent and child class now the child class will automatically get and inherit the fields and functions of the Base Class okay like like a rich kid automatically inherit money from his Rich parent right even without having it so then you can remove the written functions from both of the child classes the duplicate function because it is already present in their Base Class and uh yes remember base super parent class are the same thing similarly child derived and subass are the same thing now what will happen when you will create the object of these child classes then you will automatically get the functions of their base classes also even the those functions are not present here in the child classes and that is the use of inheritance okay this will enable the code reusability duplicate code is not required in multiple classes great now let's conclude the definition of inheritance inheritance is creating a parent child relationship between two classes where child class will automatically get the properties and method of parent class okay when should inheritance be used in application the answer is for code reusability purpose multiple subclasses can reuse fields and methods from a super class or a base class so that is inheritance at conceptual level and and in the upcoming question I will show you the implementation of this what are the different types of inheritance when to use what this is a very important question from interview point of view so let's explore it quickly the first type of inheritance is single inheritance where you have you will be having one parent in class and only one child class single Okay so so here is the code see we have one parent one class and in the child one class we are extending the parent one class okay this is normal type of inheritance which we already know the next type of inheritance is multi level inheritance and here is the illustration where we have one parent one class then one child and after that we again have one child to class which is like a grandchild okay and here is the code implementation see one parent one class child one is extending parent one and child two is extending child one okay so like this so that is how multi-level inheritance is and here is very one important thing here this child two class which will which is like a grandchild will automatically get all the methods of the child one yes right and even the parent one also okay that is the important thing thing so meaning child to class have no need to extend the parent one class separately okay that is the thing now the third type of inheritance is hierarchal inheritance and here is the illustration where we have one parent one class and which class is inherited in this multiple child classes child one child two child three multiple can be okay this is the most used and the most popular type of inheritance for reusability purpose all right so here is the code here is the parent one class which is inherited both in the child one parent one and then child two also parent one only now see one so one function in parent one will be reused in multiple child classes that is the benefit after this we have one Hybrid inheritance also which is like the combination of multi-level and hierarchical inheritance only great I think now you know the types of inheritance in Java and yes there is one more type of inheritance which we call multiple inheritance not multiple level multiple but Java does not support multiple inheritance now why Java don't allow multiple inheritance that we will cover in upcoming question what is multiple inheritance does Java support it very interesting question here you can see the diagram of multiple inheritance here this child class can inheriting more trying to inheriting more than one base classes parent classes okay but but but this is not allowed in Java a child class cannot extend or inherit more than one Base Class here is the code implementation of this same example two parent classes parent class one parent Class 2 and one child class which is trying to extend parent class one comma parent Class 2 and this will if you will try this code then it will show you the compile time error okay okay you cannot put here more than one classes second class is not allowed okay now the question is why does not Java support multiple inheritance what is the problem with this approach okay that we will check out in upcoming question what is the alternative to achieve multiple inheritance in Java this is the ultimate question asked in many interviews and the answer is here here in this diagram okay Java supports multiple inheritance of interfaces meaning at maximum only one parent class is accepted in the for the child class but you can add multiple inheritance 1 2 3 4 any numbers uh you can say you can Implement for interfaces we use the word Implement and for classes we use the word extend okay so let me quickly show you the code also so suppose you have a parent class like this okay so here we have a parent class and we have a parent method inside it then we have two interfaces they are separate okay you can create different files for them and inside the interfaces we only declare the methods if you are new to interfaces I will cover them separately then you can revisit this question okay now in the child class like this is the child class you can extend only and only one parent class like this but you can Implement multiple in interfaces interfaces one two like this so this is not exactly the multiple inheritance but it's like an alternative to multiple inheritance okay and yes as the rule of the interfaces inside the class child class you have you have to implement them okay implement the methods which are just declared here you have to implement them in the derived class okay great so that is the logic and that is the uh alternative way to implement the uh multiple inheritance via the interfaces in Java great so now all the code I have pasted here in the book also so like this I have pointed out how the multiple interfaces can be implemented and here I have also written the conclusion Java support multiple inheritance of interfaces so multiple interfaces and maximum one parent class can be inherited that's it what are the types of polymorphism very important question if you remember polymorphism means having multiple forms of the same name function like this right now we have two types of polymorphism the first one is the compile time polymorphism and second is the runtime polymorphism okay compile time polymorphism is again of two types method overloading and operator overloading now here this example which I explained here there this is the example of the method overloading okay this calculator class single class and inside we have multiple functions with same name that is overloading Method okay and then we have the operator overloading which means that operators like plus minus divide they can also have the different forms but that is possible in C++ operator overloading in Java operator you cannot overload the operator okay then inside the runtime polymorphism we have the method overwriting as its type now here is one suggestion initially when I started my career I was not aware of these compile time and runtime words so I was just say that method overloading and Method overwriting are the types of polymorphism and that is also like 90% correct answer but now we I will I will cover all of these things like what why what is compile time what is run time why method overloading is compile time and what is static early binding and late binding so that all I will cover in the upcoming questions okay what are the five differences between overloading and overriding very very important question asked in many interviews and that's why I am asking five differences not one or two differences they are not enough because if you you know a question is important then prepare as much as you can right okay so here first I will show you the side by side code here is the code of the method overlo loading which I already explained and here is the code of method overloading Base Class and here is the child class of it okay now I will point the differences here and in the next slide all differences are written the first and the most important difference is that Ma in method overloading multiple methods with the same name are there but they are present in the same and single class yes only one class is required for method overloading okay whereas in method overriding if you see method names are same okay and their signature is also same but they are present in different class so basically we overwrite the parents class me method in child class and in overloading so that we have only one class second difference is that in overloading methods have the same name but the parameter signature is different okay okay meaning either the parameter type is different or the number of the parameters are different right but in method overlo loading overriding sorry the method signature and name both must be same okay nothing should be different then the third difference is method overriding overloading is compile time polymorphism since it is identified at compile time but method overriding is runtime polymorphism because it is identified at runtime or this I explained in previous question next difference is Method overloading you do not need any annotation or any special keyword right whereas in method overriding this override annotation and keyword you require okay in the child class above the overridden method so basically it is overriding the super class or Base Class method last difference is that Constructors of the classes can be overloaded okay multiple Constructors can be overloaded whereas Constructors of the classes Base Class cannot be overridden in the derived class and yes Constructors I will explain in upcoming questions separately that is slightly more advanced this last difference but yes these are the difference and all these same differences I have written here in the next slide of the book understanding the concept is very important but but when a question is very important then we should also remember some Theory okay this information in this book will help you in doing the quick and fastest revisions for your interviews and at least you will remember three to four differences at least I recommend that great if you are here in this last chapter it means you have the spirit and my only advice to you for the interviews is the best advice keep giving interviews and never ever give up in this chapter we will explore the questions on encapsulation and abstraction and then what is the difference between them and finally what is the difference between abstract class and interface when to use what so let's start with the first question what is encapsulation let me show you a very good example you know the capsule medicine okay which contains two colors and two different medicines inside it like this right to cure some disease you must take both these medicines together together and therefore the capsules are invented if you only take one medicine then there can be the side effects right similarly to create a good software or web application encapsulation is a combination of function and data or you can say Fields okay meaning if you let the client or some other method access the data directly then your application security can be in danger your data can be in danger okay you must pass the data by first combining it with a function and then pass it okay in other words if you have a class with fields and function inside it so do not let the fields accessible outside the class directly rather use the getter and Setter functions to wrap the field or data within the function and and then only allow the access of the fields by the functions to the outside functions classes client anything finally here is the definition simplest definition encapsulation is the bundling of data fields and functions into a single unit okay single unit great now in upcoming question I will show you how to implement in caps solation in code how to achieve encapsulation or Implement encapsulation in Java let me show you the code example and yes if you already have understanding of getter and Setter methods then you already know 70% of the answer okay now suppose you have a employee class like this and inside that you have the experience field which is also called Data both are same thing okay like this now inside the main method or in any other class when you create the object of this employee class like this you can create object okay this EMP is the object then you should not set the experience field above field inside there like this EMP object name do field name equal to so this is directly setting which is not recommended okay first first you have to make sure your experience field your field is should be private so that your field is not accessible outside this class to make it secure then Define a Setter method like this to set the above experience field with the value which you are receiving in the parameter here using this this keyword okay so this is Setter method then you have to use the getter method this is getter method to get the value of the field experience okay now from the main method to set the value of the field like this you can do you can call the setter method and here you have to pass the experience in the paramet argument which will received here in the parameter and then your experience field is set okay after setting the field you will use the getter method like this to get the field value okay this will'll call the getter method here which will return return the field value experience which you can also manipulate that's why that's an example okay so that is how you can use the private field and the getter Setter methods to implement encapsulation in your programs okay application and yes now the same code I have here mentioned the same code it is okay and the same answer I have written here also encapsulation can be achieved by using access modifier private so that your field can be private and then Getters and Setters methods okay great now in the upcoming question I will quickly show you some advantages of implementing encapsulation in your project what is abstraction how do you implement abstraction so let me quickly explain with the help of an example suppose you are a web developer you created one web application okay you have the functionality and the code right code is providing the functionality now Suppose there is a end user of your application which is accessing your application and using it now here you notice users are only aware about the functionality okay they are not aware about the code and especially if that is the backend code and that is a very excellent thing right why to show the complex code to the user and if you are showing it's not a security issue also it's if it is so because if user will know the code then they might try to hack it okay so so hiding these background details or complex implementation is called abstraction it's a it's a concept okay it's a general concepts applicable any anywhere okay so here is the definition of abstraction abstraction means showing only required things and hiding the background details all right and as I said it's a very general thing so it is applied outside also for example here here is another example here a car driver knows how to drive the car right but the complex settings of the engine is hidden from the driver car driver do not need to know the complex working of the engine to just to drive the car all right so that is the benefit of abstraction it makes things simple to for end user or for anyone so now listen carefully the ultimate benefit of the uh abstraction is bying complex implementation abstraction makes things simpler for end users that's it abstraction is a concept now the question is how to implement abstraction and here is the answer mostly in coding we use abstract classes and interfaces for implementing abstraction great now in the upcoming questions I will tell you what are abstract class interfaces what are is the difference between them and what is the difference between abstraction and encapsulation because there is some confusion in between them okay so that I will explain in upcoming questions what is the difference between abstraction and encapsulation most interviewer ask differences question because by differences question they can check your two concepts okay so let's see three differences here between them first difference is just the definition abstraction means showing only required things and hiding the background details whereas encapsulation means it's the bundling of data and functions into a single unit so that is the first difference between them second difference is abstraction simplifies complexity by hiding unnecessary details whereas it's kind of Advantage okay whereas encapsulation protects the data by restricting it access only via the function so that is the second difference and encapsulation provide a concept data hiding but remember data hiding is just about the data whereas when we say complex acting by hiding unnecessary details in abstraction so that can be anything that can be function that can be any anything it's a concept okay where encapsulation is just about data heading okay next differenes abstraction can be achieved by abstract classes and interfaces right whereas encapsu encapsulation can be achieved by access modifiers and getter Setter methods I already explain all these points so basically the first is definition Second is the benefit third is the implementation all all right great so these are the differences between abstraction and encapsulation now in the upcoming question we will explore about the abstract class and interfaces which is again a very very important question from interview point of view okay what are the differences between an abstract class and an interface at least four differences very important question that's why four okay here I will show you the code side by side then show you the difference one by one on the left side we have one interface with all abstract methods and then we are implementing that interface down here okay and then on the right side we have the code of the abstract class and with one concrete method courses and one abstract method and then we are just extending that abstract class has here all right now let's see the differences between them the simplest difference is interfaces are defined by using the interface keyword okay whereas abstract classes are defined by using the abstract keyword don't me miss this kind of simple differences they are very easy to say right second difference is that interface supports multiple inheritance yes here you can put multiple interfaces you can Implement multiple interfaces but abstract class does not support multiple inheritance you cannot extend more than one Base Class abstract class okay so that is the difference next difference is in interfaces methods are implicitly abstract what does it mean is here you do not have to write the abstract keyword in the interfaces okay no keyword is required whereas in abstract classes you have to use the abstract keyword to Mark a to make a method abstract okay next difference is interfaces cannot have Constructors right whereas an abstract class can have the Constructor yes it is possible in abstract class now you may ask when we can't create the object of the interfaces and abstract class then why do we need Constructors for abstract class so that I will explain in upcoming questions in Constructors maybe but yes I will explain and the last difference is interfaces can have default methods and Abstract glass cannot have default methods again default methods I will will explain in the shortly in the upcoming question okay great these are the five or six differences I think and in the next slide I have written all of them in the book what is the similarity and then what are the five differences and uh that will be then very easier for you to revise later when to use the interface and when the abstract classes in real application so this is like a revision question a very theoretical question I'm am just concluding some points here no code and mostly I already explained okay just a revision so when should we use interfaces and here are the top three reason an interface can be used to define contracts with the implementing class must implement the methods the contracts okay we make classes more modular and structured your project actually interface is a good choice when you know you have methods to be there but it can be implemented differently by different classes okay third reason when to use interface interface do separation of concerns and because of that unit testing is very much simpler so mocking objects all these things are very easy if you have interfaces all right now when to use abstract classes and here is the reason abstract class is a good choice when you are sure some methods are concrete defined and must be implemented in the same way in all derived classes and some methods are abstract and can be implemented differently by implementing by implementing them in the classes so as you can see here interface have more and more advantages therefore normally mostly we prefer interfaces because they give us the flexibility uh to modify the behavior the methods at the later stage okay and they also provide complete abstraction and also they help in unit testing so that is the answer to this question and I hope now you have clear about what is what our interfaces and what are all the things about it okay so great and in upcoming question I will show you what is the difference between abstraction and abstract class which you might be already knowing also all right if you reached here and completed all the questions and answers then great and my best wishes are with you first thing just po post your resume and start giving interviews be ready for rejection because that is the part of the interview Journey okay and don't forget to like And subscribe and yes if you are interested in the complete interview course or or this interview PDF book then links are in the description finally again all the best and never ever give up