Session 14- Java OOPS Concepts - Inheritance and Types of Inheritance in Java

SDET- QA10,110 words

Full Transcript

so in our previous class uh we have seen how to 

use static keyword and also you have understood what is system. out. pellin and also you have 

understood about main method so what is public static void main okay so main is also uh just 

like a method how we create a normal methods in the class so normally the class is also contains 

a main method so jvm is always look for the main method so wherever in whichever class is having 

main method and jvm will look for that particular method and it will execute your class and just 

like other methods main method also will take parameters and the main method won't return return 

any value because it is a void and it is a static method okay so in the last class somebody asked 

how can we pass parameters to the main method okay so we'll see that and after that we'll start uh 

inheritance how to pass parameters into the main method how to pass parameters to main method okay because 

the main method uh will run at the run time right once you executed your program and 

jvm will start execution from the main method so if you want to pass parameters to the main 

method only at the run time it is possible if it is a normal method we will create an object 

and through the object we can pass we will just call that method and we'll pass the parameters 

right if it is a normal method but main method is not like that main method will invoke at 

the run time so we have to pass parameters so let me show you a simple example how can we 

uh take parameters to main method so not much important but just for your understanding 

I'm just showing one example you can just take a look at here uh let me create a new 

package for today and today is day 14 take finish okay now inside this I'm creating a new 

class okay I'm just saying passing params passing parameters or params to main method and I'm taking 

main method Also let's say finish just like other method main is also one of the method in the class 

but main is a special type of a method uh in Java okay this is a main method so normally when you 

write something here I'm just say system. out. pintn I'm just writing simple message right so 

this is how we can simply write one main method and when you run this run as a Java application so 

the main method will be called and the statements which are present inside the main method will be 

executed okay this we already know so now what I will do is here this is a parameter so whenever 

you receive a parameter into method we can use those parameter these are the local right this 

is a string array this is basically string type of an array so now what I will do is inside this 

method I will try to use this parameters l I will pass this okay so what I will do is in this this 

is a basically an array arcs is a string type of an array means what the AR is a variable which 

contains a multiple strings how it will internally create the array this is string array as I said 

right string array means what this is array and uh this is an array and which contains multiple parts 

we don't know how many locations are there because we are just passing only array let's say this is 

one string 0 1 2 3 index will start from zero so these are the different strings so let's say hello 

and welcome Java selenium we can store multiple strings so this is array single Dimension array 

of a string type ARS is just a array name ARS is a just an array name you can put any name you can 

put XA whatever you want okay ARS is just an array name now what I will do is I can change this name 

also if you are confusing you can put this a also like this or you can put like this also a single 

dimensional array that is also correct all these syntaxes are correct okay or else you can say 

string arcs and bracket this is also correct so now we will receive an array which contains a 

number of strings now inside this main method I will try to use those arguments okay for example 

I want to find number of parameter or number of values in this array how to find number of values 

or number of elements in Array if it is a single Dimension array how to find number of values in 

Array yes length is a keyword which we can use to find length of an array now once you receive 

this string array how can we find the length of this array how many parameters are there how 

many values we have in this ARS do length so this will give you how many parameters so let me 

just try to print this arcs do length Okay now if I execute this method observe very carefully so 

I'm just executing this method just like a normal run as Java application zero you will not get 

anything why because still we have not passed any parameters to the main meth method we are not 

passing any parameters to the main method so by default ARS doesn't have any data so ar. length 

is returning zero ar. length is returning zero now whenever you pass this array then only it 

will find number of values right so how to pass parameters to this main method if you want to pass 

main parameters to the main method through eclipse what we can do is normally through command line 

we can pass but here we are using and an editor so how to pass it is you can simply say in run 

as you can see run as Java application right instead of run as Java application just choose 

this option run configurations okay instead of directly run Java application if you run Java 

application you won't get zero because we are not passed this parameter so if you want 

to use this option run configurations in the Run configurations you can see one tab here 

called arguments here you can see arguments tag okay here you have to pass parameters so I'm 

passing multiple strings here okay just look at this welcome is one string space two another 

string space Java another string space selenium so how many strings I have passed after space it 

is considering another string okay totally four strings I have passed welcome to Java selum four 

strings so once you specified the multiple strings here just simply say run now observe it is saying 

four how many strings we have passed four strings we have passed so it is giving four now just I 

want to print those strings for example not only length I want to print those strings whatever 

data we have provided at the run time I want to just want to print them so what we can do is you 

can let me write one simple for Loop for and I'm just trying to write write an enhanced follow so 

from the ARs array I'm going to take value string value one by one all are strings okay and then 

I'm just printing the value okay from this AR array I'm reading each individual string value 

into this variable and then I'm just trying to print this by using enhanced for okay now when I 

run as run configurations so again go to argument and you can change this value so this time I'm 

passing something else hello world okay now I'm just passing only two parameters hello world 

just execute run see here you can see number of arguments are two so number of values in Array 

is two that means length of array is two and these are the two parameters which we passed so the 

main method is receiving those parameters here and again we are using we are finding length of an 

array and also we are able to read the data from this array and you can also do perform different 

operations on these particular values which are read from this array okay so this is the way we 

can pass parameters into main method in Eclipse okay so don't directly run it just run as a run 

configurations here we can pass the parameters and these parameters are fixed until unless you 

change these parameters these parameters still available okay if you again run the program 

just like a normal Java program observe this normal Java program you can see you still get 

the output because what we have already set parameters in the Run configurations so once you 

have seen this run as a run configurations you can see parameters fixed here until unless you 

change this still these parameters are available okay so that is the reason even after passing 

these parameters you can close this and now we can just run as a normal Java application you 

will see those parameter values main method is taking the parameters and if you want to change 

again go to run configurations and here go to arguments and here you can change you can say 1 2 

3 4 5 6 two parameters now it will also take as a string okay in you can just directly click on run 

button or you can simply say apply and close now we can just run as a normal Java application see 

now you will get it strings 1 2 3 4 5 also in the format of strings it is taken so this is how we 

can pass parameters you can just go run as run configurations arguments and here welcome to Java 

like this you can pass parameters you can directly run from here or else you can say apply and close 

now we can run as a normal Java application like this okay so this is how we can pass parameters 

into main method this is not important okay which are not going to pass anything in the 

main method but just for your information I'm just showing this understood everyone how to 

pass parameters to the main method so main is also just like a normal method which will take 

parameters and we receive the parameters and we can perform the operations okay yes now let let 

us uh move on to the next concept most important inheritance inheritance inheritance is a one 

of the uh object oriented programming concept so far what are the concepts we have seen so far 

in objectoriented programming so far what we have covered class object polymorphism encapsulation 

right so these are the four topics so far we covered now this is inheritance so let us 

see what is inheritance and what are the main advantage of inheritance and how what are 

the different types of inheritances and how to implement so inheritance is nothing but acquiring 

all the properties and behavior from one class to another class is called inheritance as for 

the definition java definition inheritance is nothing but acquiring all the properties 

and behavior from one class to another class properties means variables Behavior means 

methods so normally what class contains class contains a variables and methods so 

whatever variables and methods we created in one class we can get them into another class 

automatically or one class can acquire another class variables and behavior which is called 

inheritance okay for example uh let me take this okay so for example let's say I have one 

class so this is one class and uh let's say developer one is created Class A and he's created 

some variables and he's created some methods in the class okay now there is another developer 

there is another developer he implemented another class called a b and the b class is also 

having some variable b class is also having some different methods and now A and B classes are 

totally different so whatever variables methods created in the a class are different than b class 

is created by developer one this is created by developer two okay now while implementing the 

Class B let's say I want to uh use I want I need same number of variables same methods which are 

already created in another class called a suppose whatever variables and methods are created in a 

class while creating the b class we want to again a need of creating same variable same methods 

in the class B again in that particular case what will happen is instead of creating same 

variables same methods again one more time in B we can simply get all of them into B Class okay 

we can extend everything from A to B so this is called acquiring process so acquiring all the 

variables and methods from one class to another class is called inheritance and by using extens 

keyword we can get like this so whatever the the whatever the variables and methods are there in 

the the a class which will extend into b class in such case a is called parent Class B is called 

child class and this is also called as Base Class also called as super class and which is called 

child class or which is also called sub class or which is also called derived class different 

names are there we can call them as a parent and child classes or we can call them as a base class 

and sub class or we can call them as a super class and derived class so different names are there so 

most of the times we use this terminology parent and child class okay so whatever the variables 

and methods we created in one class we can acquire into another class and in that particular case 

a is called as a parent class whichever class is extending the parent class which is called child 

class okay now when you create an object of Class A we can access the variables and methods from 

the class A itself when I create an object of Class B I can access variables and methods from 

the class B as well as Class A why because B is already extended everything from a suppose in 

a I created one variable called X now B already extended that so B is also contains X suppose in 

a I created one method called M1 so B is already extended a so B is also having M1 method so when 

I create an object of B I can access everything from B plus a but when I create an object of a 

I can access only variables matter from a class this concept is called inheritance acquiring all 

the properties from parent class to child class is called inheritance concept it is simply a 

parent and child relationship it will follow parent and child relationship so in the parent and 

child relationship for example parents are having some properties okay some properties parent 

is having so after the parent automatically it will come to the child so same properties 

belongs to the child or not yes so this type of concept is called inheritance whatever the 

variables and methods which are created in the parent class after extending into child class 

the same variables and methods also belongs to child class so this is a concept of inheritance 

acquiring all the properties and behavior from one class to another class is called inheritance 

what is an advantage why we can do like this there are two main objectives the two main 

benefits for this one is reusability second one is we can avoid the duplication the first main 

objective of inheritance is we can achieve the reusability reusability we can achieve 

the second uh usage is we can avoid duplication reusability we can achieve the uh 

reusability and we can avoid the duplication so for example how can we achieve the reusability 

and how can you achieve the duplication see for example we already created one method some 

variables and some methods in the parent class let's say this is a parent class so we can if 

once you extend this parent class in any other class we will automatically get variables 

and methods into Shale class so we no need to uh again uh create these two variables and 

methods again in the CH class once we created we can reuse multiple times we can extend those 

variables methods into any other classes so we creating one time but using multiple times 

that's called reusability and we can avoid the duplication so same variables methods 

we no need to create in every class once somebody's got created we can you reuse them or 

we can again uh get them into another class so duplication we can avoid so we can achieve the 

reusability we can avoid the dup duplication so this is the main objective these are the main 

objectives of using inheritance concept now let me show you one scenario in the real time 

how we will get advantage of inheritance what is the benefit you are getting in respect 

to time and also effort so let me take one simple scenario so that you can understand this 

simple analysis let us say there is a banking application okay so there is a banking application 

there are multiple modules are there and in the banking application let's say there is one module 

called personal loan module and there is another module called home loan module so there are 

two different features which are there in the banking application there are two modules personal 

loan Module home loan is another module these are the two different modules using the same kind of 

application okay now if I look at this I'm taking this one first personal loan is one module home 

loan is another module so two different modules okay so to develop this personal loan module or 

this particular area or this particular module or application let us say developers created let's 

say 10 different okay 10 different classes okay for developing the entire load module let us 

assume the developers are created 10 different classes okay 10 different classes are needed and 

let us assume each class is taking uh one hour of time so how much time they taken is almost 10 

hours let us assume the scenario so to develop the entire personal loan module they have created 10 

different classes and uh each class assume you can take one hour and odly 10 hours time they have 

spent to design 10 different classes they have spent 10 hours of time to personal loan module 

okay now 10 classes they have taken 10 hours of time here home loan module again they have 20 

classes they have created in home loan module to develop this home loan modu 20 classes they have 

created almost 20 hours of time they have taken okay okay 20 classes are required to design home 

loan module and 20 hours they have time they have spent this is the normal scenario without using 

inheritance there is no inheritance personal loan module is totally different home loan module is 

totally different in the personal loan 10 classes have created 10 hours of time they have taken 

in home loan module 20 classes are created 20 hours of time they have taken okay just observe 

the scenario this is without inheritance now I'm taking inheritance if I use inheritance let's 

say this is again personal loan and this is a home loan module so in inheritance what we need 

to do is there are two modules right so we have to identify the common classes because both are 

related to loan related modules definitely there should be some common thing will be there 

or common things will be there so we will identify what are common things are there in the 

personal loan and homeown module and there will be some common classes also there definitely 

there will be some common classes so what we will do we will identify some common classes 

between these two modules let us say we have identify five classes which are commonly there in 

personal loan and home loan so what you will do is we will create another class called loan and then 

we have created five different classes separately these are the five different classes separately we 

created now while implementing this personal loan module five classes we already created separately 

and those five classes we will extend and another five classes we will create here we five classes 

already we created so additionally how many classes we create here just only five classes 5 

+ 5 10 classes so how much time here it is taking only 5 hours of time only 5 hours of time because 

five class proc earlier we already created we just acquiring here so only 5 hours of time it is taken 

to develop the personal load Model come to home loan model total how many classes are required 

here 20 classes are required but five classes already there so we are just reusing it so when 

I get these five classes rest of only 15 classes we will create so how much time we create here 

only 15 hours of time we will spend so here it is reducing 5 hours of time here it is is reding 

another 5 hours of time total 10 hours of time it is got reduced when I use inheritance concept okay 

in another way we can see here 5 + 5 how how much time it is taken 10 hours here 20 20 hours total 

time is what 10 + 20 20 30 hours of time it is taken to develop the both the modules without 

inheritance 30 hours and with inheritance how much time it is taken let us assume these five uh 

classes we developed in 5 hours let's say this is taken 5 hours here it is taking 5 hours 5 + 5 

10 here it is taking 15 hours totally 25 hours that means time is got reduced or not yes so this 

is the main advantage of reusability so once you created we can reuse in multiple times yes so the 

parent class whichever we created which is having most of the times common methods and common 

variables okay common methods and sometimes you may not see any common that also fine so 

parent and class suppose in the class in the child class I want to create some variables and 

methods but they are already created somewhere else some other class if already created some 

in some other class you don't need to create again we just extend the class into CH class you 

will get them automatically okay so that is also another scenario so this is a main advantage of 

using inheritance Concept in Java we can achieve reusability and we can avoid duplication 

of creating variables and methods somebody has created something we are able to reuse it 

multiple times that is the main purpose of using inheritance concept okay in the programming 

throughout the programming this concept will follow so we don't we have to make sure the 

duplication should not be there and we should able to achieve reusability at the same time so 

these two things will be satisfied by inheritance concept so somebody's created something we should 

able to reuse it same concept is applicable in automation also in automation somebody's created 

some utility file okay and we will reuse the same utility file in all other uh project projects 

and all other Frameworks we don't create same thing in every project in every framework so there 

exactly The Inheritance come into picture so once you created something we should able to reuse 

it that we can achieve only through inheritance concept okay now so let us see what are the 

different types of inheritances are there there are mainly four types of inheritances are there 

let me keep it here four types of inheritances the first is single inheritance second is multi-level 

inheritance third is hierarchy inheritance fourth one is multiple inheritance these are the four 

types single multi-level hierarchy multiple single inheritance multi-level inheritance hierarchy 

inheritance and multiple inheritance four types of inheritances let us see what are those types of 

inheritance single inheritance single inheritance means what there is only one parent one child 

there is only one parent one child this is called single inance there is only one parent one 

child which is called single inance multi- level means one parent can I have another child this is 

having another child this is having another child so this is called multi level inheritance okay 

this is called multi levels that means what this class extend into another class again this class 

extend into another class this is class another extend another class so multiple levels we can 

create this is multi-level inheritance hierarchy inheritance one parent can have multiple CHS 

okay one parent class can acquire into class one class two class three three CHS for one 

single parent class this is called hierarchy inheritance hierarchy inheritance and then 

multi-level inheritance multiple inheritance multiple means what there are multiple parents 

having single child class this is called multiple multiple inheritance okay so these are the four 

types of inheritance single one parent only one child multi- level parent to child again child to 

another child child to another child so for this particular class this is a parent for this 

particular class this is a parent for this particular class this is a parent so like this we 

can write multiple levels hierarchy means only one parent can have multiple jail classes in multiple 

iner tance there are multiple parents are having single child class so these are the four types 

of inheritances which we have now let us try to implement this practically single multi-level 

hierarchy multiple clear now go back to Eclipse so let us see how to achieve the inheritance 

concept and we'll see all types of inheritances with one example let's take a new class and here 

I will name it as inheritance type here also I'm taking main method directly main method also 

there in the same class okay let's keep this main method uh this main class separately because 

we are dealing with the classes in inheritance we can extend the classes so the main class this is a 

different class let's keep this class one side now here I will create one class single inheritance 

one parent one child right in single inance we have only one parent one child so let me create 

one class here Class A this is one class and in the class I will create one variable let's say 

int a equal to 100 and I'm directly assigning the data into variable then I'm just trying to 

print the value of a so here I'm just printing the value of a this is the implementation of 

Class A and now one more thing you remember in one file in one Java file there will be only one 

class which is having Public Access modifier and all other classes should not have Public Access 

modifiers okay remember this if you make it as a public this will give you an error syntax error so 

only one class which is a main class should have a public and all other classes should not use 

public remember so now this is one class which is is created let us call it as a parent now I'll 

class another class B and uh the B is also having their own variables let's say int b equal to 200 

and uh void show I'm just creating one more user defined method which will print B value so now I 

have created two classes Class A and Class B and Class A and B are totally different classes 

so far there is no relation between A and B both are totally two different classes now what I 

want to do is I want to make a is a parent B is a child class that means whatever variables and 

methods are there in the class A I want to get them into class B also so how can we get them 

Class B should EX extends from class A this is the keyword we have to use extends extends okay 

Class B extends a then what will happen when you say extends a whatever variables and methods are 

belongs to class A they are also belongs to class B yes so only the main class what whatever the 

class is created main class which is having Public Access modifier okay others are normal classes 

simple thumb rule is for one Java file there is a do Java right this is one file one single 

file there will be only one public class there will be only one public class okay now come back 

to this so now I have created two classes Class A and Class B now Class B is extends Class A now 

which one is a parent which one is a child class here a is a parent Class B is a child class when 

I say extends a b is acquired everything from a so now tell me how many variables are there in B 

how many variables are there in the b class two variables okay one is B which is its own variable 

the other one is comes from a similarly how many methods are there in class B how many methods are 

there two methods one is its own method which is a show method and the other one is display method 

which comes from class A okay now come to the main class so objects we can create only in the main 

class in the main method right so we have to come to the main method so now I'm creating an object 

of Class B so let's say b b obj equal to new B now I created an object of Class B Now by using this 

object what are all things we can access we can access B we can access show along with we can also 

access a and display everything from b as well as everything from a we can access so let us try to 

access I say b obj b o BJ dot can see A and B both are showing yeah so this will get the the value of 

a so let me try to print this B oj. A we are able to access a which is actually from what parent 

class but we are accessing through child class object similarly let us try to access B this is 

actually come from B object Now by using same B obj do display and B obj do show right so we are 

able to access everything just by creating the child class object where able to access everything 

because B is a child class which is acquired everything from class A so now once we create an 

object of b class we can acquire everything from b as well we can access everything from a but when 

you create an object of a you can access only the a stuff you cannot access B you can access only 

a and display nothing else but when you create an object of Class B you will get or you will access 

everything from b as well as a so this is called single in inheritance so now when I run this as 

Java application so you will get the output so here these two statements directly printed 100 

and 200 and these are the two methods which are got printed here okay so this is how we can do 

this this is called single inheritance single inheritance now let's go to the next one 

multi-level inheritance so what is multi- level multi- level also includes a single inheritance 

the combination of multiple single inheritances comes under multile see here this is one class 

this is another class this is single if you just add one more class this becomes a multi- level 

and this is here it is a single in tense here it is a single inance again one parent one child and 

here one parent one child so if you keep adding multiple classes multiple levels this becomes 

multi-level inheritance okay now let us try to implement multi-level inheritance so now what 

I'll do is I will create another class we created a b and now I'm creating another class called C 

Class C and now C extends b c is extends B okay now once C is extends b c is a child class of b 

b is a parent class of C now C is contains their own variables let's create some variable in C 

int c equal to 300 and now I'm also creating one method called print user defined method and 

inside this I'm just printing the value of C so now this is C implementation C is already extended 

from B now tell me how many variables are there in C how many variables are there in C Class 1 2 

or three three variables okay one is C which is its own variable and other is B which is comes 

from b class and B is already having a right B is already extended a so that also we will get 

so C is equal to means what a + b b is already acquired a so B contain is everything from a so 

when I extend B into c c get everything from b as well as a okay this is called multi level you 

can consider C is a grand child of B sorry Grand child of a and child of B right so this is C so 

now we have three levels A to B and B to C this is multi-level inance now come to the main class 

and here I will create an object of C class this time comment this and now I'm creating an object 

of C Class C C obj equal to new C now I created an object of C class so what are all things I can 

access all three variables all three methods I can access with this object so now we can say 

C OJ dot C ABC all three variable I can access also I can access all the methods now what I 

will do is uh instead of assigning this data directly I will assign through the object let me 

remove these values for now INB in uh at the time of accessing the variables I will assign the data 

okay now here create a c object now can we access the variables from c obj c obj do ABC I'm able 

to access but currently there are no values in ABC can I assign the data into these variables 

can I assign the data into variables yes so let me take variable and here I'm assigning the data 

okay instead of hardcoding data in the class here I'm assing C obj dob this is actually come from 

b class but still we are able to access 200 C obj do c this is coming from CCL class here I'm 

saying 300 now I have assigned the data into the variables that means by using cclass object I'm 

able to access all three variables one is come from V one is come from c one is comes from 

B one is comes from a same thing we will also able to access all the methods from CCL class CJ 

dot what's the first method display C OJ dot show C obj do print okay so the first method actually 

comes from a class second method comes from BCL class third method come from cclass but still we 

are able to access everything from cclass object we no need to create an object of b as well as a 

we directly created an object of Class C and we able to access it so when I run this you will able 

to get the value so whatever data we have assigned here these method will display the same data this 

is called multi-level inheritance Means A to B B to C you can add in number of classes like this in 

multiple levels okay so there are two inheritances so far we have seen single and multi- level single 

means what one parent one child multi-level means multiple CH classes like this A to B B to C C2 

D like this you can increase number of classes this is called multi level now the third type of 

inheritance is hierarchy inheritance hierarchy means what one parent can have multiple 

Childs this is called hierarchy inhance one parent can have multiple child classes okay 

so let me create another example for hierarchy inheritance I'm taking new 

class I'll name it as hierarchy inheritance and also I'm taking main method say finish okay so this is the main class so 

let's keep this class one side now let us try to implement hierarchy inheritance in 

hierarchy inheritance there is only one parent can have multiple jail classes 

one parent can have a multiple jail classes so here I'm just giving some 

name called parent class name itself I'm giving is a parent see when I say 

class A can we create class A like this class A so whatever names we already used in the 

previous example we should not repeat this okay so in inheritance times we already created 

Class A right so same class we cannot create again even though it is a different file we should not 

accept because all of them all of them is part of one single package right so once you created 

a Class A here we already created a we already used this class name so again you should not use 

same name here okay remember this it will give you an error also see here what is an error it is 

giving the type A is already defined where we have defined this in another file inheritance types we 

already used these names right so we should not use so entire package the class names should be 

different same class name we cannot reuse again and again even though it is a different file 

we should not reuse okay so that's the reason we cannot use same ABC so we should give some 

different names here okay and I'm trying to give a say uh parent directly I'm calling it as a parent 

class name itself is a parent now in this in this parent class I will create one user defined method 

observe carefully vo display and this time I will also take one parameter so let's say I'm taking 

one parameter integer parameter I'm getting and here I will just print the value of a so this 

is the method which is implemented in the parent class display which will take one single parameter 

integer parameter and then it will print the value of that parameter directly I'm not defining any 

variables I'm just defining only method if you want you can create variables also no problem this 

is one parent class now in hierarchy in 10 there is only one parent can have multiple CHS now what 

I will do is I will create another class let's call it as a child one which extends from parent 

class this is one child now I'll create another child class CH two which extends from same parent 

same parent parents should be same right now child one and child two both are extended from same 

parent class now let us Implement child one so child one I will create one more method called 

show and this will also uh take one parameter B and then I'm trying to print this value called 

B implemented now CH to also I'll Implement here I will create one more method void print and it 

will also take one more parameter and here I'm just trying to print C this is hierarchy Nance one 

parent there are two Childs okay now you tell me in the child one class how many methods are there 

in the child one class how many methods are there and what are there in the child one there are 

two methods one is its own method called show other method come from parent which is display 

now child one contains two methods one is a show other one is a display now come to the child 

two Child 2 is also having two methods one is its own method called print other method come 

from parent what is that display so what is the common method in the child one and child two what 

is a common method in the child one and child two display is a common method which will come from 

same parent class okay this is implementation of hierarchy Nance one parent can have multiple 

Childs but the Childs are not related the child one is totally different than child two there 

is no relation between child one and child two but still both classes are derived from the same 

parent class okay now this is hierarchy now come to the main method so now I'm creating an object 

of child one child one C1 equal to new jail one so now I created an object of jail one so by using 

this object C1 water all method I can access show and display so these are the two methods I can 

access so C1 dot show so when I call this method we have to pass parameters because show method is 

also expecting one integer parameter so I'm just passing it okay first let's call display method 

C1 do display this is actually comes from parent so I'm passing 100 C1 dot show this will actually 

come from Child one itself so here I'm passing 200 only two methods we can access by creating an 

object of C1 so we got A200 whatever values we are passing into these methods so it is received 

here and then printed those values same thing if I create an object of child two C2 equal to new sh 

to so now I created an object of child two class so by using this object we can access display 

which is come from parent here I'm passing 500 or you can pass 1,000 and uh C2 do print this is 

actually belongs to ch2 class here I'm passing 2000 so the parent class method we can access 

from Child one object as well as child two object because both the Childs are derived from same 

parent class okay so this is how we can Implement hierarchy inheritance hierarchy means what one 

parent can have multiple child classes so now if you summarize single inheritance means one 

parent one child multi- level means we can have multiple levels like this parent to child another 

child another child like this hierarchy means one parent multiple Childs you can have any number of 

CHS these are the three types of inheritances and one more type of inheritances which is multiple 

inheritance multiple is exactly opposite of this hierarchy means here two parents single child 

two are more parents you can have more parent but single child but in hierarch it has one parent 

multiple Childs here multiple parents single child this is called multile inance multiple is 

different multi level is different this is multi level different levels are there each class 

is having only one single parent here this class this is the only par parent for this class 

this is only one parent for this class this is only one parent if you take any class in the in 

this levels that is having only one parent class but in multiple there are multiple parents for 

one single child class this is called multiple inheritance okay but this multiple inheritance we 

cannot Implement using class class concept is not supported in multiple inherence single inherence 

multi-level inance hierarchy in we can Implement using a class concept everything is a class but in 

multiple inance we cannot Implement using a class concept we can Implement multiple inance using 

interface concept there is another concept called interface that is an alternative of class so by 

using interface concept we can achieve multiple inheritance class concept doesn't support multiple 

inheritance okay so inherit uh interface concept we will discuss in the next sessions but now 

we need to understand why multiple inen is not supported using class very very important very 

popular question in interview many people will know class doesn't support multiple inance but 

very less number of people will know why it is not supported why class concept is not supported 

multiple inheritance okay so I will tell you the reason for this listen this very carefully focus 

on this little tricky stuff but you can focus and understand why class is not supported 

multiple inherence multiple means what one parent can have sorry multiple parents can have 

a single child remember let's say these are the two parents having single child this is called 

multiple multiple iner so which is not supported using class why we will see that let us say I have 

one class here this is one class this is another class and this is the child class okay and uh C1 

C2 C3 when you say C C3 is a child class C1 is a parent again C3 is child C2 is a parent so parent 

to child we use extends here and here also we use extends how many extends we required here two 

two extends C3 extends C1 and C2 right both we need to extend so how to write the statement class 

C3 extends C1 comma C2 we have to write like this statement if you want to write you have to write 

like this because we want to extend two classes into one single child but extends keyword support 

only one class multiple classes not support okay extends keyword will support only one class 

multiple classes will not be supported this is one reason so class C3 extend C1 comma C2 so 

two classes are not support at the time only one class we can extend into CH class multiple classes 

we cannot extend again one more question why we cannot extend multiple classes so the reason is 

again this is related to again concept this is a C1 and this is a C2 and this is a C3 which is 

a child class of both parents now observe why multiple inherence is not supported using class 

let us assume in the C1 class I have one method called M1 in the C2 also I have another method 

I have method called M1 same method we can have right in two classes there may be methods which 

are having same name possibil there or not these two are not connected C1 C2 are not connected both 

are two different classes so C1 is having M1 C2 is also having M1 is there any possibility there or 

not yes we can have let us say C1 is developed by someone else C2 is developed by someone else 

so they created he is created M1 method in the C1 and another guy is creat M1 method in the C2 

class possibil is there now when you extend these two classes into one single CH class here what 

will happen which method will acquire M1 from C1 or M1 from C2 which one so here ambiguity 

issue comes this is the problem when you do multile multiple inheritance when extending two 

classes into one single CH class if there are any duplicates definitely there is an ambig problem 

will come means it doesn't know which method we have to acquire M1 from C1 class or M1 from C2 

class which one we need to acire into CH class right there is a conflict ambig issue comes that 

is a reason extends keyword doesn't support two classes only one class at a time we can extend if 

we have a duplicate methods M1 and M1 we cannot extend so there is a ambiguity problem so that's 

the reason multiple inheritance is not supported by using extend keyword now you will get one 

question here if we have a duplicate methods then the problem comes but if you don't have a 

duplicate methods it should work right we have M1 in C1 M2 in C2 so M1 from C1 it will come 

M2 from C2 it will come so after extending M1 and M2 will belongs to C3 so if you don't have 

any duplicates we should not have any problem right but still there is a problem even though 

we don't have a duplicate methods you have not created duplicate methods but still these classes 

are having some duplicate methods how come it is possible if you create a new class until unless 

you write method the class doesn't have any method right you have to create your own method 

but how come a class contains a methods without creating anything so here I created M1 method 

here I created M2 method still what I'm saying still both the classes are having some duplicate 

methods even though if you're not created still those classes are having duplicate methods how 

it is possible in Java whenever you create a new class by default that class extends from object 

class object is a root class of all the classes in Java okay object class is a root class or parent 

class of all the classes in Java okay so whenever you create a new class even though you have not 

extended this from any other classes still this class is by default acquire everything from object 

class same thing C2 also even though the C2 is not extended from any other classes still by default 

this is extended from object class so this is the default class which is there in Java which is a 

parent of every class which you created you no need to write C1 extend object C2 extend object 

you no need to write by default every class whenever you create a class even though it is is 

an empty class nothing is there but still this class is extended from object class by default 

so whatever methods are there in the object class whatever variables are there in the object class 

they will automatically comes into C1 or not yes there also comes into C2 or not yes in that case 

is there any possibility to have duplicates in C1 and C2 definitely right so whatever methods 

are there in object let's say M1 and M2 these methods also belongs to C1 these methods also 

belongs to C2 definitely there is a common methods available duplicate methods available 

and that also we cannot extend so even though you have not written duplicate methods still the 

classes are having duplicate methods because of that we cannot extend multiple classes when you 

trying to extend multiple classes into one single class ambiguity problem comes ambiguity problem 

means both are same methods but which method we have to get into child class it will be confused 

that will cause the ambiguity problem so that is the reason multiple inheritance is not supported 

using class concept we can extend only one class at a time you cannot extend multiple classes okay 

I will show you simple example you will understand just observe how the class will by default extend 

from the object class see let's take a simple test class and here taking main method just observe 

okay I'm creating one simple class here okay class sux okay class some X did I write any 

methods or variables in the class X nothing class X doesn't have anything no methods no 

variables now come to the main method I will create an object of class x x obj equal to new X 

I created an object of X class now observe very carefully when I use this object variable dot 

see here these are all methods we are getting from where we are getting these methods equals get 

class hash code notify notify all two string wait we are getting all these methods populated 

from this object but these methods are not created in X class right we have not created but 

still we are getting these methods from where we are getting them object class you can see that 

clearly is is showing also the class name also it is showing so all these methods are coming 

from what object class so even though we have not created any methods in this class X this contains 

some default methods which are coming from object class which is a parent class of every class which 

we created okay suppose observe very carefully now I'm creating another class class Y and in this 

also I'm not adding anything so let's create an object of Y why let's call it as a x obj call it 

as a y obj new y okay now if I look at here in X obj what are all methods are there equals get 

class hash code notify these methods are coming from object class by default now if I look at this 

even if you see y obj dot you can see same methods same methods from the same class object class so 

what we need to understand here is as soon as you created a new class even though if you not created 

anything in this class still the class contains certain number of methods some default methods 

and those methods are coming from what object class which is parent of every class in Java which 

is a parent of every class as soon as you created a class immediately that will extend from so that 

is the reason every class is having some duplicate methods definitely the duplicate methods will be 

there even though if if you are not created so that is a reason multiple inheritance is not 

possible in a multiple inheritance ambiguity problem comes so C1 and C2 and C3 when you extend 

whatever duplicate here it is there same duplicate we cannot extend so that's the reason extern 

keyword doesn't support two classes at a time only one class it will support so is this clear 

everyone please confirm in the chat box there are multiple questions you will get here why 

multiple inheritance is not supported in Java using class concept many people will know it 

is not supported but why it is not supported you have to understand this okay why multiple 

inheritances is not supported using Java because the classes are having duplicate methods whenever 

the classes are having duplicate methods we cannot get them into child class because we will get an 

ambig problem means what which method you want to get into child class it will confuse so that's 

the reason ambiguity problem comes even though you have not created any duplicate methods still 

ambiguity problem exist because every class is having some default methods which are duplicated 

which are comes from object class object is a parent class of every class by default you don't 

need to write extend or anything okay this is a reason for that so multiple inheritance we can 

achieve by using interface concept interface is another topic which is there in Java which is an 

alternative of the class so if you create this C1 and C2 as an interfaces this is an interface 

C1 is also an interface then we can extend them into CT we cannot extend actually we can Implement 

instead of extends we use implements so if these two parents are interfaces then we can get them 

into one single class in that way we can achieve multiple inheritance using interfaces concept we 

can achieve it but class concept if you create these two are the classes we cannot if you 

create these two are the interfaces then we can get them into child class we can achieve 

multiple inhs okay so in the next class we will discuss more about interface which is most 

important topic and once you understand what is interface how to create an interface what is the 

difference between class and interface so once you understand all these Concepts then finally we 

will see how can we achieve this multiple iner tense using interface concept Okay so tomorrow 

session we will discuss that so child should be always a class not an interface okay sometimes 

the child also can be interface okay we can create multiple interfaces also we can Implement 

all types of types like single okay multi- level and like hierarchy and multiple everything 

we can achieve by using interface concept also so tomorrow session you will understand 

understand what is the difference between extend and Implement extend means what suppose 

we already there some variables and methods here so B is extension of a actually right B is 

a followed by a b is an extension of a why because B contains everything from a plus some 

other stuff so this is the extension of a that's the reason we have to use extend keyword here 

right when you come to interface Concept in interface we have variables we have a methods 

but we should not Implement these methods in interface we cannot Implement those methods 

just we can create a method but there is no implementation we call them as abstract methods 

so this interface we have to implement by using a class so here implements keyword will come 

into picture so interface can be implemented in the child Class A Class can be extended into 

child class that is a major differ extension is different implementation is different 

extension means what the methods variable already implemented so we are just extending 

into another class this is extension but here in interface methods are not implemented they are 

abstract so if you want to implement in the child class we have to implement so that's the reason 

interface can be implemented in the child class so that is a major difference so tomorrow session 

you will get more clarity on this okay I will give you more examples you will you will able to 

understand this for now just understand only this much okay so concept is clear today's concept 

everyone what is inheritance what are the advantage of inheritance what are the types 

of inheritance why multiple inheritance is not supported by using a class okay these are the 

most important topics from inheritance and the tomorrow session I will introduce you interface 

concept what is interface how to create what is the difference between class and interface so 

once we understand once you see some examples on interface then finally we will understand 

how we can Implement multiple inheritance using interface concept class is not supported but how 

can we Implement using interface so that we will see in the tomorrow session okay so just practice 

this for today and this much is enough for today tomorrow we will continue same inheritance 

concept and inheritance again there are some more concepts are there along with interface uh 

we also have something called overriding method overriding earlier we discussed about overloading 

but in inheritance we have another concept called overring and also we have something called super 

keyword final keyword we have to discuss some more keywords and all these things are belongs 

to inheritance concept so tomorrow session we will continue that okay so I'll stop here 

for today's session tomorrow we'll continue

Need a transcript for another video?

Get free YouTube transcripts with timestamps, translation, and download options.

Transcript content is sourced from YouTube's auto-generated captions or AI transcription. All video content belongs to the original creators. Terms of Service · DMCA Contact

Session 14- Java OOPS Concepts - Inheritance and Types of...