Lecture 4- Introduction to Classes and Objects in Java and Paradigms of OOP

IIT Roorkee July 201
61 followers
Jan 3, 2025
[Music]
[Music]
so welcome to lecture four introduction
to object oriented programming so in the
last few lectures so we talked about the
classes and objects in C++ so we have
run several codes and
now what is the syntax in Java all right
another objectoriented programming
called Java so we are going to see the
syntax as I already Define when you
define a class the only difference is in
C++ you have a semicolon
right so if I'm writing the C++ same the
class I have to put a semicolon over
here all right so whereas you should not
put a semicolon in Java so that is the
first difference in the class rest of
them almost similar so you can see I
have imported several functions utility
Lang and input
output all right so here you have class
car car is the name of the class
all right so under this you have
two data
member all right so here you have a data
member one is brand Under
string another one is year under integer
all right so you have two data members
one is brand another one is year one is
string another one is
integer all right so here we call this
as a
method right so this is a method in C++
we call it is member function so in
Java it is nothing
but a method right so this display info
is a
method whereas in the last few lectures
in C++ we call this as a member function
both are
equivalent so I call this in
C++ all right so here you have a method
called display info more or less
similar all right so syntax is C out we
used to write C out in C++ and in Java
it is
system.out.println Ln stands for new
line right system.out.print Ln so there
you use C out the same syntax in C++ if
we write like this C+ C out brand and
then you put
endl all right exactly the same syntax
this is the similar syntax in
C++ similar syntax in C++ and in Java
you have system. out. print this is like
quote you are writing we can write in C
C++ also that's understood I did not
write it in the code you are writing
whatever you are writing in the code
will be printed and then you have plus
symbol and brand
all right so exactly
similar we have already seen several C++
syntax so you are now familiar so see
out brand andl equalent and similarly we
are Printing
system.out.print and
here right system. out. printer L year
and what is the year correct so we have
two member data and you have one member
function the class is over the class is
called car
right the car class has two member
data in fact I have to call data member
in Java and Method display info which is
equivalent to member function in
C++ so
now this is the driver class right which
is having main whenever you are having a
main in Java you call this as a driver
class
all right so you can find the
main all right so you can find the
main correct so this main is name of the
class capital M and here like a void
main or int main when you're writing in
C++ all right so here you have void main
rest of the thing we'll see in the due
course right now no need to worry
because our focus is on class and object
all right so here in Java because it is
d dynamic in nature so when you're are
creating an
object right when you are creating an
object so my car is an object and then
dynamically we have to allocate a memory
or automatically it will be allocated
when you write new and the next one is
slowly I'm introducing this
Constructor all right so here my car is
object all right and here you have
class the new operator we'll take one
example in the case of
array right and here this is first time
we are seeing this is called
Constructor anyway we will Define
Constructor extensively right suppose I
declare let us assume in C++ in a of
10 right array of 10 elements
static array so what about a dynamic
array if I ask you to create a dynamic
array what will you do in Star
a all right a equal to
new an of
10 all right so you might have used this
new operator while creating a dynamic
array this is a dynamic
array in C++
all right so this is a static
array so in the case of java everything
is dynamic suppose I want to write the
same code so this is a C++ code so in
Java what you will do you use like this
in Array notation instead of pointer
I'll put this in a and then you are
writing a equal to new in of 10 same
almost the same code except the
star right so this is in the case of
java
so in
Java everything we have to talk in terms
of dynamic right this is ruled out I
cannot declare like this in Java all
right so similarly when I have this the
object my car you have object and car is
the
class now I have given some example of
new in C++ so dynamically
create a memory the new will dynamically
create a memory so my car
y capital c here so object so this
object will be located in some
address right so here in Java we call
that is a identity Ash
map I'll write completely identity Ash
map you call it as address in
C++ right so here is a identity Ash map
so some
address let us say 0x 732 EF something
like
that so in this address your my car
object right will be
stored right and line number 27 and 28
we have the data members brand my car.
brand so I give some brand
name and my car. year
2024 so now my car is invoking display
info
my car object which is invoking so dot
operator same exactly same what we have
studied in C++ my car do operator
display info right so when it calls so
you can see that brand is Kia and year
is 2024 right so that will be prined
system. out. printer learn we want to
print these two values brand and year so
when when I run the code I will get this
as a output all right so car brand is
Kia and here is
2024 so we will see now I introduce I'll
use Escape I'll go to the eclipse
program if you open Eclipse right so
Java this is called Java eclipse this
environment is called Java Eclipse so
here you go the same program that I have
written so here you you have class car
right so here you have class car class
car has two member data or data member
brand and year right and then you have a
method called display info all right
line number 15 to 18 then when you come
to the main program we call it as main
one all right so we have car car is the
class and my car is the object
right so we are creating we are
instantiating an object called my car
under class car and a new operator and
you have a Constructor so line number 28
and 29 the my
car which is having two data member is
getting the value right my car. brand is
Kia and my car. ear is
2024 so now in line number 32 my car is
invoking
display
info right my car is invoking display
info when it is invoking display info it
goes line number 15 and there are
two print out statement right two print
statement system.out.print and car brand
so brand value what is the brand name
Kia and system. out. print and year is
2024 so when I run this code
all right so here you have you can go
here and then run this code you can see
the output in the bottom car brand is
here and here is 2024 so this is under
Java Eclipse so you can install Java
Eclipse all right and then it's a good
compiler for Java compiler so you can
run the code so this is in housee you
can install this in your machine or in
your computer and then you start running
this code right so more or less we had
seen the objects and classes extensively
we saw in C++ I have introduced Java so
you can write the basic programs in Java
as well all right so at one point of
time I'll introduce all the programs
right or all the concepts with the help
of both C++ and Java and at what point
of time right so we will follow only C++
so it is understood both are almost same
all right so once you get that feeling
so you can start writing the code only
in C++ but whenever there is a
difference or some of the
functionalities available only in C++
not in Java or vice versa so that point
of time I will talk about that right so
now the next concept is inheritance in
C++ so extensively I talked about the
code reusability in the last few class
class ES right so that means somebody
has written Class A all right and I am
the new user I have written Class B so
can I use some properties of Class A yes
you can do that that is called as a
inheritance that means B is taking the
properties of a right so one of the key
feature in op inheritance is one of the
key feature in oop so in this case a
will be called as a Base Class A will be
called as a base class and B will be
called as a derived
class so B is using all the properties
of
a right unless otherwise I talked about
this access modifier right when we I
mean slowly I introduce the private and
public when everything is public assume
that all the member data and member
functions are public B can
use if it is private only a can use
right so you cannot do the derivation
all right so those things we'll talk
once I introduce the access modifiers
right like public private protected Etc
right so right now we know what is meant
by inheritance so B can use all the
properties of a all right if it is
publicly available so one you call it as
a base class another one is the derived
class so that means B can reuse the
code that means that may contain a
member function that may contain a
member data so you are establishing a
relationship between the classes all
right so also you might have
studied function
overloading right in the basic
programming like a procedural oriented
programming function overloading when I
introduce the class class on objects so
we have another concept called function
overriding so this can be achieved with
the help of inheritance so these are all
the basic features of inheritance in C++
so what are all the different types of
inheritance in C++ so this I have told
right so Class B is using the property
of Class A Class A and Class B Class B
is using the property of Class A this is
a notation you have a arrow so that
means Class B is using let us say the
data member and member functions or
member data and member functions so that
inheritance is termed as single
inheritance or you can also call it as a
simple inheritance now you are extending
so so can I have three classes can Class
B inherit Class A and Class C inherit
Class B the answer is yes you can do you
call this as a multi-level inheritance
you can have a b CDE e f up to that
right so here B is inheriting Class A
and Class C is inheriting Class B you
call this as this concept as multi-level
inheritance and similarly now assume
that you have three classes Class A
Class B and Class C all right so Class C
can use the properties of Class A and
Class B right Class C they can use the
properties of Class A and Class B two
classes all right you call it as
multiple inheritance this is a concept
of multiple inheritance we have a
notation we'll see all right so this is
called the multiple inheritance and
similarly you have hierarchical
inheritance Class B is using the
property of Class A and Class C can also
use the properties of class all right or
other words Class B can be derived from
class A and Class C can also be derived
from Class A in the previous case
multiple inheritance Class C is derived
from both class A and Class B that is
the
meaning right so you can say other way
around it can use the properties
otherwise when I talk in terms of Base
Class and derived class the multiple
inance Class A and Class B are the based
classes and Class C is the derived class
all right so Class C can be derived from
class A and Class B and you call this as
a multiple inheritance right here Class
A is the base class and Class B and
Class C are the derived classes so that
is also possible Right Class B and Class
C are derived from class A you call this
as a hierarchical and the last one is
you have this we call it as Hybrid
inheritance right this is called Hybrid
inheritance all right so you can see
both hierarchical and multiple the
combination of hierarchical and multiple
right the multiple Plus archical you
call this as a Hybrid inheritance all
right so for example here Class B and
Class C they are derived from class A so
Class A is the base class all right so
these are all I put D1 and D2 dered
classes D1 is a derived class one and D2
is a derived class two all right and now
class D all right I'll put D12 so now
this will become derived class which are
all the base classes Class B and Class C
so when you do all together whatever
inheritance that we had seen in the
previous slide that are nothing but
multiple inheritance and hierarchical
inheritance so the combination is hybrid
all right so in C++ we have all these
features in the case of
inheritance so you may have single
inheritance like this you may have
multi-level inheritance like this you
have multiple inheritance you have
hierarchical the combination of multiple
plus hierarchical you will have right
multiple plus hierarchical you have have
Hybrid inheritance so these are all five
different inheritances which is
available in C++ whereas when I am
comparing with Java Java does not have
this multiple inheritance right C++ you
can see all are there C++ you have all
are there whereas in Java you don't have
multiple inheritance the moment you
don't have multiple inheritance we know
multiple plus hierarchical is hybrid
right so which obviously hybrid also
will not be available right naturally
all right so we have seen the last
diagram of in C++ in C++ all the
inheritance are available whereas in
Java we don't have multiple inheritance
in terms of class so right now we have
class only so in Java we have a concept
called interface right so once we study
in in fact we are going to study
interface so when I introduce interface
we will have the concept of multiple
inheritance all right so right now in
terms of class we don't have the concept
of multiple inheritance in Java so
naturally you don't have Hybrid
inheritance also in Java so C++ point of
view we have all these five inheritant
so now we'll see an example in both C++
and Java all right so you have in the
Base Class you have W right we call it
as a base class W is a base class so we
call it as a right because we discuss
about A and B right so this class has
one member data right we are in C++ now
one member data called brand and we have
one member function called so brand
right the Show brand is just displaying
see out brand brand all right so this is
under vehicle so this contains
brand that is a member data and it has
another one that is called member
function which is called Show
brand so you can write like this so one
is brand another one is Show brand one
is a data member another one is a fun
function member function right so what
is the derived class we'll see another
class so another class is car class car
so this is the syntax now how we are
going to derive class car we are going
to publicly derive still I am not
introduce this access modifier we will
see slightly later so right now we have
to worry about how it is being inherited
all right so vehicle now it is a base
class this is this is a way Class Car
colon right you put public private or
protected these are all the access
modifier right now without loss of
generality we put public we'll see the
meaning slightly later all right so this
is how the syntax go for inheritance so
car is the derived class vehicle is the
base class so I'll just go back to the
previous slide I'll write carer right so
that is B so since I put the syntax like
this so now I am eligible to draw an
arrow that means car can inherit the
properties of vehicle right so that
means it can use brand because it is in
public it can use brand it can use Show
brand suppose you are right under car
you have an object let to say car C1 so
this C1 can use right so or can access
brand and Show brand that is a meaning
right anyway we'll see that how we are
going to do that so right now the
diagram is clear all right a is the base
class and B is the derived Class A is a
vehicle car is the derived Class B is
the derived class that means car is the
derived class so that means car can
access both brand and Show brand right
so now this car class that itself is
having one member data which is called
Model right whereas here you have brand
here you are naming as model all right
so string model so model that belongs to
car right this member data belong to car
and you have one more member function
right that is under car we have used a
member function under vehicle one more
member function line number 19 it is
under car you have show model so show
model will display model right what is
the model right number all right so here
you have model is a string model is a
string line number 17 you have model so
string will be displayed so we will go
to the main program so main program you
have my car which is an object right my
car is an object so this is coming under
object
car right so my car is an object this is
coming under class car
all right so I will go back again The
Inheritance scenario car is the derived
class which is the base class
vehicle right so under this class car I
have an object called my car right so
now my car.
brand right so my car. brand where is
brand is available in Base Class yes
this can
access so now under this I have an
object called my car so this my car can
access both brand and Show brand right
so that is why you have line number 28
so this is a valid statement there won't
be any error my car. brand brand is
available in Base Class yes and next one
is my car. model model is already
available in derived class so it's not a
problem the usual class and object that
we had seen right so this is also clear
that's a usual one now my car. show
Brand Show brand is available in base
class all right so this can access your
my car the object my car under car can
access Show brand no problem because
it's concept of inheritance right so
Show brand and the next one is my car.
show model is already available in the
derived class right so now so I have my
car. brand is toyoto my car. model is
Cora all right so now what will happen
if I invoke I mean to say my car the
object my car is invoking Show brand
right what is happening Show brand you
will go to the Show brand all right so
that will display brand see out brand so
what is brand I passed so brand is
nothing but Toyota my car. brand as
Toyota so the Toyota will be printed as
a first input right under line number
31 if you look at at line number 32 my
car. show model right so show model
model what is model Corolla right so
when you go over here the show model
function because it is invoking if you
look at line number 32 it is invoking my
car object is invoking show model so
show model when you look over here model
will be displayed what is your model
Cora so Corolla will be displayed right
so both Toyota and Corolla will be
displayed as output and if you look at
if you run the code right so you have to
get these two as a output so we will see
the C++ code right for whatever we have
written we'll see the C++ code now go to
the CPP code code 7. CPP all right the
same code that I have written all right
so here you can have class vehicle which
is a base class line number five you
have class vehicle which is a base class
all right so you have string brand and
void Show brand right and similarly you
have class car which is a derived class
line number 15 you have derived class
right so under line number 15 again you
have right one string called model and
you have one member function all right
so Base Class is also having one data
member and member function so here also
you're are having one member data and
member function right so these things we
had already seen so now if you go to the
main program my car is an object and and
car is the class so my car. brand is
Toyota and my car. model is Corolla and
my car is invoking Show brand and my car
is invoking show model right which is
possible right so those things we have
seen in the theory and now if I run the
code compile and run right so you can
see the output so we are getting brand
Toyota and model Cora right so this is
how your CPP program is running for for
the single inheritance all right so this
is the output in fact we got the same
output you can see in the program also
so now in the next class the same single
inheritance program we will run in Java
okay so with this I am concluding this
lecture thank you very
[Music]
much o
[Music]
