0:07 hello everyone welcome back to our
0:09 Channel today we are diving into a very
0:11 important topic for technical interviews
0:14 objectoriented programming or oops if
0:16 you're eyeing a job at a top imy or even
0:19 a startup mastering oops is a must have
0:21 skill that every tech company values so
0:24 why is oops so crucial if you are in
0:26 your third or fourth year of college and
0:28 have been through a few placement RS you
0:29 probably already know it sign
0:31 significance in technical interviews
0:33 think about it oops and data structure
0:35 are like the building block of any
0:37 programming language can you really
0:39 create project or even write simple
0:41 Cotes without using classes methods and
0:44 objects definitely not that's why oops
0:46 is so important it is essentially your
0:48 bread and butter if you are looking to
0:50 build a career in software development
0:52 now let's talk about what to expect in
0:54 this video I am going to walk you
0:56 through 15 of the most commonly asked
0:58 objectoriented programming interview
1:00 questions these are the question that
1:02 many of our Learners faed while
1:04 interviewing for SD R so make sure to
1:06 analyze each questions carefully as we
1:08 go through them so are you ready let's
1:11 get started okay so starting up with the
1:13 very first question which is what is oop
1:18 and why is it important okay so oop or
1:20 objectoriented programming is a way of
1:22 Designing program by organizing them
1:26 around object and what are objects
1:29 object represent the real world entities
1:32 and each object object hold both data
1:34 which in this case is attribute and
1:37 behavior like functions or method which
1:39 describe what is it and what it can do
1:42 this make it easier to manage it
1:44 understand the complex program so what I
1:47 mean by this is in objectoriented
1:51 programming everything lies around
1:53 classes and
1:57 objects so suppose you want to make a dog
1:58 dog
2:02 class okay and breeds of dogs can be
2:05 different so if I consider one dog
2:08 suppose dog one it will have its
2:12 attributes for example breed then
2:16 age and then color right and it will
2:19 have certain behaviors to perform like dog
2:20 dog
2:25 bugs sleeps and eat so objects are
2:37 and
2:40 behaviors and the whole objectoriented
2:42 programming revolves around this classes
2:44 and object rather than using functions
2:47 or procedures okay and what makes this
2:49 object-oriented programming different
2:51 from procedural programming language is
2:53 that in procedural programming language
2:55 you use to do everything in functions
2:58 right but here you do everything in
3:00 classes and you make objects to attain
3:04 certain functions so coming back here
3:05 now let's discuss about the importance
3:08 of oops so modularity oop helps you to
3:10 break down a program into smaller part
3:12 or modules and each part has specific
3:15 role and it's very easy to work with and
3:17 keep things organized second code
3:19 reusability you must have heard the term
3:22 called inheritance right so inheritance
3:25 what is does it it provide a template so
3:28 you can use it again and again without
3:30 starting from the scratch
3:32 coming back to third point which is data
3:34 security so oops has encapsulation to
3:36 protect certain data and only specific
3:39 part of the program can access it then
3:41 come scalability you can easily scale
3:43 your program from smaller to larger
3:46 system and then come maintenance because
3:48 everything is modularized you can easily
3:50 modify parts of the program
3:52 independently which saves your time and
3:55 effort okay so that's all for the oops
3:56 so coming back to our next question
3:59 which is what are classes and objects so
4:00 we can say that a class is a user
4:03 defined data type okay why user defined
4:06 data type because whenever you declare a
4:08 class you write
4:11 class and then ABC then the name of the
4:14 class so in this case this act as user
4:16 defined data type because you yourself
4:19 defining the attributes and the behavior
4:20 so and it contains data member and
4:22 member function which can be accessed
4:24 and used by creating an instance of that
4:27 class okay and object on the other hand
4:29 is an instance of a class which
4:31 represents a real world entity having
4:35 properties and behaviors okay so you can
4:38 see here this example so we have made a
4:43 class dog here right this is a class
4:45 class
4:50 dog what it has it has breed size age
4:53 color you know every dogs comes with a
4:56 different breed having different size
4:58 then color and then it performs certain
5:02 functions for example eating sleeping
5:07 sit run okay so here this dog is a class
5:10 and we have objects related to that
5:12 class so you see that we have three
5:17 objects Mastiff males and chaa okay so
5:20 you know these three dogs have different
5:23 breeds different size different age and
5:25 different color so these three dogs they
5:28 serve as the object corresponding to
5:30 this dog class with different behaviors
5:34 and attribute so you can say that this class
5:35 class
5:39 dog having certain properties and Method
5:47 template for all the three dogs meaning
5:49 these three dogs will have the
5:51 properties and the behavior defined in
5:53 this class talkg moving on to a third
5:55 question which is what are the four
5:57 pillars of oops so there are four
6:00 pillars of oops which is in capsulation
6:04 abstraction inheritance and polymorphism
6:12 encapsulation okay so taking a very
6:15 simple example Suppose there is a company
6:18 company
6:21 okay there is a company with different
6:37 sales right and the finance section it
6:39 handles all the financial transaction
6:41 and keep records of data related to
6:43 finance similarly the sales section
6:47 handles the sales related task
6:57 task and the accounts team manage the
7:00 account of the
7:04 company okay now there may arise a
7:06 situation when for some reason an
7:08 official from the finance section needs
7:10 all the data about sales in a particular
7:13 month in this case he is not allowed to
7:15 directly access the data of sales
7:18 section okay he will have to firstly
7:21 contact somebody from the sales section
7:24 and then request him to give the data
7:27 and this is what encapsulation is so
7:51 unit so the variables or data of a class
7:53 are hidden from any other class and can
7:55 be accessed only through any member
7:57 function of that respective class and in
7:59 encapsulation the data in a class is
8:02 hidden from other classes which is also
8:04 known as data hiding so it has provided the
8:12 security by wrapping the data so now
8:21 abstraction so abstraction the funai is
8:24 very clear show what is needed and hide
8:29 the rest so you must have used cars
8:31 or bikes
8:34 right so do you really know what are the
8:37 things going inside the Bonet of a car
8:40 or what how the engines are functioning
8:43 no right you only know that pressing the
8:45 accelerator will increase the speed of
8:48 the car or applying the brake will stop
8:51 the car but you don't know how the
8:52 pressing accelerator the speed is
8:55 increasing and any other inner mechanism
8:58 okay that is what data abstraction so it
9:00 is one of the most most essential and
9:02 important features of object-oriented
9:04 programming which refers to providing
9:07 only essential information about the
9:09 data to the outside world hiding the
9:12 inner details so moving on with
9:15 inheritance okay so inheritance it
9:25 abilities inheritance is an another
9:27 important pillar of oops and the
9:29 capability of a class to derive property
9:31 and characteristics from another class
9:34 is called inheritance so meaning
9:37 suppose you have your
9:42 parents and here is you so you are
9:44 deriving certain Properties or certain
9:46 genes or certain traits from your
9:50 parents similarly in oops when certain
9:52 class derived some properties from
9:55 another class it's called inheritance it
9:57 allows user to reuse the code wherever
10:01 possible and it redu with its redundancy
10:03 talking about polymorphism so
10:06 polymorphism is simple adapt and respond
10:16 says poly meaning
10:20 many and morphism meaning form for
10:21 example your
10:28 dad okay he is someone's son somebody's husband
10:35 unemploy so he at one time is performing
10:38 multiple actions so the same person
10:40 possess different behavior in different
10:42 situation that is the case of
10:44 polymorphism so the ability of a
10:46 function to perform in a different
10:49 manner under different circumstances is
10:51 called polymorphism so that's all for
10:54 the four pillars of oops now moving on
10:56 with our next question which is how to
10:59 call one Constructor from another
11:02 okay so there is very famous term for
11:04 calling of one Constructor from another which
11:07 which
11:18 chaining okay so imagine you are at a
11:21 party and you just walked in so instead
11:24 of talking to everybody at once you
11:27 decide to greet one friend so Suppose
11:30 there is this person a goes to a party
11:38 there wherein a lot of person are there
11:41 so instead of going to everybody he
11:44 decides that firstly he will go here he
11:46 will greet
11:50 him okay and then this particular person
11:53 will help this person to meet
11:56 him and this person will help this
11:58 person to meet him and like this the
12:00 chain goes on
12:02 on similarly in Java Constructor
12:05 chaining is the process of calling one
12:07 Constructor from another Constructor
12:09 within the same class or from a base
12:13 class and why to reuse the code and
12:16 avoid duplication when initializing an
12:20 object okay so let me explain you with
12:23 the help of a code so let's move to our
12:26 vs code to quickly see its
12:29 implementation okay so here is this so
12:31 what we are doing we are making a class
12:34 Constructor chain and an object CC with
12:37 a new and we are here not passing any
12:40 argument okay so Java looks for the
12:42 default Constructor because we didn't
12:44 pass any argument so here is this
12:47 default Constructor and inside it it
12:51 immediately sees this St Str Java which
12:54 means it is calling the parameterized
12:57 Constructor so what will happen the flow
12:59 of code because this line is calling the
13:01 parameterized Constructor will pass to
13:04 this parameterized Constructor is
13:07 defined here string s Str with a parent
13:09 statement parameterized Constructor is
13:13 called and after this the flow of code
13:15 will again move here and this will print
13:24 run so you see parameterized Constructor
13:26 and then default
13:29 Constructor so this back and forth
13:31 calling sequence allows each Constructor
13:33 to do its part of the initialization
13:36 without repeating code so in simpler
13:38 term you can say that Constructor
13:40 chaining is like making one Constructor
13:44 call another for example here this line
13:46 is calling this Constructor with one
13:48 parameter and this particular line is
13:50 calling this Constructor with zero
13:54 parameters which is allowing us to reuse
13:55 the code if we are calling the
13:58 Constructor in the same class we use
14:00 this keyword but if we are calling the
14:02 Constructor from the base class we will
14:04 use the super keyword okay moving on to
14:06 our next question which is what are
14:09 access specifiers and their purpose so
14:11 access modifiers are the keyword which
14:13 Define the accessibility of a class and
14:15 its members which in Java are used to
14:18 control the visibility or accessibility
14:20 of a class interface variable methods
14:23 Constructor or data members so basically
14:26 they provide the authority of a class or
14:29 data members so there are three types of
14:32 access specifiers in Java firstly
14:35 talking about public you can consider as the
14:46 house okay and anyone can see and access
14:50 your house or your house things from
14:53 this main gate just like the public
14:55 members in Java which can be accessed
14:57 from anywhere in the
14:59 program then comes private so you can
15:02 think of a logged room inside your house
15:05 so you must be having a lot of rooms in your
15:06 your
15:09 house so Suppose there is one room in
15:12 your house which is
15:17 loged and only you or your
15:21 parents can access it similarly in Java
15:24 the private members are only accessible
15:27 within the same class and protected so
15:29 this is like the family one area in the
15:32 house family members like sub classes or
15:35 close relative so there must be some
15:40 part of the house which can be accessed
15:42 by close
15:46 relatives or family
15:48 members similarly in Java the protected
15:50 members are accessible within the same
15:54 package and subass in other package okay
15:58 now let's see the code for this one okay
16:01 so so you see we have made a public
16:05 class guest and a class house with
16:07 public string main gate private string
16:09 bedroom and protected String family
16:13 room and we have created a object my
16:16 house okay so now when we are
16:20 calling my house. main gate then it it
16:30 happens so you can see here the output
16:33 is anyone can enter Because this is
16:35 defined public and this class can access
16:39 it so what will happen if we'll try to
16:56 see so you see it is giving that bedroom
16:58 has private access in the house and it
17:01 is not accessible here
17:03 here
17:06 okay now let's comment this one and
17:15 one so you see we have Define this
17:18 protected String family room and when we
17:21 are calling it it's printing that
17:24 accessible within package or by any
17:27 subass so just like I told some family
17:30 guests are allowed to access your rooms
17:33 in your house similar similar is the
17:35 case with protected data members all
17:39 right so that's all for Access specifier
17:41 let's move to our next question which is
17:43 what are manipulators in Java so
17:46 manipulators are the method or function
17:49 that helps to change the state of an
17:53 object what I mean by this is consider
17:56 you have a car okay and think about all
17:58 the things you need to do to keep it running
17:59 running
18:07 oil okay then
18:16 pressure or refilling the
18:19 fuel to make sure that your car remains
18:21 in a good position so what we are doing
18:24 by refilling fuel changing oil or
18:27 adjusting tire pressure we are changing
18:31 the car state in some way or other like
18:34 updating how much fuel it has or
18:37 indicating that oil has been changed so
18:39 coming back to
18:43 manipulators so basically it means to
18:46 modify the state or behavior of object
18:48 and they are not built in features like
18:51 C++ but they are used to change or
18:54 manipulate object attributes often via
18:57 Setters and other class for example if
19:00 you people have used C++
19:03 and if you would have seen this or used
19:08 this set base int what is this this is a
19:15 function which is used to set the
19:18 library base field flag based on the
19:20 argument specified as the
19:25 parameter okay so if you using set base
19:28 as 10 it will change the argument
19:31 corresponding to base 10 in Java we
19:34 don't have directly these manipulators
19:35 but we do attain these things with the
19:38 help of a function okay so let's see the
19:42 code for the same you see here we have a
19:46 public car this oil change firstly Falls
19:49 fuel level zero and tire pressure 30 we
19:52 have this method to change the oil we
19:55 have this method to refill the fuel we
19:57 have this method to adjust the tire
20:01 pressure so what are all these methods
20:03 these methods are nothing but just the
20:06 manipulator functions to alter the state
20:17 this
20:20 see oil has been changed fuel refilled
20:24 tire pressure oil change fuel level and
20:26 pressure is currently 35
20:30 PSI so through these functions change
20:32 oil refill fuel we are changing the
20:35 state which is what
20:37 manipulators so moving on to our next
20:40 question which is what is difference
20:43 between deep copy and shallow copy okay
20:48 to explain this firstly let's talk about
20:52 shallow copy so suppose this is person a
20:55 and this is person B and this is a
20:59 common cupboard which A and B are using
21:02 okay and if this person a opens the
21:05 locker and puts his book inside person B
21:08 will be able to see because they are
21:10 sharing that single
21:13 Locker so in terms of objectoriented
21:18 programming a shallow copy is when two
21:21 references reference one and reference
21:34 okay and if one reference change the
21:37 content both reference see the changes
21:39 because there is only one actual object
21:40 in the
21:43 memory on the other
21:46 hand on the other hand if you're talking
21:47 about deep
21:52 copy so suppose this person a and this person
21:53 person
21:57 B they have their separate piggy banks
22:00 and if person a is putting his money in
22:03 his piggy bank person B will not be able
22:04 to see what money or what amount of
22:08 money is left in his piggy bank and VC
22:10 Versa is also true so new memory is
22:13 allocated and each object has its own independent
22:14 independent
22:17 reference right so let's move ahead to
22:19 see the code of uh deep copy and shallow
22:24 copy okay so let's see this example of
22:27 shallow copy in Java so here we have
22:30 created a class
22:33 ABC and here we have created the object
22:34 of that class
22:38 ABC object one and here we are copying
22:40 the reference not the value which is
22:45 shallow copy that is ABC obj2 equals to
22:49 obj1 and when we are changing obj2 dox
22:51 we're updating the value of x using the
22:55 reference variable object 2 as six and
22:57 when we are trying to print the value of
23:06 we will see the value of x is
23:09 6 so changing in object two has
23:11 automatically changed the value in
23:14 object one now talking about the Deep
23:18 copy same we are making a class
23:21 ABC with a variable
23:27 X then uh object obj 1 object obj2 but
23:30 we are creating a new object which is
23:33 deep copy and we are updating the value
23:38 of x in obj2 as 6 and when we are
23:49 happens so you see the value of x in
23:53 obj1 is 30 and the value of x and object
23:57 to is six because they two are different
23:59 identities so moving on with our next
24:01 question which is what do you mean by
24:04 abstract class and can we instantiate
24:05 the abstract
24:08 class so an abstract class is a
24:11 blueprint for another class for which we
24:14 can't create objects directly and an
24:17 abstract class can contain both abstract
24:19 method without a body and a regular
24:22 method so why to use abstract class we
24:25 use abstract class to provide a basic
24:28 structure for sub classes to build on
24:30 which ensure sub classes implement the required
24:32 required
24:43 abstract it is just a thought it's not
24:47 an actual class it's just a thought for
24:51 example if you have an animal class
24:53 class
24:56 okay so animal consist of a wide
25:00 varieties of species for example dogs
25:02 cat reptiles mammals
25:06 Etc so if if in this animal class you
25:08 have a
25:12 method suppose void
25:16 speak and that's it no implementation
25:19 nothing just over so if and if you're
25:22 making a class
25:25 dog this dog class will have its own
25:36 so this dog class will see this as
25:39 South dog barks
25:41 barks
25:50 class this cat class will has its own
25:52 implementation for this speak for
25:56 example cat meow right so you can say
25:59 that it is just a thought
26:02 okay so now let's quickly go ahead and
26:05 see the code for the same so you see we
26:07 have an abstract class
26:11 animal with an abstract method make
26:12 sound with no
26:14 implementation then we have this class
26:16 dog which extends the animal and this
26:18 has its own implementation for make
26:22 sound which is the dog box so and here
26:24 in this class animal example we are
26:28 making a object of do class and we are
26:38 happens so you see the output here is
26:41 the dog box so that's all for the
26:43 abstract method let's move on to our
26:45 next question which is what is an
26:47 interface in Java and how does it
26:50 promote abstraction provide a simple
26:54 example so an interface in Java is a
26:56 reference type that contains only
26:58 abstract methods and what abstract
27:01 method just we discussed abstract
27:04 methods without body and constant
27:06 variable so it provides a blueprint for
27:09 class to follow without implementing any
27:12 specific behavior so for example just
27:14 see the pointers here it is used to
27:16 achieve abstraction by interface we can
27:18 support the functionality of multiple
27:21 integrants and third it can be used to
27:22 achieve loose
27:25 coupling okay but now you must be
27:28 thinking if interface is also
27:30 a reference that only contain abstract
27:32 method then what is the difference
27:34 between abstract method and this
27:38 interface okay so the major difference
27:40 between this abstract method and interface
27:42 interface
27:51 level okay so this abstract
27:58 abstraction what is the meaning of
28:02 partial abstraction is they can have
28:04 both abstract
28:12 methods but in case of [Music]
28:20 interface they have 100% abstraction
28:23 meaning all the functions are abstract
28:31 the second difference comes that only
28:40 class can be
28:43 extended so only one abstract class can be
28:43 be
28:47 extended and a class can Implement multiple
28:56 interfaces okay so let's quickly
28:59 see the coding implementation for the
29:02 same okay so you see this is abstract class
29:03 class
29:07 animal with abstract method void Mak
29:10 sound and a concrete method with a
29:13 definition sleep animal is sleeping so
29:15 firstly this is the interface with one
29:19 abstract method play then there is a
29:21 second interface with one abstract
29:24 method greet when we are making a class
29:27 dog and extending the animal which
29:31 implement ments pet and friendly so you
29:33 see this class dog extend the abstract
29:35 method and implements both pet and friendly
29:36 friendly
29:39 interfaces meaning it can Implement
29:43 multiple interfaces and when this dog is
29:45 giving its own implementation void makes
29:48 sound as dog box and own implementation
29:51 for this play dog is playing and for the
29:55 Greet also dog greets you so let's see
30:02 so when this my dog. Mak sound is called
30:05 then an abstract method from animal is
30:08 called when my dog do sleep is called
30:10 concrete method from animal is called
30:13 and then when my dog. play is called
30:15 then dog is playing method from pet
30:17 interface is called and same for the
30:20 Greet method from friendly interface is
30:24 called so this is all for the
30:26 interface so let's move to our next
30:27 question which is discuss about
30:30 exception handling in Java so the
30:33 exception handling in Java is one of the
30:35 very powerful mechanism to handle the
30:37 runtime errors so the normal flow of the
30:41 application can be maintained so what are
30:49 exception so exception is an
31:07 program okay so some of the famous
31:09 exceptional handling mechanism to handle
31:13 runtime errors are class not found IO
31:16 exception SQL exception remote exception
31:18 Etc so what is the core advantage of
31:20 this exception handling so the core
31:22 advantage of this exception handling is
31:25 to maintain the normal flow of an
31:28 application okay so you know that
31:29 exception normally disrupt the normal
31:32 flow of application and that is why we
31:36 need to handle this exception so if I
31:39 say that you have 10 statement statement
31:43 1 statement two statement three
31:47 statement 4 and an exception occurs at this
31:49 this
31:52 statement okay suppose this line of your
31:54 code has some sort of exception so what
31:57 do you want do you want your code to
32:00 stop here or you want a block of code to
32:02 handle this exception and rest of the
32:06 code should run of course so however
32:07 when we perform exception handling the
32:10 rest of the statement will be run 6 7 8
32:13 9 10 so by performing this exception
32:15 handling your exception will be caught
32:17 here and rest of your program will work
32:21 smoothly so let's quickly see the
32:24 demonstration okay so as you can see on
32:27 the screen we have type of exception
32:30 user defined and built-in so built-in
32:32 exception consist of checked exception
32:36 and unchecked exception and so how is
32:39 exception handling achieved so for that
32:43 we have four keywords in Java Tri block
32:46 so Tri block is something the part of
32:49 code that may throw an exception catch
32:52 block it handles the exception that has
32:55 occurred in Tri block findy block it
32:57 execute the codes regardless of whether
32:59 an exception occurred and then throw
33:01 keyboard which manually triggers an
33:03 exception in the
33:06 code so let's see the code so we have
33:09 this public class Java exception example
33:12 we have this try block so this part of
33:15 your code can raise some exception for
33:17 example if you are putting in data
33:20 equals to 100 divid by 0 you know that
33:24 anything divided by 0 will give a error
33:27 so the cat block will cach that error
33:29 and then the rest of the code will be
33:38 code so you see arithmetic exception
33:43 divide by 0 is the error and rest of the
33:47 code will be run as it is so that's all
33:49 for the exception handling let's move to
33:52 our next question which is what is a
33:55 Constructor and how we can overload them
33:59 so Constructor is a very special method
34:02 called when an object is created and it
34:05 has the same name as the name of class
34:07 and no return type so what are the key
34:09 pointers it matches the class name it
34:12 doesn't have the return types and it is
34:14 called when using a new for creating a
34:17 new object so what I mean by Constructor
34:21 is suppose you have a
34:23 class say
34:26 main here you have int
34:30 X okay and you're defining here a
34:33 Constructor mean with the same name of the
34:34 the
34:37 class xal to
34:41 5 and here outside this main class you're
34:43 you're
34:46 creating an object my
34:58 right so as soon as you create this my
35:04 object object the flow of code goes to
35:07 this Constructor and it says that okay
35:09 one object is being created now you have
35:12 to assign this object value as
35:17 five okay so whenever it will print
35:22 system.out.print Ln my OBG dox it will
35:26 return this x value so every time an
35:28 object is created it's saying okay fine
35:31 I'm being created Now give me my value
35:33 and that object will ask its value
35:36 through the Constructor okay so how we
35:38 can overload them so we have two types
35:42 of Constructor which is default and
35:45 parameterized so this is default zero
35:47 parameters this is the parameterized
35:50 Constructor with one parameter string
35:54 name and this is the Constructor with
35:58 two parameters so we can overload them
36:01 by defining the number of parameter the
36:05 Constructor will have Okay so let's move
36:07 on to our next question so what do you
36:09 mean by polymorphism state the
36:12 difference between static and dynamic
36:14 polymorphism okay so as I told you
36:17 earlier that
36:19 polymorphism is the ability of a
36:23 function to act in more than one form
36:28 poly meaning many morphism meaning form
36:32 Ms like your dad a
36:34 father a
36:37 child an
36:40 employee right so the ability of a
36:42 message to be displayed in more than one
36:43 form is called
36:46 polymorphism right and this polymorphism
36:47 has two types
36:50 types
37:09 overloading and runtime function
37:13 overriding right for example you have a class
37:20 H with area
37:23 function and it
37:28 returns a into B okay
37:35 on takes parameter as int a comma int
37:39 B okay and if we have another function
37:43 but with different data type for example
37:45 example double
37:47 double
37:54 area able a comma Double B and
37:58 return a into B Etc
38:01 and if we'll pass parameters for example
38:06 if we are passing 2A 4 and 2A 5A 3A
38:12 5 then in case of 2A 4 this function
38:14 with int a and int B will be called and
38:18 in case of 2.5 and 3.5 this able a and
38:20 double b will be called so you see the
38:22 name is same but the functionality is
38:24 changed so that's all for the function
38:27 overloading now coming to method
38:30 overriding so it is also known as
38:32 Dynamic method dispatch it's a process
38:34 in which a function call to the
38:37 overridden method is resolved at the run
38:39 time and this type of polymorphism is
38:41 achieved by method overriding which
38:43 occurs when a derived class has
38:45 defination for one of the member
38:48 functions of the base class so now
38:51 talking about overriding suppose you
38:54 have this base
38:59 class class base you have void show
39:00 show
39:07 okay and the statement is I am in Base Class
39:08 Class
39:12 okay and you have this derived
39:15 class class
39:19 derived with same function name
39:21 show I am
39:24 in derived class
39:27 okay now when you will create the object
39:29 object of this derived class and when
39:32 you will call this function you will
39:35 find that the output will be I am in the
39:38 derived class and this has overridden
39:41 the method of Base Class which is called
39:44 as method overriding so when an object
39:46 of a child class is created then the
39:48 method inside the child class is called
39:50 this is because the method in parent
39:52 class is overridden by The Base
39:55 Class okay so that's all for the
39:58 polymorphism let's move on to our next
40:01 question which is what is coupling so
40:04 coupling refers to level of dependency
40:07 between two classes or component meaning
40:10 how much they are interrelated or
40:12 interdependent upon each
40:15 other so there are two type of coupling
40:18 tight coupling and loose coupling so
40:20 tight coupling occurs when two classes
40:22 are highly dependent upon each other
40:26 Suppose there are two classes Class A
40:28 and Class B
40:31 and they are highly dependent like if
40:33 you're changing anything in class A
40:36 Class B also get altered this means that
40:38 if one class changes the other class may
40:41 also need to change in this scenario the
40:42 classes are closely linked which can
40:45 make the system less flexible and harder
40:49 to maintain in simple terms imagine you
40:51 have a
40:53 shirt and
40:57 trousers that are stitched together and
40:59 if you want to wear a shirt of different
41:00 color then you have to change your
41:04 trousers also because they are connected
41:05 this is similar to tightly coupled
41:08 classes where one class implementation
41:10 heavily relies on the other classes
41:12 implementation now talking about the
41:15 loose coupling so loose coupling occurs
41:18 when classes have minimal dependencies
41:20 if one classes changes it usually does
41:23 not require changes in the other classes
41:26 which allows for greater flexibility and
41:28 easier Main maintainance so in case of
41:30 tight coupling the dependencies are
41:32 higher more coordination and more
41:35 information flow which also reduces in
41:38 security because the classes are
41:41 connected and altering one will alter
41:44 other which increases the chances of
41:47 data breach whether in loose coupling
41:49 dependencies are less coordination is
41:51 less and information flow is also less
41:53 making the code more flexible and
41:56 maintainable so moving on to our next
41:58 question which is explain the
42:01 inheritance and its type so inheritance
42:03 is a fundamental Concept in
42:06 objectoriented programming that allows
42:09 one class to inherit the properties
42:12 which is fields and behavior the method
42:14 or function of an other class which
42:17 helps in code reusability making it
42:21 easier to manage and create the code so
42:24 you can think of the inheritance like a
42:26 family tree so at the top your grandparent
42:28 grandparent
42:36 come okay then you come deriving the
42:38 properties of mother and father so it's
42:43 like a family tree okay so why to use
42:45 inheritance it enhances the code
42:48 reusability so subclasses can use method
42:51 and field from the parent class reducing
42:54 duplication method overriding subclasses
42:56 can customize method from the super
42:59 class which is which enables runtime
43:01 polymorphism and abstraction so common
43:04 features are defined in super class
43:06 while subclasses Implement only specific
43:10 behavior which helps in less code now
43:12 talking about the types of inheritance
43:14 so there are basically four types of
43:18 inheritance single multi-level
43:21 hierarchal and hybrid so talking about
43:24 single inheritance so there is one
43:27 animal class and only one base class
43:30 which is deriving from this animal class
43:32 multi level inheritance there is animal
43:35 class then there is a mammal class and
43:37 then there is child class so mammal
43:39 class is deriving from animal class and
43:42 this child class is deriving from mammal
43:45 class so here you can see the code on
43:48 the screen we have a class animal with a
43:51 function eat and it is giving a print
43:53 statement as animal eats then we have a
43:56 class dog which is extending this animal
43:59 which has its implementation system.
44:02 out. printel dog bar so you see that
44:04 only one deriv class is there which is
44:07 deriving from the base class now talking
44:09 about multi-level so you see this animal
44:11 class is there then dog class is
44:13 extending animal and there is then a
44:16 puppy class which is again extending the
44:23 animal then
44:25 dog then
44:29 puppy so properties of animals are being
44:32 inherited by dog and properties of dogs
44:35 are being inherited by puppy class so
44:37 that was multi-level inheritance now
44:40 let's see hierarchical inheritance in
44:42 hierarchical inheritance multiple
44:45 classes for example you see this dog
44:47 class and this cat class is inheriting
44:50 from this one single animal class and
44:52 multiple inheritance is when the same
44:56 dog class in is inheriting from more
44:59 than one classes for example canun and
45:02 canbar these two are interfaces and this
45:05 is deriving from that so you see the
45:07 code for hierarchical inheritance is we
45:10 have a class animal and a eat function
45:12 we have a class dog which is extending
45:15 animal and we have a class cat which is
45:25 animal inherited by dog and Same by cat
45:27 so this is called hierarchy so you see a
45:29 specific hierarchy is being followed
45:31 here so that's all for the inheritance
45:33 let's move on to our next question which
45:36 is explain virtual keyword so the
45:39 keyword virtual is not used in defining
45:41 a virtual function in Java but as we are
45:44 learning oops concept there is an
45:46 important Concept in objectoriented
45:48 programming which is known as virtual
45:50 keyword so what happens suppose you have
45:53 a class A and Class B and Class C they
45:56 both are inheriting a property of Class
46:00 A okay and suppose you have a Class D
46:03 again you are inheriting the property of
46:06 Class B and Class C now when you're
46:09 inheriting D from B you are inheriting
46:15 B's a and then you inheriting C's a okay
46:17 which leads to ambiguity because you
46:19 don't know which instance you have
46:23 copied from for class D okay and to
46:26 resolve this ambig something known as
46:30 virtual comes into picture which ensures
46:33 that only one instance of a in D is
46:36 there preventing
46:39 duplication so we'll see a code for the
46:42 same but before we jump into the coding
46:46 demonstration the problem here arises is Diamond
46:53 problem okay because there is Class A
46:58 there is Class B there is this class C
47:00 and then there is class t
47:10 Okay this class is inheriting a this is
47:14 also this you see this is a diamond like
47:16 shape problem has occurred so
47:18 interviewer can directly ask you what is
47:20 a diamond problem in oops so don't get
47:23 confused this this same thing is called
47:25 The Diamond problem also now let's see
47:27 the coding demonstration
47:31 so I have made this virtual file in C++
47:33 because C++ directly uses virtual
47:35 keyboard okay so we have this class
47:38 animal with void sound we have this
47:40 class dog which is publicly inheriting
47:42 animal with again the sound
47:45 implementation this cat class also with sound
47:46 sound
47:48 implementation and then we have this pet
47:52 class which is deriving both dog and cat
47:55 so you see the pet should not get
47:58 confused that which which sound it is
48:01 calling so that it is virtually
48:03 inheriting you see this dog is virtually
48:05 inheriting the animal the cat is
48:08 virtually inheriting the animal so that
48:11 two instance doesn't get
48:16 created so now let's run
48:19 it see dog
48:23 box cart meow an animal makes a sound so
48:26 you see using virtual keywords make it
48:27 very easy
48:29 and further protects your program and
48:32 remove any kind of duplicacy so that's
48:35 all for the virtual keyword so if you
48:37 have liked this video kindly hit both
48:39 the like And subscribe button for Intel
48:42 Parts YouTube channel thank you and see