0% found this document useful (0 votes)
2 views

NoteGPT_Java OOPs Concepts in 120 minutes _Object Oriented Programming _ Java Placement Course _ Simplilearn

This tutorial covers object-oriented programming in Java, focusing on classes and objects, access modifiers, inheritance, encapsulation, and interfaces. It explains the concepts of attributes and behaviors in entities, using a student class as an example to illustrate how to create and manipulate objects. The session also introduces access modifiers for security in Java programming, emphasizing the importance of limiting access to variables and methods.

Uploaded by

swathi342005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

NoteGPT_Java OOPs Concepts in 120 minutes _Object Oriented Programming _ Java Placement Course _ Simplilearn

This tutorial covers object-oriented programming in Java, focusing on classes and objects, access modifiers, inheritance, encapsulation, and interfaces. It explains the concepts of attributes and behaviors in entities, using a student class as an example to illustrate how to create and manipulate objects. The session also introduces access modifiers for security in Java programming, emphasizing the importance of limiting access to variables and methods.

Uploaded by

swathi342005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 39

00:08

hey everyone welcome to simply learns youtube channel in this session we


will be covering the object oriented programming in java in this tutorial we will
discuss the following concepts classes and objects access modifiers inheritance and
protected access modifiers encapsulation abstract classes interface multiple
inheritance in java using interfaces and finally we will cover some of the projects
based on our learnings that is atm project and calculator project so before we
begin make sure that you have subscribed to

00:39
our youtube channel and don't forget to hit that bell icon to never miss
an update from simply learn hello guys this is vikesh and let's get started with
today's topic which is about classes and objects in java so like i have been
promising in the previous sessions that i will cover this in a dedicated session
and this is a session where we will go deeper into the concept of classes and
objects you have been seeing this keyword all along in the all the sessions
wherever i ran the different examples and let's

01:11
understand what this means technically if you go by the definition a
class is basically a blueprint of an object which brings the question as what is
object an object is basically an instance of the class i know this becomes more
confusing so let's break it down into simpler definitions a class is technically a
logical entity it's you can think of it as any entity in the world if you have to
let's say if you have to build a student management system or a college management
system then all the real life

01:45
objects which you see we always say that java is heavily inspired from
real life examples and classes and objects are the reason we say that because
whatever you see in the world is always basically some entity human beings are an
entity cars are an entity students are an entity lecturers are an entity so who are
all the actors all the actors which you see in the world are basically entities and
you can map them as classes in the java program now why do you want to map them
because these entities have basically

02:21
two kind of specifications one is every entity will have some attributes
and it will have some behavior and actually that is how you identify an entity for
example human beings have attributes like we have hands we have eyes we have ears
legs etc these are our attributes then we have our behaviors behaviors are actions
you can think of them as a human being can speak so can speak is an action it's a
behavior can listen is an is an action it's a behavior can walk is an action is a
behavior similarly can eat is also an action or a

02:58
behavior so that's how you basically represent an entity that's how
basically you identify an entity that an entity will have some attributes and some
behavior if you see those two specifications in any entity you can map that entity
or you can write that entity inside a java program as a class because a class in
java will exactly have these two properties it will have some attributes which we
call as properties and it will have some behaviors which technically are called as
methods so it will have
03:32
attributes it will have properties and methods similar to real-world
entity where we have attributes and behavior behavior our methods and attributes
are properties that's how it is so if you if you apply this simple philosophy
whenever you are taking requirements from a customer for a particular project
always try to identify these entities these actors in your program once you have
identified those you will be able to write a corresponding java class in your java
program which can represent those entities without representation of

04:02
those entities you cannot write the program so java will kind of force
you to think in this direction to find entities which will have attributes and
behaviors so taking that philosophy further and taking that example further here
for you i have prepared an example where i am creating a student class so basically
let's say if you have to build a college management system or a student management
system then student is probably one of the main entities which will be talked about
when you take the

04:32
requirements so if you think about the attributes and the behavior
concept a student will have a name will have an age will have an address and at the
same time a student can also have some behaviors like can enroll in a particular
course can attend a lecture can take a leave can take exam etc these are all
behaviors let's understand this in a more detail with the help of this example
where i've created a class which is named as student and you see the public keyword
here again i will cover access modifier

05:06
public is basically an access modifier and i will cover this in very
detail in the upcoming sessions for now let's focus on this part so we have created
a class to represent our entity you put a curly braces around it it ends here and
after that you provide the attributes so here i'm saying that a student will have a
name it will have an age and it will have an address it has three properties so the
technical name of attributes is properties after that let's skip this part and i
will come back to this part once you

05:38
have declared these properties you would want to provide a way for the
name to be filled or replaced or received for example if you have created the
student class how do you insert the name inside the student class or how do you
insert the age or how do you insert the address or modify the age name or address
properties for that we have these setters and getters methods we call them setters
and getters because they start with this keyword not keyword but they start with a
string called set it is not

06:12
a keyword you can actually write it as anything you can call it as modify
name that will also be absolutely fine java will not give you an error but the
standard naming convention given by java is to start the methods as starting with
set and get this is the this is the standard naming convention and like i said it's
a naming convention it's not a compulsion but if you write this way your co-
developers and the people who are going to maintain this project in the long run
will be very happy otherwise they will be very

06:44
angry if you use strange methods because like i said this is the standard
naming convention so now you are going to write a setter method for each of the
properties in this particular class for name age and address so we call set name
again you put public void because this is not supposed to return anything this is
just supposed to set the value of name with whatever value you specified then you
call set name and then you provide the value which you are supposed to set against
the name so whatever value like any random name john

07:20
or jane whatever you need to supply that name here value while you call
this method and that name uh whatever you name you supply will be set against the
name property of this particular class and remember i use the word this here
intentionally because the way you are going to represent this is with the help of
this keyword this keyword means current object it means that you are as when you
use this dot name it is referring to the current object's name property and this
name is basically corresponding to the name which is

07:53
coming in when somebody is trying to set a name similarly for set age age
is a numeric number if you if you observed it's basically an integer then i'm
saying age and whatever value of age will be coming will be set in this particular
object's age then similarly for set address we have a string address argument being
passed into into this method and this gets set inside the current object's address
property so i have provided a way for the external world to set the values of name
age and address because for each

08:25
student you would need to set these values now you also need to provide a
way to receive the values or to return the values or to read the values for reading
operation you need to provide the getter method so you see this get again this is
not a keyword it's a standard naming convention so similar when you when you wrote
set name similarly you will write a get name method this get name method is
supposed to be reading the current object's names properties value and will return
that value since name is a string

08:55
that's why you see the return type as string here similarly get age is
going to return the age which is an integer and address is going to return the
address you can also write it like this which is absolutely fine it is not going to
complain because technically it is returning the current objects name properties
and age property and the address property now let's talk about this what is what is
this section so this section is basically called a constructor and i have a whole
session dedicated on the

09:25
constructors but let's understand why we need this remember when i told
you the definition of the class i said that class is a blueprint of an object so
you have created a student class and you have 5000 students in your college now you
need to create a new student object for each of those student you need to
initialize this student class 5000 times how do you do that to do that we do that
with the help of constructors we call this constructor this is a special type of
method remember this is a special type of method which is

10:01
provided by java to initialize any classes objects if you want to create
multiple objects of the student class you can repeatedly call this student
constructor we call this constructor so you put public here because we want this
constructor to be accessible to the outside world then you put the exact class name
so whatever class name you have provided here you need to put the exact class name
here if you don't do that java is going to raise an error if i do this here it is
going to you see a red line here because java

10:32
failed to compile this constructor it does not understand what is student
triple one but it does understand what is student so this is how it is going to be
and then you provide all the properties which are going to create the student
object so you're going to create the name the age and the address so the name age
and address and then you set those values again similar to the setter methods we
have seen the reason we need to use this here because if you don't if you write it
like this then java doesn't understand the

11:03
difference between the current object's name and the name coming from the
argument so to differentiate that you need to use this here if you use n here and
if i just use n here then this is fine because there's only single name variable in
this whole class then but if i write it like this then and if i write it like this
then java doesn't know if this name is basically the student class's name or this
name is student classes name it will get confused like you are getting confused if
you if you try to just find

11:33
this in mind mapping so that's why we need to write this here so that
it's clearly differentiable that whenever i use this dot name i'm referring to the
classes name property and when i'm just using the name i'm referring to the method
argument that's what we do in doing this in this constructor so you basically
initialize all the properties of your class with some values with the help of
constructor so we have the constructor we have the setter we have the getter we can
cover about this two string basically this two

12:02
string is to print print something but we can cover that later now uh
let's initialize this particular class so i'm similar to the initialization we have
seen so far you just write student you write the object name this can be anything
and then you use the new keyword which is used to initialize anything in java new
is a keyword in java which you can use to initialize literally anything in java so
you use new student and when you say new student you can provide all the values
which should match its constructor so

12:33
here we have three values string integer and string and here also we are
supplying three values string integer and string again so remember to match this
exactly with one of the constructors which is available in the class if it doesn't
match it is going to fail it will not work so if i for example if i do this then
i'm supplying four arguments to the student class but there is no constructor
matching four arguments so it is going to complain you see this red line and the
code is not compiling if i remove this then it is matching it

13:07
and that's how you basically create a student object so an object is
basically an instance of the class remember that very well an object is an instance
of the class once you have created an instance of the class then you can call any
method like say let's say you want to read the name so you can call get name you
can read the name similarly if you want to so if you if you want to read the age of
this student then you can do that as well so let me just copy this and do this
let's call get age i want to print the name i want to print

13:40
the age and i also want to print the address so get control space i get
suggestion address yep so i'm printing the name i'm printing the age and i'm
printing the address after i have initialized the object successfully by using this
constructor representation using the new keyword so now let's run this method or
run this class basically okay so when i run this class the whole class got compiled
the constructor the getters and the setters and the properties and their types
everything was set then the main got called and i

14:14
created a new object from this classes blueprint the object name was john
then i called john.getname to print the name which is stored inside this particular
instance of the student class similarly the age of age which is stored in this
particular instance so we stored the name as john so when we call john.getname we
get john we got when we call john.getpage we get the age25 and we call
john.getaddress we call we get this particular address whatever values you
specified here the constructor got called here and the name

14:49
john got set to the student's name value the age value which you supplied
got set against the students age and the address value which you supplied got set
against the student's address and that's how you are going to create a class so
remember whenever you create a class identify the properties and the behaviors like
setters and getters write a constructor which can cover all the values which you
want to initialize and after that just initialize that class create an object of
that particular class

15:20
basically and once you have the object then you can call getters you can
also call this in this way if i don't supply any of this then it currently it
complains and why it complains i will cover that in the following session when
whenever when i will cover the multiple constructors concept but remember you see
this behavior here that if you have created a constructor which accepts some
arguments and if you supply this kind of initialization then student class does not
have a constructor which is parameter less

15:50
student class has only one constructor which is taking three parameters
and that's why java complains because it is not able to find a representation of a
constructor which accepts no parameter but like i said we will cover this we will
find a solution to this particular problem in the in the coming uh in the coming
session but for this session this is all we are going to cover we covered about
classes we covered about objects and we also saw how we can write getters and
setters we can initialize those

16:18
methods uh initialize those objects and we can access the properties of
those objects using the getters and we can also call the setters to modify the
values as well similar to this choose from over 300 in-demand skills and get access
to 1 000 plus hours of video content for free visit scale up by simply learn click
on the link in the description to know more let's get started with access modifiers
in java so when we talk about access modifiers let's understand what is the
underlying philosophy of the reasoning behind this

16:51
and the reasoning is very straightforward it can be even wrapped into a
single word which is called security the reason is that you will be creating
multiple kinds of objects and variables while you write your java program and you
should always write your java program or any program in a manner that only the
intended classes packages modules or applications should be able to see what they
are supposed to see so as a consumer application i should only be able to consume
the variables which i am expected to consume i should not get

17:26
any variable or i should not even have access to any variable which i am
not supposed to access so the idea is providing the least privileged principle give
access to different functions different variables different programs only by
providing them the kind of access which they need to run efficiently and do not
give them complete access so with that notion in mind java created the concept of
access modifiers and it created these three modifiers which are called public
protected and private if you do not

18:01
provide any modifier then it goes to no modifier or some people also call
it as default modifier which is also a type of access modifier so let's understand
theoretically first how it works and then we will try to have a look at an example
let's understand the first one first of all these modifiers are keywords and you
can use these keywords in front of classes in front of variables and also in front
of methods and you might have already noticed this particular keyword called public
in front of all the classes i've created or

18:33
even when we talked about the public static void main method we saw this
public as a keyword it is basically an access modifier and whenever you put public
in front of a class or a variable or a method that particular component becomes
available to the whole of the program it will be available to the whole world
basically any subclass or any package or any other class which might have been
written in your java program will be able to access that particular class or
variable or method if you put public in front of that the

19:07
second modifier level is protected one of the other thing which you you
will understand as i explain this is that as you move down into this particular
table the access level will start getting more and more restricted by restriction i
mean that less and less components will be able to get access to those modifiers so
if you write protected in front of a class or in front of a method or in front of a
variable then everything within that particular class will still be able to access
it everything within the package will still

19:42
be able to access it i still haven't covered the concept of package but i
briefly touched upon it showing you how whenever we write a class we have a package
in front of it and package is technically is just an aggregation of the classes if
you have to ripe write multiple classes and wrap all of those classes into a single
logical bundle that would be called a package just to show you an example these all
are packages which you see here you see this square box here and you see that these
names these all are packages and these

20:12
packages contain multiple classes and technically that's all the packages
mean that you can wrap multiple related classes into a single package so coming
back to the access modifier whenever you write protected in front of a class or a
variable or a method then that particular component will be accessible to everybody
within the same class everybody within the same package it will also be accessible
to everybody which is a subclass of the class this is again a new concept which i
have not covered but this basically means a child

20:44
of this class now what is a child of the class we will cover at that
concept in detail when we will talk about inheritance but for now you can
understand that it is a basically an extension of the class so when the moment you
declare something is protected it can easily be accessible within the same class in
which this protected component was declared it can be accessed in the package it
can also be accessed in the subclasses of the class but anything outside the
package will not be able to access it because

21:14
you see an n here then we have the default access modifier or no modifier
if you do not write anything in front of a class or a method or a variable it gets
the default access modifier privileges and that privilege mean that anybody within
the same class will be able to access that default variable or a default component
anybody within the package will also be able to access it the subclasses of the
class where the default component was declared those sub classes will not be able
to access it and anybody outside the package will

21:47
obviously not be able to access it then comes the last one which is the
most restricted privilege which is called private so if you put private in front of
a class or in front of a method or in front of a variable that component will only
be accessible within the class where it was declared it will not be accessible in
the package it will not be accessible in the subclasses and definitely not be
accessible to anybody outside the package so private is the least privileged or
least access permission

22:19
level you have in java once you write private in front of any component
that only the the classes other variables the the same class in which you declare
this particular private variable only that class itself will be able to access it
and nobody else will be able to access it and as you move above the accesses become
more and more lenient you can see it is just one y and three n here the moment you
move a bit above to the default modifier you get two y's so everybody in the class
and everybody in the package will

22:48
be able to access that particular component rest of for the rest of the
other world it is still unknown when you write something as protected then
everything in the class everything in the package and everything in the subclasses
will also be able to access it rest of the world cannot access it and if you write
something as public then anybody can access it it's like why everywhere so that is
how the access levels work now let us understand this with the help of example so i
will go here and i've just written

23:15
a very simple dummy class here which has uh which is a class called test
and i have a method here which is a display method so very simple class nothing
fancy here and if you if you notice i have not put any kind of public private or uh
or protected modifier in front of the class so this will get the default privilege
if something has default privilege then the whole world will not be able to access
it so let's go to a different package let's go to this package which is here and
let's try to access this particular

23:48
test class so we will try to access this test.java here in the main class
so let's try to do that let's say test t equal to new test i'm just initializing
the same class so what do i get i get a compile error let me try to fix it if i can
and i cannot fix it if you see i'm just trying to use this particular test class
into a different package but that package is not able to see the class at all i'm
getting a compilation error and if i try to click on the error generally eclipse
provide me suggestions to fix the to

24:22
import this particular class but eclipse is not able to find and find any
class with the name of test which can be imported in this package which is a
different package than the one of the test class so we cannot access it because
it's in the default access now let's go back to the test class and let's make it
public so i've just made it public here and now if i click on this again i get the
suggestion if you see the suggestion was not there earlier now it says import test
and if i click on this double click

24:50
it gets imported and everything is happy so you can see the whole access
modifier in effect here the default walls is versus public you can also try the
other other kind of access modifiers let's try private as well i think private will
also have the same effect because it will still not be accessible from the package
or from the subclass from or from the uh from the whole world we already saw this
particular in effect but we will see the private in a in a different way now so
going back to the test class i have
25:18
made the class as public but the method here is default i have not
exposed this method to be used outside the class let's see what happens if i try to
call this display method from the main class so i have successfully initialized the
class and if i say t dot and if i look at the suggestions here i do not get the
suggestion of the display method why because it is default if i go back make it
private can i now access it let's try again nope still we can't access it eclipse
is not even able to find it inside this package

25:56
because it is private let's go back and make it public so i've made it
just public now going back to the main class and again saying t dot now i get the
display suggestion as the first suggested method here and now i am able to use the
display method once i have made it public so you see the moment you change the uh
the access modifiers the changes are propagated immediately and you can see the
public versus default versus private in play here we can even go into further
detail by using the protected but for protected

26:28
i have to explain the concept of subclasses so i will cover this
protected versus subclasses concept when i talk about inheritance and there we will
see the usage of protected variables and the protected access modifier but we saw
the usage of public the default modifier and the private modifier do check out the
documentation for these access modifiers and i would strongly suggest you to play
around with this get more comfortable with this concept of different kind of
modifiers let's also run an example for for

26:54
understanding the default modifier in a better sense because we can see
that for the package it says why right so let's go back to eclipse and within the
same package where we created test class let's create another class let's call it
test demo so i will just click finish i get a new class let me also write the
public static void main method here so i get the public static void main method
here and let's try to access the test class here within the same package so let me
again go back to the default privilege here so i put the test

27:30
class access as the default class or default access and same goes for
display so can other classes in the same package access the test class now let's
see that if i go to test demo and here if i try to get the test class initialize
does it work or does it give me a compilation error yes it works it works because
the test class and the test demo are in the same package and test class has that
default access privilege so let's try to run one more variation of it to understand
the usage of the private method better going back to the

28:03
test class this display method has the default privilege right now so
going by that logic if i say t dot i should be able to get access to the display
method and yes that is possible if i run this particular program i will be able to
get whatever is given out by the display method which is hello world now let's try
to make this particular method as private and let's try to access this method from
one of the other class in the same package as of the test class the class is
default the method is private
28:37
and if you see this class has already turned red there is a compilation
error because now the display method is not accessible to the testdemo class even
if it was in the same package it's not accessible because the display methods
privilege or access modifier has gone to private now exactly the same as explained
here because it has become private so the package level members cannot access it
there's an n here so this is this was another variation to show you how you can use
public protected and private together to only

29:08
expose the members methods and classes which are supposed to be exposed
to the consumer this is a very critical and very important concept to be used in
java and it is used from a very simple program to all the highly complex
application this is sort of the bare bone or the laying foundation for any kind of
basic security level premise which you need to build your application let's get
started with inheritance so inheritance is basically one of the most important
properties of any object-oriented

29:38
programming language it's basically a concept and the concept says that
what if you have a scenario where you want multiple classes to share similar
behavior and similar properties let's understand this with the help of an example
let's take the example of a bank when you go to a bank to open an account there are
multiple types of accounts there can be a savings account it can be a type of
current account there can also be a different offering for personal account and
commercial accounts such as

30:08
for businesses then you may also have loan accounts which are having
different kind of properties so all of these types of accounts are still
technically accounts and they will share some similar properties for example all of
them will have to maintain some sort of an interest rate for example all of them
will be used to either deposit money or withdraw money so there would be some kind
of similarity across all of these types of account classes but at the same time
there will also be some sort of differentiality amongst these

30:40
classes the way you will calculate the interest rate for a current
account will be different from the way you will calculate the interest rate for a
savings account and the same applies for commercial account personal account loan
account etc so you see that we have some common behavior but at the same time we
also have some different behavior so whenever you have such scenario always think
about applying the concept of inheritance in your application there's another
simple way to spot whether inheritance can be used at a particular

31:11
place or not inheritance is also called is a relationship so whenever you
read the requirements from a customer just try to find the words which use this
particular phrase is a for example car is a vehicle savings account is a type of
account similarly you will find many other types of similar examples so whenever
you will see that is a relationship between two different kinds of objects or two
different kind of entities that means that there might be an inheritance
relationship being applied between them
31:46
and that's how you will spot the usage of inheritance so so far we have
understood this concept that there would be some sort of relationship between the
two classes what will be this relationship the relationship will be of parent child
type there will be a parent class some people also call that as base class and then
there will be a child class so the child class will be inheriting the properties
and the behaviors from the base class or from the parent class i will be using base
class and parent

32:15
class terms interchangeably during this session but the basic idea and
basic premise is like that only that there will be a child class and there will be
a parent class and the child class class is extending from the parents class
remember the word extend because we are going to use that and i've just shown you
the uh what you see here is basically the official definition which is given by
javadocs here we can read the definition as well and the basic idea which i told
you lies in the parent and child relationship and this is how

32:44
you can conceptualize it that you will have a base class or a parent
class and then you will you can have multiple children of it and then those
children can again have their own children and so on so forth so this hierarchy
this whole inheritance hierarchy can go as deep as you want it to be and that's the
basic idea and basic conceptualization of how you understand and how you visualize
inheritance now let's move to eclipse ide and try to understand this with the help
of an example so for this

33:15
particular case i have created a set of classes basically which we will
be using to demonstrate the relationship of inheritance and i've taken the simple
example of a bicycle so a bicycle is again a very generic definition of any two
wheeler and bicycles can have multiple types there can be an all-terrain bike there
can be a mountain bike there can be a city bike there can be a bike which is
specific specifically made for ladies and similarly there can be multiple different
types of bikes but all of these bikes will have some

33:48
similar properties for example all of them will have two wheels all of
them will have a seat all of them will have brake all of them will have some sort
of some sort of a speeding mechanism whether they can accelerate or de-accelerate
so there will be some commonality but at the same time a mountain bike's features
might be very different from a city bike feature a mountain bike features might
feature mountain bike might have gears for example whereas a city bike might not
have gears because you don't need that

34:20
much of a variation in in inclination and declination when you drive down
the road so i'm trying to conceptualize the same example here where i've created a
class called bicycle and this is my base class i define two properties in the in
the bicycle class which is gear and speed we will come back to protected in a while
but let's let's leave that there so i have two properties which is gear and speed i
have a constructor where i'm initializing the starting speed and the starting gear
and then i have three methods here which says set

34:52
gear apply brake and speed up when i apply brake the speed is going to
get reduced so that's why you see minus equal to unity operator and when i'm trying
to speed up the speed is going to increase that's why i'm using the plus equal to
unity operator so when you use the method speedup the speed is going to be
increased by the value you supply in the method similarly when you apply the break
the speed is going to get reduced by the amount you supply in the method argument
and that is my base class

35:24
bicycle gear speed and three methods now let's have a look at the child
class so the way you will define the inheritance relationship in java is by using
this extends keyword remember i told you to remember the extends keyword so what
you will do that you will write your class name as usual and then you will use
extends keyword and then you will use the parent class name or the base class name
so here i am saying class mountain bike extends bicycle and here i am providing an
additional property remember the whole commonality

35:56
versus differentiality equation so a mountain bike might have an extra
property to adjust the seat height so that's the extra property which mountain bike
is having and then since this mountain bike is extending the bicycle class this
mountain bike class has the responsibility to initialize the bicycle class as well
and the way it will fulfill that responsibility is by taking all the arguments of
bicycle class plus the mountain bike class in its constructor and that's why you
see three different properties here you see the

36:35
start height property which is the property of this particular class
which is the seat height and then you see the start speed and start gear property
which directly are not available in this class but this class is inheriting those
properties from the base class remember the keyword inheritance here and then once
you do that how do you initialize the base class constructor is by calling the
super method this is again a keyword in java and you will use this super keyword to
refer to the base class constructor in

37:06
this way so what you will do technically is that you will write super and
you will supply the arguments which is required by the base class constructor if i
go back the base class constructor requires two arguments start speed and start
gear so i write super and then i write start speed and start gear so whatever i got
from here in the constructor of mountain bike i'm supplying those values as is to
super and java will automatically take care of invoking this particular constructor
the moment it

37:39
uh encounters this kind of statement another another thing to notice is
that the super keyword or super statement has to be the first statement inside your
constructor remember that whenever you are dealing with inheritance and whenever
you have to initialize the super class constructor or the base class constructor
use the super constructor invocation as the very first statement inside your child
class constructor otherwise java will not compile the program and then you will
initialize the local variable i should

38:10
call this as this remember i we discussed about this in this in the
classes session okay and then it just has a method of set height so there's no
extra method which this mountain bike is providing but this mountain bike will have
access to all the methods which have been provided here and all the properties
which have been provided here this mountain bike can access everything from the
bicycle class and that's the whole idea of inheritance that the child class can
access the properties of the base

38:40
class by extending the base class now let's look at how do we invoke this
particular class so for that i have a simple class name as inheritance demo which
has a public static void main method and then i am initializing the mountain bike
class i'm supplying three values as 20 10 and 1 so the 20 goes to start height the
10 here goes to start speed and one here goes to gear or the start gear and that's
how i'm setting the values so when i call this particular constructor here the
mountain bike constructor is

39:17
going to this mountain bike constructor is going to get called and which
in turn is going to call the base class bicycle class constructor as well and
everything will be set accordingly and then let's try to access the gear property
remember gear is not directly available in the mountain bike class but i'm still
able to do mountain bike dot gear because child class can access the parent's class
properties similarly the same thing goes for speed as well and seat height is the
local property of this particular class

39:44
mountain bike so it will anyways be accessible so i can access all the
properties of the base class and the child class itself seamlessly without any
differentiation now let's try to call a method so here i'm calling the method apply
break remember apply break was defined in the base class not in the child class
it's defined here so i'm calling the apply break method and i want to reduce the
speed the current speed by one and then i'm printing the current speed saying that
bike speed after applying break is mountain bike

40:18
dot speed so let's run this particular program and observe the outputs
okay so the the first statement which gets printed is at line number seven which
says gear is one because it's coming from here then the seat height is mountain
bike dot seat height which is 20 which is the first argument here and then the bike
speed is 10 which is coming as here and getting printed here and then when i apply
the brake let's go back to the apply brake method the logic is that whatever is the
current speed reduce it

40:51
by the value i supply in the method so if i supply 1 10 minus 1 becomes 9
and that becomes the current speed if i supply 5 here the current speed will be 10
minus 5 equal to 5. let's assert that particular assumption as well yes we can see
that and similarly you can access other methods of the base class as well if i just
do control space here and you can see apply break is accessible set gear set height
speed up all the methods which are defined in the base class are accessible in the
child class as well

41:23
this is the power of inheritance one more thing which i said i will come
back to is around the usage of protected access modifier remember when we discussed
about the access modifiers in the previous session i said that protected is used
when you are dealing with inheritance that whenever you define something as
protected it will be available to be accessible in the child classes of the base
class so if i define this particular variable as as protected then this particular
property will be accessible in the child class mountain

41:55
bike as well let's play around this let's make this as private and see
what happens so if i make this private now when i say private the scope of this
particular variable is within the class only and nobody outside the class will be
able to see it and you can see the effect of it i can see an error being coming
here in my inheritance demo class because now mountain bike object cannot access
the gear property because the gear property is defined in the base class and now it
is private that's why the child class

42:26
now mountain bike class cannot access the property gear and the moment
you make make it back to protected it will be accessible the error is gone there's
no red line here so that's how you can you can use the protected access modifiers
it is always always recommended to use them when you when you write any kind of
inheritance logic and inheritance logic as well intentionally if you want to hide a
particular property from the child classes just make it private and that's all i
want to cover in this particular session where

42:57
we dive deep into the concept of inheritance we had a look at an example
using the bicycle and mountain bike kind of construct and you can apply the same
kind of construct on any kind of other other kind of business requirement remember
there is a relationship let's get started with encapsulation so again let's
understand what is encapsulation in general and then we will apply that concept to
java encapsulation if you try to break that word if i just write it here in the
form of a comment it's n

43:28
capsulation just trying to break this word into to make it more visual so
you see you see a word called capsule here so if you try to imagine a capsule why
do you create that and what's the concept of a capsule capsule tries to pack a lot
of things inside a small tube-like shape and the concept of encapsulation is sort
of inspired from that not exactly not literally but that's the basic idea of of
this root word which is called encapsulation so encapsulation basically means to
restrict accessing the properties of a class to

44:06
the outside world if you want to protect the properties of your class how
do you do that you do that with the help of the concept called encapsulation so
here you this is a sort of a security feature you can think of a think of it as a
security feature that you want to protect the properties of your class and you will
use encapsulation to protect the properties how you implement encapsulation let's
have a look at that now so basic idea is to encapsulate the objects create sort of
a capsule around

44:41
the object which can provide that protection layer and you will define
how the outside world is going to access the properties of the class if needed so
i've created a simple class here and it's if you if you remember one of the
previous examples when we discussed about classes i used the same class which was
the student class and i'm just using the same class for continuity so i have
created a student class here and i have created three different properties of this
particular class which is name

45:12
name of the student age of the student and address of the student you see
intentionally i have used the keyword private in front of these three properties
and that is the first thing which you need to do to implement encapsulation on your
object that you hide all the properties from the outside world remember the moment
you make anything is private only the class which is the student class itself only
the class itself can see these variables or these properties nothing outside the
student class will be able to access the

45:49
name or age or the address directly from the student class it's not
possible that's the whole idea of making it private because you don't want the
outside world to access these properties so you make those properties as private
then you create a constructor the constructor has to be public so that anybody can
initialize the student object because you want your object to be accessible
remember so you want the objects to be accessible but you want the restriction on
the access of the properties of the object

46:19
and these are the properties of the object so you have restricted them by
making it private but that doesn't solve the problem because there will be some
illegitimate consumers of the student object which should not have access to the
student properties but there will also be some legitimate consumers of the student
object which should be having access to the properties of the student for example
the payroll application will need access to these particular properties the
examination application

46:51
will require access to these particular properties so how do you do that
you create getters we call them getters because they start with the keyword get so
you create a getter method for each of the property you create the method called as
get name and this is the naming convention with java promotes that you write get
and then you write the name of the property in camel case so you write get name and
this property is supposed to return the name property of this particular object and
you here you say return name so you're returning

47:27
the the name which is being stored by the current student object whenever
somebody calls student object dot get name and this is the way you have restricted
the access of this particular object as well so remember you made it private so
that nobody can access it and then you defined your own way of providing access to
the name property of this particular class now if anybody wants to access the name
value of your student object there's only one and one way to do that which is via
calling get name so you have

48:00
provided restrictions and you have also provided prescriptive guidance on
how you are going to expose the properties of your class that's the second uh thing
which you need to do to implement correct form of encapsulation on your object that
that is to provide the getter methods with the right visibility you want to make
them public so that anybody can call them but since you are governing which is what
is written inside the method you have full control on what kind of exposure you
want to give for this particular

48:33
property similarly you create a getter for age calling getage which
returns the age and then you call get you write get address and return the address
here it is simply returning it but if you want to do more stuff here feel free to
do that like i said you govern the control of how do you want to return the name
maybe you don't want to return the complete name and you want to just return the
initials then you can split the string trim the string and return only the initials
so that's the basic idea of getters

49:04
now after you have created the object there might be use cases where
somebody needs to overwrite the values of the properties like name age or address
whatever student's name was incorrectly mentioned when they called the constructor
and now they want to overwrite the name or fix the name so basically they have to
replace the existing value of the name with a new name so in that case you need to
provide what we call as setters and they start with set that's why we call them
setters and we follow the same kind of logic that

49:37
you provide the setters for all the properties of your class so you had
three properties which were named age and address and you provided the setters for
all three of them and how do you provide that you write you again make them as
public because you want the outside world to access this setter method to overwrite
the values of the name this method is not going to return anything so it is going
to be void then you write the name of the method which starts with set and then the
property name in camel case and then you provide

50:07
the new value which is the value which should be which should be the new
value and this is the value which should replace the existing value so whatever new
value you want to supply you supply that as the argument of the setter methods for
set name for set age or for set address and then you write this particular line
which says set the current name property of this particular object with the name
which is coming in from the argument so this dot name remember this dot name
represents the current

50:41
object's property because this represents the current object so whatever
is the current object property value of the name is just override that value with
the argument coming in do the same thing for age and set age method and do the same
thing for set address in the in the set address method so this is how you are going
to provide the setters and the getters so setters and getters can also be generated
in smart id is light like eclipse so you just right click you go to source and here
you will see the

51:13
option of generate getters and setters if you click on this it will give
you the list of the fields which are available to be had to be having getters and
setters currently it says no fields or all fields have getters and setters already
yes because they have it let me just comment this and show this how this work so
for example let me comment all the getters and setters so i'm commenting them and
if i right click source generate getters and setters i get all the fields available
for which setters

51:45
and getters can be generated so you can select them and leave everything
as default hit generate and it is automatically going to generate the getters and
the setter you see all the code of the getter and setter is automatically generated
you do not even need to write it and this is the power of the smart ideas let me
just revert what i did so you created everything as all the properties as private
and then you define how you want to replace the values by providing the setters and
you define how you want the

52:18
outside world to access your properties so you have full control of the
class now you you control the visibility you control how they are going to get
initialized you are you are controlling how the properties are going to be
overwritten or set and you control how the properties are going to be returned so
you have complete control of the class so you have created a perfect capsule here a
perfect form of encapsulation in this class and this is how you can implement
encapsulation let's get started with abstraction let's first

52:48
understand the definition of abstraction before we relate this to java
abstraction generally means hiding something when you want to hide some details of
of a particular object from the outside world you apply the concept of abstraction
for example when you drive a particular car you just hit the accelerator and the
car speeds up now it's not only one action when you hit the accelerator the whole
machinery of the car and all the components different parts of the car work
together to speed up they would be engine there would be

53:23
propulsion there would be fuel there would be there might be some if it's
an electric car they would batteries uh there would be whole different kinds of
pistons and plugs which will make the car speed up it's not only a single component
it's a lot of different components under the hood and that's the concept of
abstraction that we only show the relevant parts to the user and we hide all the
internal details from the user that's the basic idea of abstraction so a car
manufacturer abstracts away all the inner technical

53:54
details from the driver and just gives the driver a single pedal to speed
up the car for a user it's a very good experience because he doesn't need to know
about all the internals of the car but the car manufacturer can hide all the
details and provide a very simple interface for the driver to interact with which
is just a gas pedal and that's the basic idea of abstraction and we try to apply
the same kind of concept to java in the form of abstraction and java provides this
kind of concept implementation with the help of a

54:25
keyword which is called abstract now abstract is a keyword which can be
used in front of a class or a method when you use it in front of a class the whole
class becomes abstract abstract class is a type of class so the class itself
becomes an abstract class one of the things to remember is that abstract class
cannot be instantiated but can be subclassed remember we talked about the concept
of subclass and the parent class when we talked about inheritance so if you make a
class as abstract class you

54:58
cannot call the constructor of the class because you cannot instantiate
it but you can extend the abstract class and create a child class class out of it
that's the concept of abstract class another concept is abstract method you can
also use the abstract keyword in front of a method and when you use abstract
keyword in front of a method then that method is only declared without an
implementation you are not going to provide any implementation of the method and
you're just going to declare the method something like this

55:29
we will see a similar example in the in the hands on as well but that's
the basic idea that you just declare the method you are not providing an
implementation as to what does move to method actually does because that's the
whole concept of abstraction and it is the responsibility of the child classes
which inherit the particular class in which the abstract method would be sitting so
that child class will be having the responsibility to provide a concrete
implementation of the declared method let's understand this better with

55:59
the help of an example so here i have created a class called graphic
object and i have made the class as abstract class this is the keyword which you
need to use in front before the class keyword and the task becomes an abstract
class now when you create an abstract class it automatically gets some properties
the first property is that this particular class must have at least one abstract
method and this is how you define the abstract methods where you just provide the
declaration but you do not provide

56:31
the implementation of the draw method or the resize method and the way
you will create the abstract methods is by using the abstract keyword before the
actual method declaration starts so general method declaration would look like this
and if you use abstract in front of it this becomes an abstract method declaration
abstract class can also have concrete methods so that is the liberty which abstract
class gives you that you can have abstract methods which are just declared but you
can also have normal
56:59
methods which are which where you can also provide the implementation so
you can have normal methods and abstract methods as well in an abstract class so
this is the abstract class now if if you want to provide an implementation of the
draw and resize method you have to extend this abstract class so we extend this
abstract class by creating two child classes out of this particular abstract class
which is circle.java where you can see i create a normal class and i use the
extends keyword for inheritance and i'm saying that circle

57:32
class is inheriting from the graphic object class which was my abstract
class and once i use this extends keyword then i have the responsibility of
providing the implementation of the abstract methods actually java will force me to
provide the implementation of these two methods if i do not provide all the
declared methods so there are two declared methods here so if the circuit class
does not provide that the implementation for both draw and resize methods then java
will throw an error and it will ask you to take an action

58:08
which can be either define all the methods meaning provide implementation
for all the methods or make the circle class itself as abstract let's see if i just
comment this particular out i get an error you see a red line here and if you click
on this particular balloon here it will say either you can add the unimplemented
methods or you can make the circle type as abstract these are the two options which
you will get to fix this particular error so an abstract class can be extended by
another abstract class if the child abstract

58:40
class is not implementing all the methods of the parent if you want to
implement all the methods then just implement them and keep that keep it as a
normal class and that's what i've done here i've just created a normal class here
and provided the implementation and once you provide the implementation of both of
the methods the error is gone similarly i have also provided another child class
because it's uh there can be there can be multiple children of a particular parent
so rectangle also extends from the graphic object and it

59:09
also provides its own implementation of drawing and resize because
generally the way you will draw a rectangle and you resize a rectangle will be
different than drawing or resizing a circle here i'm not doing anything in the
methods because this is just to show you the demonstration i'm just printing a cis
out saying drawing a circle and resizing or drawing a rectangle and resizing a
rectangle and in the circle class drawing a circle and resizing a circle so one
abstract class is the parent class which is the graphic object and

59:39
two children circle and rectangle both providing the implementation of
the abstract methods which were declared in the base class now let's go to the main
class where i've defined defined the public static void main method and here you
see i have created two different kind of blocks of code here one block is where i'm
saying graphic object creating a reference of the graphic object named as circle
and initializing the circle object now you see a difference here here is the
abstract class and here is
01:00:11
the concrete class so whatever you define on the right hand side whatever
is the type of object will be the type of methods being caught so if you created a
circle object and then if you call the draw method and resize method these methods
will be called from the circle class if you initialized it as new rectangle then
you are creating a rectangle class object and then the draw and the resize method
will be called from the rectangle class that's the basic construct and that's how
you will initialize

01:00:41
a particular class which is using an abstract class so i i call a graphic
object circle new circle and i call circle.draw and circle dot resize similarly
graphic object rectangle equal to new rectangle and rectangle.draw rectangle dot
resize let's run this program and observe the output so when i call this particular
block of code circle object gets initialized and then circle.draw method prints
drawing a circle and circle dot resize method prints drawing resizing a circle
similarly when i create the rectangle

01:01:15
class object referencing it with the graphic object class rectangle.draw
prints this particular line drawing a rectangle and rectangle.resize prints this
particular line which is resizing a rectangle and this is how you will use the
concept of abstraction there's another very interesting thing which which is which
is going on here remember i told you that the abstract classes cannot be
instantiated so if i go here and i try to create a constructor of the graphic
object class let's see what happens if i

01:01:47
do this and i i create i just put a system out here base abstract plus so
if i do this here if i create if i still write a constructor though java says that
it cannot be instantiated if i still write a constructor of it and i do not do any
changes here and if i run this particular program let's see what happens you'll see
the base abstract class being called here let me just reformat it by putting ln
here so that we get a better output so if i run this particular program you see
base abstract class is getting printed somehow though

01:02:23
we said that abstract classes cannot be instantiated but still the
constructor of this is still getting called somehow when i create a circle class
the base class constructor gets called and when i cl create a rectangle class again
the base class constructor gets called this is this is the power of inheritance
because in inheritance remember how it works when you call when you initialize a
particular child object referencing it with the parent object see the references of
the parent class and the

01:02:51
object is of the child class so whenever you do it like this first the
default constructor because i'm calling the default constructor here the default
constructor of the circle class will get called which will go here remember i told
you that java provides the default constructor automatically if you don't provide
it so the default constructor of circle gets called but java sees that you are
writing this code here which says circle extends graphic object so java is
automatically going to call the parent class and going to
01:03:21
initialize the parent class as well because that's how it will be able to
see these methods and so so that's why it will automatically call the base class
constructor even if you don't call it explicitly in fact you cannot call it
explicitly java will not allow you to initialize the graphic object class
explicitly it can only and only be called by java itself indirectly via the concept
of inheritance if you try to do this let's try to do this for fun and and also for
better understanding so if i write graphic object go equal to

01:03:54
new graphic object you see i don't even get an option but let's write it
and let's see what happens so you see that this particular it gives basically an
error because it's not able to find the constructor of the graphic object class
though even i have defined the default constructor here but still java is not able
to find it because this class is abstract so hence proved abstract classes cannot
be instantiated explicitly they can only be instantiated by the child classes
through the internal mechanisms of java itself and

01:04:27
that's all i want to talk about in this particular session where we
looked in detail about the concept of abstraction couple of more things which i did
not cover in the previous session i just want to call them out for clarity java
does not support multiple inheritance when i say multiple inheritance what it means
that you will not have a scenario where a child will be extending from two base
classes java does not support it it does support it with the with the help of
interfaces but i will talk about

01:04:56
interfaces in the coming sessions but here just understand this that you
cannot use multiple inheritance with classes in java a child cannot have two parent
or two base classes in java java will not allow that it is also called the diamond
problem because the child class does not know whether to follow the route of base 1
plus or the base 2 class if it is extending from 2 different base one and base two
classes so a class can only extend from one class in java we can find a workaround
of it by using the concept of interfaces

01:05:30
but that's something which will be coming later another thing to
understand is that when you create an abstract class here these variables you can
declare declare these variables as well and these variables will be accessible by
all the child classes as you access them let's get started with interfaces in java
now again let's understand what do we mean by an interface in general and then we
will see how do we apply the concept of interfaces in java so imagine let's let's
take the same example of

01:06:01
vehicle industry because that's something which we see every day around
ourselves and we can relate to it so imagine if all the car manufacturers were
creating a different way of driving a car what if in one car you have a steering
but in other car you do not have a steering but you have something else which is
which may be called something else in one car you have the gas pedal to speed up in
other car you have a button to speed up similarly in in one car you may have
different kind of doors and in other

01:06:32
cars you may have different kind of doors which are completely different
looking it's not even a single simple handle to open it so you see the problem here
the problem here is that all the car manufacturers are not working on a single
contract they are not respecting a single interface which will create a poor user
experience because the users whenever the users have to buy a new car or they have
to switch from one manufacturer to other manufacturer they are completely clueless
as to how to drive that particular class because the

01:07:03
interface they were using in the previous car is completely different
from the interface they are being shown in the other car and that's that's the
basic need for the concept of interface think of it as an contract agreement you
see it is also mentioned here that if you apply the same kind of concept to
programming world imagine if every programmer was writing their own logic in their
own way without following a single contract or a single guideline soon it is going
to create a problem and that's the kind of problem

01:07:37
interfaces try to solve so interface is again a keyword in java and the
basic idea or the basic construct is that the interfaces are going to define the
specifications of how a class would act let me repeat this in a more generic way
interfaces are going to define the contract or the specification which is going to
govern how an application or code or programming logic will behave that's the basic
idea of interfaces so interfaces are their own type and the basic property of
interface is that it

01:08:15
can only contain constants method declarations similar to abstract
classes and some default methods or static methods or nested types what is nested
type we will cover that later default methods will touch upon a bit here and the
static methods as well but the basic idea is that when you create when you create
an interface in java you cannot write anything concrete in terms of the behavior of
the interface when i say behavior always think of that as method so you cannot
define a concrete method in an interface

01:08:50
you can only declare methods you can also declare constants you can
create a particular type of concrete method by using the default keyword but use it
in very special circumstances you can also create static methods as well which are
only going to belong to the interface and nobody else now let's look at an example
and and also before we go to the example let me also phrase this here that if an
interface cannot define a concrete implementation of the methods then naturally the
interface is going to be

01:09:20
extended by a child class a concrete class which is going to provide the
implementation of the methods declared in the interface it does sound like abstract
classes but there is a difference let's understand the difference with the help of
an example this is how an interface will look like i've used the same construct of
bicycle and mountain bike just to maintain some continuity and less context
switching but this is how an interface would look like you will not use the class
keyword but instead you instead of that you will

01:09:50
use the interface keyword you can only declare methods you cannot define
methods remember when we looked at the abstract classes example we were able to
define concrete methods as well but in interface that's a strict no no you cannot
define concrete methods let me show you the abstract class in the case of
abstraction which we used this was the abstract class and it did have a concrete
method right but bicycle.java here which is because it's an interface it is not
going to allow you to create a

01:10:23
concrete method another thing is that if you try to declare a variable
inside the class they will have to be constant you cannot just declare them without
any value for example i cannot do this if i do this it is going to give an error
you see an error here and it says initialize field x at declaration something like
this public static final so interface is going to force you to declare all the
variables as public static final with some value if you try to use them inside
interface so that is also something which

01:10:57
interface will mandate so you see interfaces are more strict they are
more strict in terms of what you can do you can only declare public static final
variables and you can only declare methods you cannot define methods so abstract
class is less restrictive interface is more restrictive and that is the
differentiation which you have to keep in mind whether to use abstract classes or
whether to use interfaces that is also a very common confusing question in the
interviews as well and remember that interfaces are only for

01:11:29
contracts or specifications where in case of abstract class you can write
a concrete behavior as well in terms of a concrete method in the base class
interfaces there's no concrete implementation in the base class abstract classes
yes there can be a concrete implementation in the base class that's the basic
difference moving on i have created a child class here which is again mountain bike
and it has three properties seat height gear and speed similar to the previous
example it has a constructor and remember in again

01:12:01
interfaces cannot be uh initialized directly so you don't even need to
call a super method here because there is nothing to initialize you initialize a
class to create a state state is denoted by the properties but interfaces will
force you to write a public static final property and initialize its value
automatically so you do not need a constructor for the interface because there is
nothing to initialize everything has been initialized in terms of the variables and
the methods are anywhere just declared that's why you

01:12:29
don't need to call super here after that the moment you write this
particular keyword here implements and that is how you will basically extend from
the from the interface you will use the implements bicycle keyword here implements
is the keyword and bicycle is the name of the interface if you write this here then
java is going to force you to provide the implementation of these two methods if i
if i do this implements bicycle and if i comment out these two methods which is
coming from the base class it is going

01:13:00
to give an error and if i read out this error it says either you add the
unimplemented methods or you make the mountain bike class as abstract there are two
ways to fix this particular class similar to abstract classes it will force you to
write the implementation of the base class declared methods another thing which
java will will suggest you and will also force you via some ides as well is that
you use this particular annotation we use this particular annotation to tell java
that we are overriding this method

01:13:30
from the base class since apply break and speed up methods were defined
here in the base class and this child class is supposed to provide the
implementation of the methods by overriding them overriding is again a concept
which we will talk about in detail in the coming sessions but for now remember that
you need to write this annotation here on the methods which have been taken from
the base class to provide an implementation and the implementation is similar to
what we have seen before so whatever value you

01:13:59
supply in the apply break the speed is going to get decremented by that
particular value and in case of speed up whatever value you supply in the argument
that value is going to be added to the current speed and the speed is going to go
up let's look at the main class here which is called interface demo it has a public
static void main method and here i'm again initializing the child class nothing's
nothing strange here and then i'm using the getter method here because i have made
everything as private here

01:14:29
and rest of the things are just here as is i have the getters and setters
here for all the properties and i'm using those here so i'm using the getters to
just get the value of gear seat height and speed and then i'm applying the brake
and then i'm printing the current speed after applying the brakes let me just
expand this a bit more and then i'm using the speed up method trying to increase
the current speed of the bike by 10 and then printing the new speed again so using
the apply break and speed

01:14:59
up methods which are concretely defined in the child class by using this
override annotation and these were the methods which were declared in the interface
in the base interface and that's all it which is happening in this particular class
let's run this particular program and these are the values i get i get the gear as
1 which is coming from here i get the seat height as 20 which is coming from here i
get the current bike speed as 10 this was the value which it was initialized with
then i'm applying

01:15:31
the brake and after applying the brake the speed of the bike gets reduced
and it becomes 10 minus 1 which becomes 9 and that's what gets printed here and
then i'm speeding up the bike by 10 so the current value of the current value of
the speed of the bike is 9 and if i incremented by 10 it becomes 19 and that's what
gets printed here let's get started with inheritance so inheritance is basically
one of the most important properties of any object oriented programming language
it's basically a concept and the concept says

01:16:08
that what if you have a scenario where you want multiple classes to share
similar behavior and similar properties let's understand this with the help of an
example let's take the example of a bank when you go to a bank to open an account
there are multiple types of accounts there can be a savings account it can be a
type of current account there can also be a different offering for personal account
and commercial accounts such as for businesses then you may also have loan accounts
which are having different kind of

01:16:40
properties so all of these types of accounts are still technically
accounts and they will share some similar properties for example all of them will
have to maintain some sort of an interest rate for example all of them will be used
to either deposit money or withdraw money so there would be some kind of similarity
across all of these types of account classes but at the same time there will also
be some sort of differentiality amongst these classes the way you will calculate
the interest rate for a current account will

01:17:11
be different from the way you will calculate the interest rate for a
savings account and the same applies for commercial account personal account loan
account etc so you see that we have some common behavior but at the same time we
also have some different behavior so whenever you have such scenario always think
about applying the concept of inheritance in your application there's another
simple way to spot whether inheritance can be used at a particular place or not
inheritance is also called is a relationship so whenever you read

01:17:43
the requirements from a customer just try to find the words which use
this particular phrase is a for example car is a vehicle savings account is a type
of account similarly you will find many other types of similar examples so whenever
you will see that is a relationship between two different kinds of objects or two
different kind of entities that means that there might be an inheritance
relationship being applied between them and that's how you will spot the usage of
inheritance so so far we have understood this

01:18:17
concept that there would be some sort of relationship between the two
classes what will be this relationship the relationship will be of parent child
type there will be a parent class some people also call that as base class and then
there will be a child class so the child class will be inheriting the properties
and the behaviors from the base class or from the parent class i will be using base
class and parent class terms interchangeably during this session but the basic idea
and basic premise is like that only that there

01:18:47
will be a child class and there will be a parent class and the child
class class is extending from the parents class remember the word extend because we
are going to use that and i've just shown you the uh what you see here is basically
the official definition which is given by javadocs here we can read the definition
as well and the basic idea which i told you lies in the parent and child
relationship and this is how you can conceptualize it that you will have a base
class or a parent class and

01:19:16
then you will you can have multiple children of it and then those
children can again have their own children and so on so forth so this hierarchy
this whole inheritance hierarchy can go as deep as you want it to be and that's the
basic idea and basic conceptualization of how you understand and how you visualize
inheritance now let's move to eclipse ide and try to understand this with the help
of an example so for this particular case i have created a set of classes basically
which we will be using

01:19:47
to demonstrate the relationship of inheritance and i've taken the simple
example of a bicycle so a bicycle is again a very generic definition of any two
wheeler and bicycles can have multiple types there can be an all-terrain bike there
can be a mountain bike there can be a city bike there can be a bike which is
specific specifically made for ladies and similarly there can be multiple different
types of bikes but all of these bikes will have some similar properties for example
all of them will have two

01:20:18
wheels all of them will have a seat all of them will have brake all of
them will have some sort of some sort of a speeding mechanism whether they can
accelerate or deaccelerate so there will be some commonality but at the same time a
mountain bike's features might be very different from a city bike feature a
mountain bike features might featu mountain bike might have gears for example
whereas a city bike might not have gears because you don't need that much of a
variation in in inclination and declination when you

01:20:51
drive down the road so i'm trying to conceptualize the same example here
where i've created a class called bicycle and this is my base class i define two
properties in the in the bicycle class which is gear and speed we will come back to
protected in a while but let's let's leave that there so i have two properties
which is gear and speed i have a constructor where i'm initializing the starting
speed and the starting gear and then i have three methods here which says set gear
apply break and speed up when i apply

01:21:22
brake the speed is going to get reduced so that's why you see minus equal
to unary operator and when i'm trying to speed up the speed is going to increase
that's why i'm using the plus equal to unary operator so when you use the method
speed up the speed is going to be increased by the value you supply in the method
similarly when you apply the break the speed is going to get reduced by the amount
you supply in the method argument and that is my base class bicycle gear speed and
three methods now

01:21:54
let's have a look at the child class so the way you will define the
inheritance relationship in java is by using this extends keyword remember i told
you to remember the extends keyword so what you will do that you will write your
class name as usual and then you will use extends keyword and then you will use the
parent class name or the base class name so here i'm saying class mountain bike
extends bicycle and here i am providing an additional property remember the whole
commonality versus differentiality equation so a mountain

01:22:26
bike might have an extra property to adjust the seat height so that's the
extra property which mountain bike is having and then since this mountain bike is
extending the bicycle class this mountain bike class has the responsibility to
initialize the bicycle class as well and the way it will fulfill that
responsibility is by taking all the arguments of bicycle class plus the mountain
bike class in its constructor and that's why you see three different properties
here you see the start height property which is the

01:23:03
property of this particular class which is the seat height and then you
see the start speed and start gear property which directly are not available in
this class but this class is inheriting those properties from the base class
remember the keyword inheritance here and then once you do that how do you
initialize the base class constructor is by calling the super method this is again
a keyword in java and you will use this super keyword to refer to the base class
constructor in this way so what you will

01:23:33
do technically is that you will write super and you will supply the
arguments which is required by the base class constructor if i go back the base
class constructor requires two arguments start speed and start gear so i write
super and then i write start speed and start gear so whatever i got from here in
the constructor of mountain bike i'm supplying those values as is to super and java
will automatically take care of invoking this particular constructor the moment it
uh encounters this kind of statement

01:24:07
another another thing to notice is that the super keyword or super
statement has to be the first statement inside your constructor remember that
whenever you are dealing with inheritance and whenever you have to initialize the
super class constructor or the base class constructor use the super constructor
invocation as the very first statement inside your child class constructor
otherwise java will not compile the program and then you will initialize the local
variable i should call this as this remember i we

01:24:39
discussed about this in this in the classes session okay and then it just
has a method of set height so there's no extra method which this mountain bike is
providing but this mountain bike will have access to all the methods which have
been provided here and all the properties which have been provided here this
mountain bike can access everything from the bicycle class and that's the whole
idea of inheritance that the child class can access the properties of the base
class by extending the base class

01:25:09
now let's look at how do we invoke this particular class so for that i
have a simple class name as inheritance demo which has a public static void main
method and then i am initializing the mountain bike class i am supplying three
values as 20 10 and 1. so the 20 goes to start height the 10 here goes to start
speed and one here goes to gear or the start gear and that's how i'm setting the
values so when i call this particular constructor here the mountain bike
constructor is going to this mountain bike constructor

01:25:46
is going to get called and which in turn is going to call the base class
bicycle class constructor as well and everything will be set accordingly and then
let's try to access the gear property remember gear is not directly available in
the mountain bike class but i'm still able to do mountain bike dot gear because
child class can access the parent's class properties similarly the same thing goes
for speed as well and seat height is the local property of this particular class
mountain bike so it will anyways be

01:26:13
accessible so i can access all the properties of the base class and the
child class itself seamlessly without any differentiation now let's try to call a
method so here i'm calling the method apply break remember apply break was defined
in the base class not in the child class it's defined here so i'm calling the apply
break method and i want to reduce the speed the current speed by 1 and then i'm
printing the current speed saying that bike speed after applying brake is mountain
bike dot speed so let's run this particular

01:26:47
program and observe the outputs okay so the the first statement which
gets printed is at line number seven which says gear is one because it's coming
from here then the seat height is mountain bike dot seat height which is 20 which
is the first argument here and then the bike speed is 10 which is coming as here
and getting printed here and then when i apply the brake let's go back to the apply
brake method the logic is that whatever is the current speed reduce it by the value
i supply in the method

01:27:20
so if i supply 1 10 minus 1 becomes 9 and that becomes the current speed
if i supply 5 here the current speed will be 10 minus 5 equal to 5. let's insert
that particular assumption as well yes we can see that and similarly you can access
other methods of the base class as well if i just do control space here and you can
see apply break is accessible set gear set height speed up all the methods which
are defined in the base class are accessible in the child class as well this is the
power of inheritance one

01:27:52
more thing which i said i will come back to is around the usage of
protected access modifier remember when we discussed about the access modifiers in
the previous session i said that protected is used when you are dealing with
inheritance that whenever you define something as protected it will be available to
be accessible in the child classes of the base class so if i define this particular
variable as as protected then this particular property will be accessible in the
child class mountain bike as well
01:28:23
let's play around this let's make this as private and see what happens so
if i make this private now when i say private the scope of this particular variable
is within the class only and nobody outside the class will be able to see it and
you can see the effect of it i can see an error being coming here in my inheritance
demo class because now mountain bike object cannot access the gear property because
the gear property is defined in the base class and now it is private that's why the
child class

01:28:53
now mountain bike class cannot access the property gear and the moment
you make make it back to protected it will be accessible the error is gone there's
no red line here so that's how you can you can use the protected access modifiers
it is always always recommended to use them when you when you write any kind of
inheritance logic and inheritance logic as well intentionally if you want to hide a
particular property from the child classes just make it private and that's all i
want to cover in this particular session where

01:29:23
we dive deep into the concept of inheritance we had a look at an example
using the bicycle and mountain bike kind of construct and you can apply the same
kind of construct on any kind of other other kind of business requirement remember
that is a relationship let's get started with working through a particular project
using all the concepts which we have learned so far so in this session we are going
to focus on a particular project and the project we are going to focus is is about
an atm application so you might have interacted

01:29:52
with different atm stores in your cities where you go in you insert your
card you enter your pin and then you can view the balance you can withdraw some
amount you can also deposit some amount from the atm so we will try to look at it
from that perspective and and we will see what kind of different uh java and oops
concepts we can use to build such atm application this is not a super versatile
application but but this is just to give you the idea of how to use those oops
concepts to build a simple or

01:30:23
a complex java application so let's start from the options menu class
because that will give us the understanding of what kind of functionality i'm using
and here you will use that i am using different concepts which i which i have
taught you so far in this particular series so for example we are using the i o
exception we are using the hashmap class we are also using a scanner class which is
used to take input from the command line so this option menu class is extending the
account class and i will come to the

01:30:53
account class in a while but let's see what this option menu class is
doing this particular class has uh it is using a decimal format money format class
which is to denote what kind of format of money you have then we have a hash map
which is storing the couple of records of account number to pin mapping so we
create a map and inside the get login method we add couple of entries to the map
here the first entry the first integer is pointing to the customer number and the
second integer is pointing to the pin of the account of
01:31:28
this particular customer number so if we call the get login method this
method will insert the data after that it will ask the customer to enter the
customer number which has to be one of these two if you provide any other value
then there should be a logic in the application to reject the input so whatever a
customer number the customer inputs we pass that inside this particular method
called set customer number again i will come back to that and once you have
provided a valid customer number then this method is

01:31:57
going to ask you to enter a pin number and remember i told you this is
the customer number to the pin number mapping so whatever customer number you
select from here you will be providing the corresponding pin number and again if
you provide an invalid pin number you will get an error so that will all happen
inside this try block and if you enter any invalid characters in the pin number or
the atm uh customer number then you will get an exception which i am writing inside
the catch block this is all what we have seen so far after

01:32:26
the successful enter of the customer number and the pin number i fetch
those values from these two methods again i will talk about what these methods are
doing and again i'm doing some basic validation checks but things get interesting
in this method so what we covered so far was the get login method let me close this
method here so we covered the get login method the second method which is defined
in this particular class is the account type so here that we will ask the once the
customer has provided the customer

01:32:52
number and the pin number we will ask the customer to provide the account
type this application supports two different account types which are checking
account or a savings account so whatever so the customer has to choose one of them
again we will be taking the input from the command line using the scanner class and
then i'm using a switch case if the customer entered 1 then i will execute some
code for the checking account which is defined here in the case 1 and if the
customer entered 2 then the case 2 will

01:33:21
be executed and if the if it enters three then i will exit the
application by just printing a sys out so here i'm using the switch case construct
to provide different options to the customer there's also a default case specified
here which says invalid choice so this is what is happening in the get account type
method the next method is if the customer has used the option one then we call the
get checking method so let's go to the get checking method which is the next method
which is defined in this particular class so if

01:33:49
the customer has selected a checking account then we provide the
following functionality to the customer which is the customer can view the balance
customer can withdraw the funds customer can deposit the funds to the checking
account or they can just exit the application and again i'm using a switch case
here based on the customer types one two three or four the corresponding case block
will be executed for example if the customer wants to view the balance then this
block will get executed and i call the get account type
01:34:18
method if the customer selects the to as the withdraw fund then get
checkings with withdraw input method gets called and then get account type gets
called similarly if the customer enters three then we deposit the fund and for that
also i have defined a method i will go to the definition of these methods as we go
along but right now we are just building the framework so the get checking deposit
input method will be called whenever the customer selects the option three and for
option four we just

01:34:45
exit the application so this is what is happening in the get checking
method now if from the account selection if the customer selected second option
which was savings account then we were calling get saving method so this is where
the get saving method is defined and again it is following the exact same format as
we saw in the get checking method where we present the following options like
viewing balance withdrawing the funds and depositing the funds in the savings
account and i have some methods defined

01:35:12
here for example get saving withdraw input get saving deposit input so we
define the switch case here based on the customer's choices here and that is what
is happening in the get saving method so let me close this method as well and
that's all what is defined inside the option menu class so we ask the customer to
enter the customer number the pin number from this get login method then based on
that we provide the customer the options to choose the account type whether they
can choose the savings

01:35:41
account or the checking account and based on that whatever account type
they use we call the get checking or get saving method and inside this method we
provide the customer the other options to either withdraw the money deposit the
money or view the balance that is what is happening inside this option menu class
now let's go to the account class and all the methods which we saw here will be
provided a definition in the account class you also see this option menu is
extending the accounts class so

01:36:08
an inheritance concept is being used here let's go to the account class
now so this is the account class where i have defined some private member variables
like customer number so now you will be familiar with the customer number pin
number which i just described and also with the checking balance and the savings
balance you can also divide this account class into two child classes where you
have a savings account class and a checking account class but for simplicity i've
kept it to one class i'm initializing a scanner input which

01:36:36
will be used at multiple places and then i have the setters and getters
for these four member variables so you see set customer number get customer number
if you remember this set customer number was called in the option menu class if i
go back to the get login once we ask the customer to enter the customer number we
call the set customer number which gets called here similarly we call the set pin
number which gets called here similarly once we have said that we then we were
fetching the values using the

01:37:04
get customer number and the get pin number and that has that is also
defined here in terms of the getters get pin number and get customer number
similarly we also have the getters for the checking balance and the saving balance
member variables we do not have the setters for them because they are not
technically setters but we have to calculate the balance it's not a simple setter
where you can just return the value so now let's look at some of the business
functions of the account class how do you deposit how do you view the

01:37:33
balance how do you withdraw the balance etc so you see these two methods
which are specifically when the customer is trying to withdraw the money from their
account and these methods are basically called internally by other methods so we
will come to that let's see that let's just understand that we have two methods for
calculating the withdrawal amount or executing the withdrawal transaction for the
checking account account and the savings account similarly i have two more methods
here which are calculating

01:38:02
the depositing for the checking account and calculating the depositing
for the savings account and then we get the the bigger methods which are using
these calculation methods so i define four calculation methods so far calc checking
withdraw calc saving withdraw calc checking deposit and calc saving deposit
basically we're drawing in deposit for both of the classes and then we have these
business functions called get checking withdraw input and get saving withdraw input
get checking deposit input and get saving deposit input so

01:38:35
these getter methods are something which were being called from the
option menu class if you remember if you go back to the option menu class and if
you chose the checking account you get these options and let's say you chose option
2 which was withdraw funds or option 3 which was deposit funds then the
corresponding methods which were being called were get checking withdraw input and
get checking deposit input and this is the exact same method get checking withdraw
input and get checking deposit

01:39:04
input so if you see what is happening inside this particular method is
couple of sysouts then we take the scanner input amount and we convert that amount
to the double after that we check if the amount which the customer wants to
withdraw is smaller than the checking balance or not and i'm running this condition
to check that if the amount is smaller than the checking balance then we call the
calc checking withdraw method which was defined right here remember i told you that
these four calc methods will be used and this is how

01:39:36
they are used so here i'm actually subtracting the value so whatever
amount the customer wants to withdraw subtract that from the main balance and then
return the new main balance that's what you do right if you have thousand rupees
and if you want to withdraw 500 you subtract 500 from thousand and then you send
back the remaining balance so that is what is happening when we call this method
and then we print the new checking account balance that is what is happening in the
get checking withdraw

01:40:01
input method similarly if you chose option 3 which was depositing funds
to the checking account then get checking deposit input gets called and it is
defined right here get checking deposit input and here instead of subtraction we
are doing the addition so here i'm again doing a condition check that the current
balance plus the new amount which has been supplied by the customer from the
command line the total sum of it should be greater than zero what if the customer
entered an amount minus 5000 i

01:40:31
should not allow that so that's the reason i'm putting this condition in
place and if the current balance plus the new amount which is supposed to be
deposited to the account the sum of it is greater than zero then call the calc
checking deposit method and this is where the calc checking deposit method is
present and where i'm just going present balance plus the new amount and returning
the new balance that's all which is being done here and you will see exactly
similar logic running for the savings account as well if you look

01:41:00
at the get saving withdraw input which is used to withdraw money from the
savings account so i take the input from the command line and i run a condition
check that the amount which the customer wants to withdraw should be less than the
current saving accounts balance if yes then call calc saving saving withdraw method
which is defined here whereby subtract the amount from the current balance and i
return the new balance and similarly you will see the exact same functionality in
the get saving deposit where we add the money

01:41:30
instead of subtracting the money and this is all what is defined in the
account class so we talked about the option menu class we talked about the account
class and now we have the atm class which is the entry point which basically
initializes the option menu class and calls the get login method remember get login
was the first method which asked you to provide the customer number and then get
login called the method to choose the account type and the account type methods
then called the method to choose the option to withdraw

01:41:59
deposit or exit so now we have built a fair understanding of what is
happening in this application so let's run this application so i just ran this
application and let me bring the console here it's first asking for a customer
number so i will go to the option menu class and i will pick up one of the customer
number which i have hard coded here so i will provide that customer number and hit
enter then it asks me for the pin number so remember you have to provide the
correct pin number corresponding to the customer number so

01:42:27
i'm taking this pin number just copying it and pasting it here then this
whole code got executed and then it called the get account type and the get account
type method provided you with these options which you see here on the screen so
based on what you select the case block will get executed and let's say i want to
go for a checking account so i press one and i hit enter once you select the
checking account the case one gets selected and it calls the get checking method
and the get checking method again provides you with another

01:42:57
menu to withdraw view view withdraw or deposit or exit so let's say i
want to deposit funds so i press 3 and hit enter it says the current balance is 0
and enter the amount you want to deposit let's say i want to deposit 500 and i hit
enter you get the message saying the new checking account balance is 500 and you
have successfully deposited the money and that is what i did for the checking
account now let's try to withdraw something from the ticking account so you again
get back the same option because inside the get checking

01:43:26
method we call the once we have successfully done the business operation
we again call the account type method to display the menu again to the user so
let's see again select the checking account class and this time i want to withdraw
the funds remember i've added 500 there so let's say i want to withdraw so i choose
the option 2 as the choice current balance is 500 and it's asking me the amount you
want to withdraw from the checking account so i enter let's say 100 and i hit enter
when i do that the new checking account

01:43:56
balance becomes 400 so the 100 got subtracted from the 500 and the new
balance is shown to your screen you can do exactly the same thing for for the
savings account as well and let's also run a negative condition so i again select
the checking account and i want to withdraw the funds so i choose the option to
current balance is 400 and i i would like to withdraw a thousand what happens if
you do that we get a message saying balance cannot be negative and this is coming
from the calc checking method

01:44:25
which which is defined here so what is happening that i wanted to
withdraw the funds from the checking account and here get checking withdraw
input.called here this condition was checked so the checking balance was 400 and
the amount i wanted to withdraw was 1000. so 400 minus 1000 is not greater than
equal to 0. so it jumped into the else block and it said balance cannot be negative
so this is how you can also build some error conditions in your code which are
mandatory and which should be built

01:44:55
otherwise your application will have lot of bugs we can also exit the
application so if you just type 3 and hit enter you will exit from the application
and it will come in it will again call the login method where it will again ask you
to enter the customer number so this is how you can run this application and this
is this was a simple use case for you to show how you can use the different object-
oriented concepts like classes objects methods setters getters switch case etc to
build the application

01:45:22
with the correct business logic let's get started with working through a
code java project using all the different oops concept we have learned so far so in
this example we are going to have a look at a calculator application and this
calculator application supports primarily four different operations like addition
subtraction multiplication and division so let's see how do we build that app so in
front of me right now is open the main class so i have a main app class here which
has a public static void main method

01:45:55
and the first thing we see is a read input class which is calling a read
method so if you go to the read input class it has a simple static method here
which is the read method and it is using the scanner class if you remember i talked
about the scanner class which is used to take inputs from the command line from the
user so we initialize the scanner class and we start taking the inputs and whatever
the user inputs in terms of the expression let's say the user entered 4 plus 5 or
whatever that

01:46:25
whole expression gets sent back into this input line as a string and that
comes back here in that input expression once we have captured the input expression
the next thing we do is divide the input expression in the numbers and the
operators basically an expression would be either something like 4 plus 5 into 3
divided by 2 let's say let's say the user entered this expression so this has
either numbers or operators so that's what we are doing here we are using the split
method of the string api

01:46:58
the string class basically and this split method is going to split the
numbers and operators into two different string arrays and here i am providing the
split expression that split at every time you encounter one of these symbols so
this when you execute this particular statement it is going to return a numbers
array which will be created based on this expression so you will get an array with
4 5 3 and 2 and similarly in the operator array you are going to split or pick out
the elements every time you

01:47:31
receive a number so this operator array is going to hold this plus sign
this multiply sign and this divide sign hope you get the understanding that we are
just splitting the whole expression into two different arrays where first array is
holding only the numbers and no mathematical expressions and second array is just
holding the mathematical operators and no numbers and we'll see why we need to do
that in the upcoming lines once i have created this string array i just want to
store or convert these

01:48:03
string arrays into a queue so i convert this array into a list by calling
the array dot as list so this expression is going to return a list and then i'm
going to convert this normal list into a linked list and then going to store this
linked list into the queue of numbers and i'm going to do exactly the same thing
for the operator array as well converting this into a generic list converting that
generic list into a linked list and then storing this linked list as a queue so i
have an operators

01:48:37
queue and i have a numbers queue both of them have been stored into a
queue now and then i'm using the poll method if you remember the poll method of the
queue is used when you want to fetch the first front element the head element of
the queue so whichever element is sitting at the head of the queue will be taken
out the moment you call call the poll method so i fetch the first number from the
numbers q so in this case 4 will be fetched here in this variable and after that i
run a loop on the numbers q

01:49:10
and i'm going to run this particular loop till this particular queue is
not empty if you see there's a negation here so i'm going to pick up numbers one by
one from this numbers queue till the number q becomes empty and inside the loop i
do the exact same poll operation for the operations queue so this operations queue
is holding all these mathematical operations like plus star and divide so i'm going
to pick the head of it so in this case plus will be picked up and will be stored in
this variable called opr and

01:49:44
then i am going to run a switch case on this opr basically as i told you
in this particular calculator application we are supporting four operations which
are addition subtraction multiplication and division so i define a case for each of
the mathematical expression so you see there are four different cases here and for
each case i'm initializing a class if it's a plus sign then i'm initializing the
add class if it's a minus sign then i'm initializing the subtractors for
multiplication i have a class called

01:50:16
multiply and for division i have a class called divide let's have a look
at what these classes are containing so if i go to the add class first of all it is
implementing an interface called operate and this operator interface has just a
single method called get results which is accepting a variable argument array if
you remember i told you that you can also use a array this is called a where arc
expression this means that this numbers will be an array of arbitrary length it can
be a one element array it can be a

01:50:47
two element array it can be a four element array or whatever you supply
but it is still going to be a fixed length array but the length is going to be
dependent upon how much argument you supply to this particular method so that is
what this interface operate is doing it just has one method declared which is
called get result which is accepting an array of numbers so if the add class is
implementing the operator interface then it has to override the get result method
and here it is just doing a summation of all the

01:51:16
numbers which have been supplied so if you supply four numbers it is just
going to add up those four numbers and return the sum and the exact same kind of
logic or similar logic will be seen in other operate classes so if i go to the
subtract class it runs the exact same operation but it subtracts the values if i go
to the multiply class it is also implementing the operate interface and in the get
result it is just multiplying all the values similarly in the divide class it is
also implementing the

01:51:45
operator interface and it is dividing all the numbers one by one so it's
doing pretty much the same thing in terms of the expression and the logic the only
difference is whether you are using an add operation or a subtract or multiply or a
divide so we have checked all these classes and the operator interface as well and
let's go back to the main app now now you also see that i've defined a reference of
operate here and each of the switch case block i'm assigning the object type to
this interface reference so i'm using

01:52:15
the concept of dynamic binding which we talked about when we talked about
interfaces that we can also do dynamic binding where we can assign an object to an
interface type and the object type will depend upon the expression so once we have
filled that based on the case while filtering out this expression and picking up
plus star and divide sequentially then we go to the next line outside the switch
case block which is to pick the next number from the queue remember we already did
one poll here so as per this example 4 was here in

01:52:50
this res and now when we do this number dot poll again 5 will be picked
up because that will be the next head of the queue so we pick up 5 here and then we
call operate dot get result now this get result will be called based on the
expression which was encountered first in the queue again looking at the example
the first expression encountered will be plus so the expression was plus so the
operate type object is of ad type and when you call operate dot get result the get
result method of add class is

01:53:24
going to get invoked and it is going to sum four and five this rest is
holding four right now based on what i described here numbers.poll and this num is
holding the the second operand basically basically 5 here so 4 plus 5 gets
evaluated here in this case and then it again goes back here and it checks if the
number q is now empty no it is still not empty because we still have 3 n2 left
there so again it does the operations operations.poll and it picks up multiply sign
here then it again runs the switch case since

01:53:59
it's a multiply sign it will go here and it will initialize the operate
interface reference as the object of multiply class then coming here it will do a
numbers dot poll and this time the numbers dot poll is going to pick up 3 so 3 will
be picked up here and then the current value of result which is already 9 4 plus 5
was 9 the current value of result will be updated here so rest will be 9 here and
num will be 3 here and when you call operate dot get result the multiply classes
get result will get

01:54:29
called and it will say 9 into 3 which will become 27 it will again go
back here to this particular line and it will check again if the numbers queue is
now empty no it is still not empty because we still have one element left then it
pulls uh the queue of the operations where the expression divide is present there
which is the only element left in this particular operations queue and since we
have the opr value as divide this case block gets called and the new divide class
gets initialized the divide class

01:55:01
basically gets initialized and we come here and we do numbers.poll now
the last number which is available in this queue is 2 which gets picked up here and
then we again call operate.getresult this time since the operate object refer
object operator reference type was of divide class type so when we call
operate.getresult the divide operation or the getresult method of the divide class
basically this method will get invoked and it will divide the current value of
result which was 4 plus 5 9 into 3 which was 27 so it

01:55:37
is going to divide 27 by 2 which was the last value of the numbers dot
pole so it is it is going to execute 27 by 2 and whatever value it gets it is going
to store the value back into the rest variable and then it is again going to go
back here and again going to check the condition if the numbers q is not empty but
now the numbers queue is empty because 2 was the last element so it is going to
come out of the loop and it is going to print the final result that is how this
whole program is working now let's try to see

01:56:10
this in action and will try to execute the exact same expression so if i
right click and go to run as and run this as java application and i supply an
expression saying 4 plus 5 star 3 divide 2 and if i now hit enter i get the value
as 13.5 which is exactly what we ran through because 4 plus 5 will be evaluated
first which will result into 9 then 9 into 3 will be 27 and 27 divided by 2 will be
13.5 exactly so this is how this whole program is running where we first take the
inputs and then we divide

01:56:50
the input into numbers and operation arrays or in fact queues and once we
have those queues built up then we pick up the first number and we keep running
this loop till the numbers q is not empty inside the while loop at each step we
pick up the first expression or the operator we check what is the operator whether
it is an addition subtraction multiplication or division operator and whatever kind
of operator we have we initialize the corresponding class of it and then we fetch
the next value in the numbers q

01:57:24
and then we call the get result based on the expression and whatever
result we get from the get result we store it back into the rest variable so we
keep updating the rest variable at every step that's the reason the result of 4
plus 5 gets stored in the rest and next time you are calling the get result for a
multiply operation here the rest value is 9 now and it says 9 into 3 again in the
next time the rest value will be updated to 27 because we are storing the result
back into the result variable

01:57:53
so this is all i want to cover in this particular session where we looked
at an example of a calculator application purely from the console standpoint
obviously there can be lot more stuff which we can do into this particular
application make it more error-free and cover a lot of corner cases like validating
the inputs etc also looking at or putting some conditions around the exception
handling handling the divide by zero scenario etc which is not here but this is
just to show you the basic idea of how you can build
01:58:23
simple to complex cli applications using the core java concepts so that's
all i want to cover in this particular session if you like this video a thumbs up
will be massively appreciated thank you and will meet again in the next session and
with that we have come to an end of this particular tutorial on object-oriented
programming in java if you have any queries regarding the topics covered in this
session then please feel free to ask them down in the comment section below our
team of experts will be happy

01:58:49
to resolve all your queries until next time thank you stay safe and keep
learning [Music] hi there if you like this video subscribe to the simply learn
youtube channel and click here to watch similar videos turn it up and get certified
click here

You might also like