Hello everyone.
So in the last week we discussed how construct a sound destruct those are calling gifts of inheritance.
So in this we do I want to discuss different types of inheritance.
Okay so the first one is Single inheritance.
Now what is the meaning of Single inheritance.
So it means only one parent and one child.
Okay so class be inherited glasses.
So one parent and one child vehicle and God.
Okay so only one parent and one child.
So what is this index syntax is quite familiar.
Class God then access modify it and then the name of the class which is we call.
Okay so this is the syntax for Single inheritance.
Now the second is we have multi level
multi level inheritance
okay.
So what is the meaning of multi-level inheritance.
So in multilevel inheritance we have multiple levels.
Okay.
So for example Class B inherited from Glass a glossy inherits from Class B and so on.
Okay.
So we took an example in the last session so we could class this guide class inherits from the working
class and then we have 130 class which inherits from the Carter class.
Okay so how do the properties will be accessible so well it depends upon the access modifiers you use.
Okay.
Now the third one is now the third one is hierarchal inheritance.
Okay so radical.
Inheritance so what is the meaning of identical inheritance.
So in this case we have one parent and multiple child.
Okay so only one parent will be there and there will be multiple child B C and D.
Okay now let us take an example.
So we have a weaker class car class inherits from the class bicycle class inherited from the working
class.
And similarly that class will inherit from the.
We could class.
Okay so multiple class in added the base class.
So what is this index.
So this index is quite familiar for example if we want to use track class so class truck and then public
vehicle public is the access modifier.
Okay so let's say in gold although the code is not necessary but let's see
so I want to create a class class drug.
Now this class drug inheritance tax is modified is public and it inherits from the working class.
Okay
and that's all okay.
So we can see here is Class Card which also notes from the working class.
And similarly there is a class drug which also includes some division of class.
Okay
so this was hierarchal inheritance.
Okay now let's see the next one.
So the next one is the fourth one is now this is a multi-level might be level inheritance
so what happened in multi-level inheritances so one class will inherit from multiple base classes.
Okay.
One class will inherit from multiple base class letters a b c and d a Class D so one class will inherit
from the multiple base classes.
Okay so suppose we have Teacher Teacher class and student class
and the third one is the teaching assistant.
Okay so a teaching assistant will have some properties of teacher and teaching assistant is also a student.
Okay so when a class will inherit from the multiple B's classes.
Okay so let's say in good
so I have this class multiple inheritance starts CPB sorry I have this file multiple in our TPP Let's
create two classes.
So class teacher so this class teacher has some public properties let's say the properties are name
and age.
Okay so String name and legacy and Deej also let's say there is a print function where the print inside
the brain function let's print something.
Okay so we'll see old
digit Okay now let us also create a class.
Student So we have a class.
Student now this student.
Class let's say it also has a function print.
So void print.
And now that this print a student inside the print function.
So student Okay now we have a class teaching assistant so this class teaching assistant as you can see
here this class teaching assistant it inherited from both the classes or damage it inherited from the
teacher as well as from the student class.
Okay.
And let's see the excess specified is public so class teaching assistant this index is very similar.
First we have to rate the excess modifier.
Let's say the excess modifier for the teacher class is public then coma again excess specified for the
student class.
And then the name of the student class.
Okay so this is this index for multiple inheritance.
Okay so this class teaching assistant it inherits from both teacher class and grade class.
And for both the access specified is public.
Okay.
Now what will happen if I want to create an object of their teaching assistant so teaching assistant.
Let's see the object is a the name of the object is it.
So what is happening here is okay.
So we have teacher and student.
So I have a class teacher and I have a class student and then I have a class teaching assistant it inherits
from the teacher and the student.
So
this is teaching assistant class and teaching assistant class in edits from teacher as well as it inherits
from the student.
Okay now what will happen when I write this statement when I'm creating the object of the teaching assistant
class then whose construct that will be called first.
Okay so it generates from teacher and it then added strums truant from what the classes.
So whose construct will be called first teacher or student.
Okay so the answer is teacher y teacher because I write teacher first.
Okay so because I write the name of the teacher class first.
So that's why.
First of all teacher constructor will be called then student constructively record.
And finally the teaching assistant constructor will be called when we are creating the object of the
teaching assistant.
Class okay.
Now let us try to not let us use the print function.
Okay so now let's call the print function.
So what will happen.
Okay.
So if I run this file there will be an error.
Now let us try to understand why the error will come
so what is happening it is.
So first of all this print function is accessible for teaching assistant.
Why.
Because as you can see here is print function is public.
And similarly the print function is public here and we are using the access specified public.
So teaching assistant has access to the print function.
Okay now what is happening here is we have a teaching assistant class and in their teaching assistant
class we have a teacher and we have a student and teacher class has a brain function.
Similarly student class also has a print function.
So when I am calling the print function I am telling you there will be an error and that I will be there
because there is ambiguity whose print function to call compiler will get confused.
Okay so compiler will get confused among whose print function to call Deja brain function or Australian
print function.
Okay so that's why I edit will be there.
If the print function is present in one class then there will be no idea.
Okay so suppose if the print function was not present here then obviously there will be no error obviously.
Then this print function would have been called.
Okay but what is happening it is both the teacher and the student classes.
They both have the print function exactly the same name.
That's why the compiler will get confused because there is ambiguity whose print function to call.
Okay.
So if we will run this file we will get an error and that it will be saying like like there is some
ambiguity in the brain function.
Okay so let's try to this file.
Okay.
So we are getting an error at this line and that will be something like print function present in multiple
base classes.
Okay so we have two basic classes.
Student and teacher.
So we have two base classes and the brain function is present in multiple base classes something like
this.
Okay.
Now how can we resolve this issue so to resolve this that we have to specify the class name.
Okay suppose I want to call the print function present in this to rent class.
So what I will write a dot student name of the class whose print function we have to call and the scope
resolution operator.
Okay.
Now this time I will run this file.
There will be no error and output will be student.
Okay.
This this print function will be called.
Okay so this print function will be called.
Okay let's straight on this file
so a student is getting printed.
Okay.
Now what if this teaching assistant class also has the function brain function.
Okay now what I'm telling you is now where did the teaching assistant class also has the function print
and inside the brain function.
I am writing teaching assistant Okay.
Now what will happen and let's say if I run this file then the output will be student only because it
is very clear here that we are not we want to call the brain function as truly in class.
OK so let's send this file produced protested and no doubt.
Now what if I remove the student.
Now what will happen if I do a dot print.
Will we get any added Now they're desperate to find out.
So this is our teaching assistant class so in that teaching assistant class we have teacher and it inherited
from a student.
So teacher has a print function.
Student also has a print function and teaching assistant has also has a print function.
So what happens internally is when we write it out.
So what it will do first of all we will search in the teaching assistant class and the teaching assistant
class has a function called Print.
Since the teaching of history in class has a function called print.
So we will not go to its parent classes.
Okay we will not go to its parent classes and this print function will be called and our output will
be D.
Okay.
Now what happens is in the earlier case their teaching assistant class doesn't have a print function.
So if the teaching assistant class doesn't have a brain function then we will go to it parent classes
and both the parent classes as print function.
That's why there will be an added ambiguity in it.
Okay.
So let's try to run this file and our output will be teaching assistant.
Okay so our output is teaching assistant now just like the function if we have multiple data members
with the same name then we will also get the error and that it will be like did the member present in
multiple base classes for example this teacher the teacher class has a string name property let's say
three and also has the name property.
Okay.
Now we can see here same name.
Okay.
Now if I want to let's say I want to access the name date remember.
So a node name is let's say a b c d.
Now if I had this file I will again get data and that it will be ambiguity added.
Okay.
So we are getting an added.
Now how to resolve this error.
Again we have to specify the last name.
I want to exist the name property of the teacher class.
So a dot teacher school resolution operator.
Okay.
Now let us on this file.
So now all code is working fine.
Okay.
Now let us revise everything.
So we have discussed four types of inheritance single multilevel and hierarchical all the three were
very simple.
The last one which was multiple this was a little bit tough.
Now what happens in multiple is.
Suppose you have three classes a b and c and let's say you have a class D and class D and added from
all the classes glossy class B and classy.
So this is the example of a multiple.
It is the example of multiple inheritance.
What is the problem with multiple inheritances.
So if you ever need a member or function with the same name in multiple base classes then there will
be an added.
And how does all this error.
So we have to specify the class name so d dot first of all that last name then the school resolution
operator and then the property name.
Okay so what is the problem with multiple inheritance if more than one if more than one base class.
But as a written member with the same name or the function name then they will be added ambiguity it.
Okay.
And resolve this error.
We have to specify the class name first and then the property name.
Okay.
And the second was if the properties also present.
Suppose the properties also present in class D then that property will be accessed.
Okay.
Then there is no confusion.
We can simply access this property okay.
Now there is also a fifth type of inheritance which is the hybrid inheritance which we will discuss
in the next video.
Okay.
Thank you.