you welcome to nurse technologist amateur bangarraju and this is a continuous session for our inheritance what we have been discussing earlier so in my previous video I was just talking about there are six important points to remember while working with inheritance and in the last example in the last video we have been talked about three what was point parent class constructor must be accessible to child class or else inheritance will not be possible there is a first important point I clarified you why because whenever the child class instance is created the child class constructor will
implicitly call the parent class constructor so that the parent class variables will be initialized and then they can be consumed under the child class okay so the part of this particular reason parent class constructor must be accessible to the child class this is a first rule of inheritance second in inheritance child classes can access parent classes members but a parent class can never access its child class members so there is a second important thing child class can access parent class members but parent class can never access a child class members okay so we have been
talking out according to the law children how write on the parents property but parents never have write on the children's property the same principle applies here also child classes write on parent members but parent never write on the children members third point we can initialize it a parent class variable by using its child classes instance to make it as a reference and that reference will be consuming the memory of child class instance but with that reference we cannot call any child class members so I just also showed some few images over there what is it
so parent class reference created by using the child class instance and both are accessing the same memory but in this using P we cannot call the child class members so these are the three things what we have been learning in the previous session now in this let me continue from the fourth one what are the fourth point see what the fourth point is every class every class that is defined by us are predefined in the libraries of the language has a default parentless what is that that is object class of system namespace generally when we
define a class we think like we did not inherit from any class but by default your class is inherited from object class and that object is the parent class for all the classes that are present in our base class libraries as well as each and every class what we are defining your object is a parent class so because object is a parent the members of the object class for important numbers of the object class what are the four equals get hash code get a type and a toasting you can call these methods or accesses methods
from anywhere have a look object obj is equals to a new object so what is this object object is a class and this is a root class in our system namespace now in this class if you notice in the intelligence it will display you four methods it will display four methods there what are the four methods equals get hash code get a type toasting and remember these four methods are accessible from anywhere means every class will contain these four methods why because for every class the default parent classes object because the default parent us is
object these four methods are inherited to all the classes we can watch right now you are seeing only four methods in the list and now suppose if you create the instance of the class one notice class one P is equals to new class one I am creating the instance of the class one and if you notice P is an instance of the class one and four class one parent classes object now so P dot you can see six methods why six methods two methods what we defined in class 1 plus extra four methods 4 plus
2 6 methods suppose if you create the instance of our class to allow seven methods why 7 methods for from object class to from class 1 and 1 what we defined in class 2 totally 7 methods will come into picture for you so every class will be having a default parent class and the default parent class is going to be the object class which is present the system namespace you do not require to inherit simple generally when you are compiling your code when you are compiling the code it verifies is this class inheriting from any
other class if he has no issues if no automatically that class gets inherited from object if it is no automatically that class gets inherited from object suppose think like this is your class one and this class were not inheriting from any class at the time of compilation this class one inherits from object class now class two compiling class two is class two inheriting from any class yes with class class one so no need to in it from object the reason object already inherits from sorry class one already in hex one object because class one already
in heads from object for a class two also object is apparent maybe it is a grandparent it becomes a grandparent so what is the hierarchy no object class under that class one under that class two so will be having the classes like this first object class under this we have class one under this you health class two suppose tomorrow we are defining class three inheriting from class to automatically class three will come in the hierarchy and class three units from class 2 and that is inherited from class 1 and that is inheriting from object but
every time this will be on the top this class will be always in the top if you just to see the intellisense here when you are just going to type and one more thing here you will be finding when you're typing you'll be finding two objects in this one in the lower case one in the proper case don't confuse between these two these two are one and the same this is C sharp type and this is il type all C subtypes after compilation gets converted into il types this is il type in proper case end
in lower case that is c-sharp type okay and when you are just going to select this when you're going to select this particular thing over there and if you notice you will just check it out once if you put your mouse or over there and read supports all classes in the.net framework class hierarchy and provides low-level services to derived classes this is the ultimate base class of all classes in the.net framework understand is the ultimate base class of all classes in the.net framework it is the root of it is a root of the type hierarchy
say it's a very important question introduced also what is the default parent class the default parent class is object class present inside the system namespace and because that is a default parent class the methods which are present in that class what are the methods so the methods are members of object class what are those equals get hash code get type and those strengths are accessible from anywhere or accessible from anywhere means they're available for all classes whatever class you come across in that class these four methods are by default available to you because it is
a default parent class so don't forget whenever you just asked about a question what is the default parent class for dotnet so your answer should be object class because that is the root of your type hierarchy and under that only all the classes will fall down or under that only all the classes will come down maybe there is a predefined class or maybe that is a user-defined class no matter but object will stand on the top of the hierarchy and because it stands in the top of the hierarchy these four methods are accessible from anywhere
you can call these four methods from anywhere have a look class 2c is equals to a new cluster I am creating three instances now first instances of object class second instances of class one third instances of class two here there is a method in that class what does that method get type there is a method called as get type this method returns the typeof obj ms obj switch type it returns here you can call this get type on all the things right now P dot get type and you can call it as C dot get
type like this you can just call the get type on all what it tells you it tells you this is a which type obj which type P is which type C is which type run if you notice output visas which step object which namespace system P is which type class one which namespace inheritance Project and sleeves which type class to which project inheritance project so the point here is it will tell you the fully qualified name cool full qualified name in the sentence prefixing with your namespace see our name does not have any value without
a certain name per fix we just use the surname as a prefix exactly the name of the class also requires a namespace prefix so the namespace of your class too is inheritance project the namespace of your class one is inheritance project but the namespace of the object classes system so that is what it is trying to show you system dot object inheritance project class one inheritance project dot cluster but if you understand earlier in the first point I was telling you whenever the child class instance created it will internally call its parent class constructor if
you relate that here when I create the instance of the object class it will start executing from the object class constructor only but if I could at the instance of the class one year it calls to constructors internally what are the two constructors object class constructor class one constructor when I create the instance of class 2a now it calls three constructors what are the three object class constructor class one constructor and class two constructor that is what I explained you in the first rule of the inheritance okay the same thing and trying to apply here
and tell you internally that many number of constructors will be called for you so this is the fourth important point or the fourth point every class that is defined by us or predefined in the libraries of the language as a default parent class that is object class of system namespace so the members of object class or members equals garage code get a type toasting are accessible from anywhere from anywhere you can access all these particular four members the next fifth point what we require to learn I'll tell you but before that we'll talk about a
tropic order as types of inheritance types of inheritance what these types of inheritance will tell you is it will talk about the number of parent classes a child class can have or the number of child classes a parent class shall so the types of inheritance we'll talk about the number of parent classes a child class have or the number of child classes a parent class have this is what and according to C++ according to the c-plus which because why I am Telling according to C++ is object oriented programming came into picture from C++ so according
to the C++ there are five different types of inheritance this here what are the five multi-level hierarchical hybrid and multiple and multiple so what are all this particular file I'll tell you Oh have a look on this particular image to understand them first single inheritance what is single class one and class two where class two inherits from class one and this class two has one parent class to it this is called as single items but what is multi-level multi-level is also very similar like single only but thing is when you have class one class two
one parent and one child this is single but one we have one parent and multiple child means in in decide our key linear hierarchy class one under that class two under that class three this is called as multi-level inheritance okay but very similar your single multi-level both are very similar when there are two classes they call single more than two that is going to be called as multi-level and if you come to hierarchical theological what is the difference one class as multiple children see our parents of multiple children and not only we our brothers and
sisters are also present and all of us of the children of for mother and father that is what a single parent class having multiple child classes is what we call hierarchical inheritance and the fourth one hybrid what is I breed I breed is a combination of multi-level and hierarchical a combination of multi-level and hierarchical what is this if you notice class one under that class two class three what is that hierarchical but say class one under that class 2 under the class for what is that multi-level a combination of multi-level and hierarchical is what we
call as hybrid inheritance okay and lastly multiple what is multiple one class having multiple parent classes here one parent having multiple children but here one child having multiple parents this is called multiple if you understand hybrid is a combination of three things now multi-level hierarchical and multiple you see class one under the class two class three what is that hierarchical class one under the class two under the class four what is that multi-level again class two class three under the class 4 what is this multiple the combination of multi-level hierarchical and multiple is what we
call as hybrid inheritance so like this we have different different types of inheritances that are supported for you according to C++ but I try to simplify this part clotting I am trying to simplify the things what is say is actually a single inheritance or multi-level inheritance both are very similar your having two classes three classes four classes you're calling multi-level if two are there single more than two we are calling multi-level but that does not make any difference here also class one under that class two class three but the point to understand here for class
- there is one parent for a class two there is only one parent for class three that one parent if you remove class three there that looks like single or multi level only and you watch it here also class to one parent one parent for class for two parents and here class three has two parents so I'm trying to simplify the concept like this what is it you simply there are only two types of inheritances what are those one single inheritance - multiple inheritance a simple way you can just understand it what is it if
at all a class has one immediate parent class to it we called as single inheritance and if it has more than one immediate parent class to it we call it as multiple inheritance if you clearly understand this there is no point of talking about a five a term if a class has a one immediate parent class to it we call it a single intence and if it has more than one immediate parent class we call it as multiple inheritance in the sense have a look I am calling this a single non multi-level not calling multi-level
calling single why what I said you if a class has one immediate parent class see how many immediate parent classes class two as one how many immediate parent classes class three has a one only immediate parent so for class three immediate parent is class two for class two immediate parent is class one so this is single more than one immediate we call it as multiple if you simply follow this particular definition there is no point of confusing with single multi-level and all these part clotting one immediate parent or more than one immediate parent now come
back to this picture and watch how many immediate parents class two has one single how many immediate parent classes class two as one how many immediate parent class three has one how many mediate parent classes class two has one only here also one so are we supported with all these things miss yes we are provided with all this particular option now come here how many made a pair in class two as one how many class three as one and how many class four as to more than one immediate parent class multiple and immediate parent classes
for class 3 class 1 class 2 so more than one so what I'm telling is if you have one immediate parent let us call it single if you have more than one immediate parent I am just calling it as multiple say there is no point of confusing ourself with all this particular thing just simply let us check how many immediate parents are out one only single more than one multiple okay now what is the fifth point I want to tell you in inheritances this is a fourth point the fifth point what is the fifth point
you know in c-sharp we don't have support for multiple inheritance through classes what we are provided is only single inheritance through classes there is a fourth point so we are not supported with multiple interns here what we are supported is only single in attends what is this more than one immediate parent classes are not allowed how many immediate parent classes can we have one now let us analyze what is provided what is not available is a single init unsupported yes single entrance is supported why one immediate parent class allowed is a multi level supported yes
here also one immediate parent allowed is heretical supported yes illogical also supported one immediate parent is a hybrid supported no why class four as more than one immediate parent classes is multiple supported no class four as more than one immediate parent classes yes see two immediate parents two immediate parent so hybrid not supported multi-level also not supported what you are supported single supported multi level supported heretical supported but my level and hybrid are not supported so without that my point of discussion of my view is my view according to my view just remember only two
points one immediate parent single more than one immediate multiple multiple not supported with classes singles are protected you call it a single or you call it as multi-level or you call it a heretical no matter however you call all are having one immediate parent only if not more than one immediate parent so they are all are supported for us so this is the fifth rule of our inheritance what is it we are provided only with single inheritance through classes but not with the multiple inheritance okay next the last thing the last sixth point on the
last one earlier I demonstrated you whenever I create the instance of the child class first the control jumps to class to constructor that is the child class constructor and this will implicitly call its parent class constructor that is class one constructor and class one will call its parent class constructor with object class constructor is a different story but class two constructor will call its parent class constructor I showed you the execution process also by placing a breakpoint there if you watch execute the code again and if you just execute your code now execution starting from
the main method the main method of the child class and from here the control jumps to the child class constructor control jump layer and from here without executing it it will jump to its parent class constructor actually internal it'll jump to object class constructor also we cannot see the code because we do not have the source code of object as we cannot say but internally class one constructor it will jump to object class constructor okay once the execution is completed now so let this execution complete executed class one constructor comes back to class two constructor
executes the class two constructor and comes back to main method Y from main method we jump it to class two from class two a jumper to class one class one execution completed come back to class two class to execution completed come back to main method so this is what we have been earlier just to checked out the process we have checked out the process in the first the first point or the first rule what we explained you so class 1 constructor called class 2 Constructicons but we have a small problem here what is the problem
let us go to class 1 and make this constructor as parameterised class 1 miss parent class constructor is parameterize now and now this I value I want to use it here plus I this I value I want to use it here plus site I am using the I value there and now if I come to class 2 and run the code I will get an error I will get an error what is the error what is the error is watch out there is no argument given that corresponds to the required required formal parameter I of
class 1 not class 1 what it was trying to tell you is class 2 constructor implicitly calls class 1 constructor but if you want to call class 1 constructor it requires a parameter now without passing the parameter you cannot call so now class 2 was not able to call the class 1 constructor why it was not able to call earlier constructor is parameter lies directly call it but right now constructor is parametrized if you want to call it you require a value now and class 2 don't know what value to be sent over there that
is the reason why you are getting error in class 2 not in class 1 error is not in class 1 the error is in class to watch out the error is in class 2 why class 2 was not able to call the class one's constructor so what to do implicit calling is not working now implicit calling is not working so to resolve the problem we need to go for explicit calling how to call like this : base base means referring to the parent class not referring to the base class what is the base class base
class is parent what's this if you watch this carefully when I say base when I use base here it was referring to class one and telling asking you tell me the into value I'm passing the value when I pass this particular value here when I am going to pass this value and run the code again let's start the code watch from here the control first comes to class two constructor execution started from main method came to class two constructor and from here the control jumps to class one constructor and notice I received that value now
I received that value you can see the value of I in that I received the value 10 and after receiving that value it was executing now executing and printing the results to you and it is printing the results to you watch out you got the value so what is the sixth point you know in the first point we learnt whenever child class instance is created child class constructor well implicitly call its parent classes constructor but only if the constructor is parameter less whereas if the constructor of parent class is parameterised child class constructor cannot implicitly
call its it's parents constructor so to overcome the problem it is the responsibility of the programmer to explicitly call parent classes constructor from child class constructor and pass values to those parameters pass values to those parameters and very importantly to call parents constructor from child class we need to use the base keyboard the base keyboard is used here so that is what I showed you right now here child class was not able to call why child class was not able to call because it is not aware what value is required for execution so that is
why programmer should take the responsibility programmer knows what value is required whereby we are the developer here we know what is the value implicitly computer don't know anything at all we know because we know we are supplying the value the value of whatever we supply here will go to the parent class but unfortunately this is a static value is always the same value comes but if at all you want the value to be dynamic what to do you know pass a parameter to class to constructor also into a so when you pass the parameter now
what you require to do whenever you are creating the instance of the child class we need to pass that value here and passing a value 50 so what happens now the value 50 first to reaches to yay and the same a value I can pass to my parent class watch it value 50 can be simply speaking whenever you create the instance of the child class pass the value to it the value first comes to child class constructor from there it gets transferred to parent class constructor and parent class constructor starts making use of the value
and executes and if you want you can use this a value in class to also no problem it's not like this value should be compulsory send off to parent class only you send to parent class it is compulsory required but if required that value can be used in the child class else so this is the sixth important rule of inheritance so these are the six things what you require to remember every time before you are starting working with the inheritance thank you for today