Time
4 hours 19 minutes
Difficulty
Beginner
CEU/CPE
4

Video Transcription

00:00
hi and welcome back. So previously in the lab we were finishing up our constructor, which we made over here, which has the three properties of a student, a name and an age and a school name.
00:14
And now we will be making something called A to string method. Now, let me go ahead and first explain what a to string method is.
00:22
So essentially,
00:24
we're gonna be making a new student in a different class. And that is called an object in Java, as I have mentioned previously in my past video electors,
00:35
Um, and to represent that object and properly printed out to the consul like we are doing here with hello world,
00:42
we need something called a to string method
00:45
so that the
00:47
program or the compiler, knows how it should represent a student and printed out to the council.
00:56
So in order to do that,
00:57
we will
00:58
go ahead and type public
01:00
to strength.
01:03
Okay, Perfect. So here you congee leet the override.
01:07
And you can also get rid of this
01:10
and just keep the return. So
01:14
I mean, ah,
01:15
delete this part and keep the return. So this is our to string method. And in here, Let's say I want to say
01:23
Ah, student X with name X and School X. Let's say I want to put that into a sentence. How would I be doing that?
01:33
So I'm gonna go ahead and type out the representation I want of the student. Once we print a student object, for instance.
01:42
So that's a hello
01:45
and then
01:46
let's say hello to the student by referring to their name.
01:51
So I'm gonna add name into here
01:56
and then, um
01:59
then we can say Hello, student on Dhe. Then we
02:04
can print out their age
02:07
with age parameter here,
02:12
and then we can also have or include their school name,
02:17
school name
02:21
and then schooling.
02:23
Perfect. Now what this will do is once we end up creating our new class with a new student, it will print out the student here
02:31
with with It won't put it out here, but it will print out the student with this exact sentence. So let's say, for example, my name is Solomon and my age. Let's say it's some random number, but speak 37 or 38 all the wine of 19 and then
02:49
let's bake for school name let's speak Harvard University or something like that. Something totally random.
02:54
It would say Hello, Salman. And then age with the colon. 37 or whatever Or 19. And in school name? Harward. So this is how it would represent,
03:06
um, that object.
03:08
Now, in order to accomplish that, we're gonna go ahead and create a new class,
03:14
and we're gonna need that class. Sorry. One second.
03:17
So we're gonna name that class
03:21
Constructor tester,
03:24
Castor Or actually, let's name it yet. It's named constructor Tester.
03:30
Constructor tester.
03:34
Perfect.
03:37
Okay. And then we're gonna make our main method.
03:40
I mean,
03:43
and
03:44
strength,
03:46
Air Gs.
03:50
All right,
03:54
so here, I'm gonna make
03:58
a student. And for that I will have to first use the name of the class, the previous class, which we called constructor example to use these properties
04:06
and then I'm gonna name my constructor. Example. New
04:13
new student
04:15
equals new
04:16
constructor. Example.
04:19
Perfect. So this is how you create a
04:24
new student or even or you take use of the constructor example class. And as you can see here,
04:30
there is an error here. And the reason that there is an air here is because it's expecting a name and age and a school name which we defined in our constructor over here.
04:43
So I'm gonna go ahead and give it a name.
04:46
Let's say someone
04:47
and, um let's give it an age,
04:50
and you can all see here again if you go over the air, it specifies that you need to give it three parameters a name, an agent, a school name,
05:00
so many pit in my age
05:02
and then score school name. That's put in,
05:05
um,
05:12
Harvard or something like that.
05:16
Although I do not go to Harvard, so just keep that in mind. But yeah,
05:20
so for example, sake, we've put in my name my age and a random school name. Now, let's say we want to print that information. We will have to go over here in the main method
05:30
and
05:32
essentially just,
05:35
uh,
05:36
one second.
05:42
So for some reason, this is giving me an ER.
05:46
Let's figure this out. So
05:47
I would like to
05:51
prince
05:54
access maybe private. Interesting.
05:59
So how about we look at our other example for our class?
06:05
So for some reason, it is not giving me the right.
06:10
Uh, I would put, but we're gonna click on our class example and see what we did over there.
06:16
So in her class example, looking perfect. So
06:20
the problem that is happening here, if we go back to our constructor example, is that
06:27
you need to include your new student in your main method. Otherwise, it will think that it does not exist. So I'm gonna copy that.
06:34
And then I will be printing that over here.
06:39
Sorry.
06:40
Yeah, Perfect.
06:42
So know what this is telling? The main method is that we or this class, is that we created a new student,
06:48
and that new student has these three properties, and we would like to print that new student.
06:57
So this is where it will make use off our
07:00
public to string method, which is our string representation of our object.
07:05
So I'm gonna go ahead and run this class
07:12
and let's see what happens
07:16
usually does take a few seconds to just compile everything, but it should be done within
07:23
a few more seconds.
07:24
Perfect. So what it is saying here is Hello? My name age, my school name. But as you can see, it's all like, clamped together. And we do not want that to happen. And in order to prevent that. I'm just gonna add in a pair of double quotes
07:41
so that it is properly spaced out.
07:48
And then one over here.
07:54
Perfect.
07:58
Okay, let's run our test her again
08:05
on Dhe. Do we have one before age?
08:09
Okay.
08:24
All right.
08:26
So I'm just trying to make
08:28
the up and look more neat. Perfect. So
08:31
here we have a nicely represented object which says my name, my age and my schooling.
08:39
All right, so this was our project. This is how we are going to be using constructors. And this is an example, a very basic example of how you can use a constructor and going back to our lab to summarize everything. We discussed how to implement constructors, and hopefully you have understood how to do so. And in the next video,
08:56
or in the next module will be going over
08:58
control, throw and structures. Thank you very much for watching. And I hope to see you in the next video lecturer. And, as always, if you have any questions at all, feel free to contact me via Lincoln.

Up Next

Introduction to Java

In this course, you will learn Java starting with the fundamental and key concepts that have made it become so powerful and widely used around the world for personal and industry projects. Some concepts covered in this course are the following: Inheritance, Encapsulation, Polymorphism, and Abstraction.

Instructed By

Instructor Profile Image
Salman Haider
Cyber Security Assessnent Analyst at Bank of Canada
Instructor