Class Introduction

Complete Step By Step Java for Testers Classes And Methods -> Object Oriented Concepts
11 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€92.30
List Price:  €129.22
You save:  €36.92
£79.14
List Price:  £110.81
You save:  £31.66
CA$135.92
List Price:  CA$190.30
You save:  CA$54.37
A$153.17
List Price:  A$214.44
You save:  A$61.27
S$134.71
List Price:  S$188.59
You save:  S$53.88
HK$782.26
List Price:  HK$1,095.19
You save:  HK$312.93
CHF 90.56
List Price:  CHF 126.79
You save:  CHF 36.22
NOK kr1,076.34
List Price:  NOK kr1,506.92
You save:  NOK kr430.58
DKK kr688.47
List Price:  DKK kr963.88
You save:  DKK kr275.41
NZ$166.49
List Price:  NZ$233.09
You save:  NZ$66.60
د.إ367.19
List Price:  د.إ514.08
You save:  د.إ146.89
৳11,000.23
List Price:  ৳15,400.77
You save:  ৳4,400.53
₹8,335.05
List Price:  ₹11,669.40
You save:  ₹3,334.35
RM473.25
List Price:  RM662.57
You save:  RM189.32
₦139,656.03
List Price:  ₦195,524.03
You save:  ₦55,868
₨27,872.43
List Price:  ₨39,022.53
You save:  ₨11,150.09
฿3,637.73
List Price:  ฿5,092.97
You save:  ฿1,455.24
₺3,222.80
List Price:  ₺4,512.05
You save:  ₺1,289.25
B$497.67
List Price:  B$696.75
You save:  B$199.08
R1,888.37
List Price:  R2,643.79
You save:  R755.42
Лв180.67
List Price:  Лв252.95
You save:  Лв72.27
₩134,942
List Price:  ₩188,924.20
You save:  ₩53,982.20
₪366.06
List Price:  ₪512.51
You save:  ₪146.44
₱5,625.18
List Price:  ₱7,875.48
You save:  ₱2,250.29
¥15,118.78
List Price:  ¥21,166.90
You save:  ¥6,048.12
MX$1,663.06
List Price:  MX$2,328.35
You save:  MX$665.29
QR365.44
List Price:  QR511.63
You save:  QR146.19
P1,373.02
List Price:  P1,922.28
You save:  P549.26
KSh13,148.68
List Price:  KSh18,408.68
You save:  KSh5,260
E£4,723.53
List Price:  E£6,613.14
You save:  E£1,889.60
ብር5,694.53
List Price:  ብር7,972.57
You save:  ብር2,278.04
Kz83,612.73
List Price:  Kz117,061.17
You save:  Kz33,448.44
CLP$98,024.19
List Price:  CLP$137,237.79
You save:  CLP$39,213.60
CN¥722.49
List Price:  CN¥1,011.52
You save:  CN¥289.02
RD$5,928.03
List Price:  RD$8,299.48
You save:  RD$2,371.45
DA13,469.95
List Price:  DA18,858.47
You save:  DA5,388.52
FJ$225.25
List Price:  FJ$315.36
You save:  FJ$90.11
Q781.84
List Price:  Q1,094.62
You save:  Q312.77
GY$20,987.69
List Price:  GY$29,383.60
You save:  GY$8,395.91
ISK kr13,817.61
List Price:  ISK kr19,345.21
You save:  ISK kr5,527.60
DH1,010.57
List Price:  DH1,414.84
You save:  DH404.26
L1,763.81
List Price:  L2,469.40
You save:  L705.59
ден5,677.01
List Price:  ден7,948.05
You save:  ден2,271.03
MOP$807.66
List Price:  MOP$1,130.76
You save:  MOP$323.09
N$1,895.98
List Price:  N$2,654.45
You save:  N$758.46
C$3,689.21
List Price:  C$5,165.04
You save:  C$1,475.83
रु13,357.88
List Price:  रु18,701.57
You save:  रु5,343.68
S/371.02
List Price:  S/519.45
You save:  S/148.42
K383.60
List Price:  K537.05
You save:  K153.45
SAR375.02
List Price:  SAR525.04
You save:  SAR150.02
ZK2,548.47
List Price:  ZK3,567.96
You save:  ZK1,019.49
L458.83
List Price:  L642.38
You save:  L183.55
Kč2,334.38
List Price:  Kč3,268.22
You save:  Kč933.84
Ft36,425.52
List Price:  Ft50,997.18
You save:  Ft14,571.66
SEK kr1,059.50
List Price:  SEK kr1,483.35
You save:  SEK kr423.84
ARS$85,666.43
List Price:  ARS$119,936.42
You save:  ARS$34,269.99
Bs695.09
List Price:  Bs973.16
You save:  Bs278.06
COP$389,673.99
List Price:  COP$545,559.18
You save:  COP$155,885.18
₡50,509.65
List Price:  ₡70,715.54
You save:  ₡20,205.88
L2,474.33
List Price:  L3,464.17
You save:  L989.83
₲736,638.54
List Price:  ₲1,031,323.43
You save:  ₲294,684.88
$U3,781
List Price:  $U5,293.55
You save:  $U1,512.55
zł398.17
List Price:  zł557.46
You save:  zł159.28
Already have an account? Log In

Transcript

Hello all welcome back to the next one. In this class we will talk about classes. In Java programming language classes are the core component of most of the object oriented programming languages. And to understand this, the first thing that we need to understand here is, what are objects, right? This is the most important thing. So to understand the objects in programming language, let's first understand it in terms of the real world.

What we see everyday in our real life like usual objects, maybe you're sitting in front of computer right now, or maybe an iPad or iPhone, these are all objects. A car is an object which most likely most of you use everyday, houses an object. A book is an object. These are the examples of real world objects. objects have two major characteristics to understand even in real world and we will transform them in the Programming world also. So that same characteristics we can kind of talk about in the programming world also, what are the characteristics, state, and behavior, these are the two main things.

So you know what, let me just create a class here, I'm just going to create a class inside this OOP concepts. And I'm gonna call it the name is car. And I'm not gonna define any main method right now, because we don't need it and I'll explain everything. So this is my car class. And and we are creating classes from the beginning, right, but we will talk about it we will talk about what this is and what objects are. So it all make sense.

Just keep following it. So let's take an example of the car class. What kind of state it can have. Its it can have color, right? Let me just let me just put it into comments. It can have a color it can have a make It can have a model these are just few examples here of of the product like the year of make those kinds of things, what kind of engine it has, it can have those things, how many doors it has, are those kind of thing so like whatever you can think about it how many realtors have or anything else though these are the state of the car or you can say them as attributes of the car.

So state of the object or attributes of the object. So these are the state or attributes of the objects so for car as an example, these can be the things if you talk about your iPhone, what can be the attributes or state, maybe the color of the phone or what kind of screen it has, what is the size of the screen? What kind of processor it has, these are the state of the phone like not just iPhone, like any phone right? The second thing is the behavior of the object. So what can be the behavior of the car? Whether the engine is running, whether we whether the engine is stopped like stopping, then we want to stop the engine stopping behavior.

That's the behavior. how fast it can go? How fast is it running right now? Like increase the speed of the car? That's the behavior we are increasing the speed of the car, or maybe decrease the speed. Put it to stop.

Is it making more noise? less noise? Like those kind of things are the behavior of the car? So this is for the car object, right? Talk About Phone how fast your phone runs. How is the color showing visit like more red, more blue?

I mean, I don't know just like those are the behavior. When you do something, how it responds these kind of thing. And we talk about in natural language also, right like my phone behaves a little slow my phone behaves a little fast and these kind of things are the behavior of the phone object. So, when we transform everything in programming language, we can talk about the same things right. So, what we did is we talked about the color make model here can be the state of the car behavior can be started the engine, increase the speed, decrease the speed stop the engine, these can be the behavior of the class right. So, programming objects are stored as the they store the states in fields and fields are nothing but variables, we are seeing variables from the beginning.

And we also call them as member variables or class variables when we are actually working with classes. So so like we can create private string color, this is one member variable or class variable. And then I can create one line Private string MC. So this is my second member variable. So, what I'm doing is I am storing the state of the object, which is my car object in these variables. So, these are member variables class variables and these are popularly known as fields also.

And let's talk about the model and model can be an again a string. So, let's talk about model. So model of the car and ear can be an integer. So private end here. These are my few objects, member variables, right. behavior is represented by methods and we have previously also talked about that methods are the way to do something.

So if we are doing something we are doing it with the methods Like if I want to let them just take in an example, if I want to increase the speed, I can say public wide increase speed, right. And then I can write some code inside it and which kind of deals with the software of the car and it increases the speed of the car, right? So this is the behavior of the car, where it's going to increase the speed right? So I'm just gonna I'm just gonna write system dot out println statement to show that the method is doing something in creasing the speed or something right. So, this is the behavior of my car and in programming, we will represent the behavior by methods. We have been actually using the classes from the very first lecture.

Java code cannot be written without classes. So now to understand classes, what do we understand? Class is actually a template, or you can call it a blueprint, which we use to create objects. Right like this is the car object. This is the car class but we can use it to create the car objects in some other class will, we'll see it later. But this car class is a blueprint which it provides us where we can define the state we can define the behavior and then we can use these things to create objects, right.

Previously, we have seen primitive data type string data types like this one these ones we have defined here. So these are all data types, which are provided by Java to us. Now, when we look at car, it is not exactly but it can be used as a data type which we are creating, because we will be creating the objects and we will be assigning the values to the state of the object and it's all handled by us. Right. So that's one thing. Another thing I want to show right now here is that when we create a class, I have already created one, but I'm going to show the naming convention, it's important, it's really important.

So we can, we can right click on the package, and we can go to new. And we all know that and we click on class that creates a class for us. The naming convention usually usually is the first letter of the word should be capital. So if I'm making car it should be capital C ar, ar car, if I say if I like add two words, in the name of the class like car class, so the C for car is capital and C for class is capital, right? This is this naming convention. So naming ns capital convention sees capital so it's simple.

When you are using multiple words in the name of the class. Use the first Word capital, and others should be small. And if just a single word then obviously the first one capital. So there should be a naming convention of the class. This is really important to keep your code clean. And also, this public is by default created.

So a class can be other types also. But that's what we usually work on public classes. It means it's unrestricted. Anyone can use this class everybody can see this. And public private protected, we have a separate lecture about these things. We will talk about access modifiers in in a lot of detail.

So don't worry about this public right now. And we will talk about it later. But when we create a class, so if the class is public, the name of the class should match the file name. So now our name is car dot java. And file name is sorry, now the name of the class is car in the file name is car dot java. If I call it a car class, and I save it, it's going to give me an Or it's gonna say, either to do one of these things, either rename the compiler ation unit to car class dot java, which means change the file name, or rename, type to car or rename this guy.

So this is the type. That's what I was talking about, like this is kind of a data type, right? rename this type to car. So now Java is happy. And it doesn't give me any errors right now, right. So that's about it for this one.

In next lecture, we will actually talk about accessing these values because we have defined all of them private, which means they cannot be accessed from anything outside of this car class, we can we can access these variables within this class, but not outside of this class. So we will talk about how we can access these private variables from outside of the class. And why do we define them as private here? Kind of we will talk about the getters and setters in Java. So stay tuned for the next lecture. lll, see you there.

Thanks for attending this one.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.