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