Time
4 hours 19 minutes
Difficulty
Beginner
CEU/CPE
4

Video Transcription

00:00
Hi and welcome back to the course. I hope you guys are doing well. And in this video lecture or lab, we're gonna be creating a student profile program.
00:12
Now, to go over the learning objectives of this lab, we're gonna be creating this student profile program to use encapsulation and demonstrate how encapsulation works
00:26
and that we're gonna achieve by displaying that specific students University in four profile, which will consist of their name, age, school name, student number and many other things.
00:39
All right, so at this moment, I'd like you to go ahead and switch over to intelligence
00:44
now in and tell Ajay I have two classes. As you can see, one is called student and one is called Tester. No, the tester class here has ah bunch of errors in it. And the reason it has errors in it is because the testing for it has been implemented in the tester class. But I haven't completed the student class yet.
01:03
I'm gonna leave the code inside the tester class
01:07
so that instead of writing the test code from scratch, we can just go off from what we have. So having an error right now is completely normal that we can avoid for now.
01:19
So in our student clause,
01:21
we want to be able to
01:23
set a student's name, get a student's name and do the setting and getting for age, student number, school name and status. And their status is basically if they're full time or if they're a part time.
01:34
Now. Since we're trying to use encapsulation as we discussed in the previous lecture, in order to fully implement this idea of encapsulation, we first need to make our variables private.
01:49
So that's the first thing that we're gonna start by doing so for that, let's start by first creating a private
01:57
string may.
02:00
So I created a private string called Name, which is the first parameter here that we want to be able to create. And then I'm gonna move on to
02:12
age
02:13
on. This is also a story. This is a
02:16
H, okay. And then we're gonna create a student number. So a student number would also be represented by an integer, and I'm gonna need my variable student number.
02:30
And then then we're gonna have a school name and school name would be a string, so I'm gonna make it a private string school
02:39
name.
02:40
Okay, And then finally, we're gonna have a Boolean which will determine the students status. So as we know, billions can either be true or false, So I'm gonna call my bullion status.
02:54
Perfect. So now that we have these private variables, how do we actually modify these private variables outside of our class?
03:07
Now, that is what I mentioned a few seconds ago. In order to achieve that, we need to be able to create gather and center methods to get this information in another class, for example, in our tester class, and to set this information in our tester class. And that is the only way we can
03:25
access these private
03:27
fields or attributes.
03:30
Now, I will show you an example of a getter method. So let's say we want to make a getter method for our private string name. I would say public t to tell other classes that this is a public method, and then let's call it
03:46
get me
03:47
And then that,
03:49
uh, is a string. So it's gonna return a string. And in order to do that, we're just gonna type a return,
03:55
um, name.
03:59
Now, what that's gonna do is whenever we asked to get the name of a student once we set the name that it's going to return the name of that student and we need to do,
04:09
um, something similar for the rest of We need to essentially do the same getting offer method creation for the rest of these parameters. So I'm gonna go ahead and stop for a few seconds and complete that, and then I will be right back.
04:30
Okay? Perfect. So now, as you can see, we have or get her methods. Now I'll explain them step by step. So since we have a name, a tribute or variable, we need to obviously get the name of the students. So that's why I have get name, as I mentioned a few seconds ago.
04:49
And then I have get age
04:51
gets to numb, get school name and get status.
04:56
No,
04:57
just because we have all our get her methods.
05:00
It doesn't really do anything on its own because we need to be able to set values for these variables and to do that, meaning to have set her methods.
05:09
Now I will make each center method
05:12
under each corresponding getter method. So let's say So obviously, I have get name as my first method getter method here. And I want to be able to set the name of a student.
05:26
Says you can see I can auto complete this operation over here. If I click enter,
05:30
what it's gonna say is public white set name, string name, and then this dot name equals need.
05:39
Now, that is how I want to be able to set a student's name. I'm gonna slightly modify this so that it makes a bit more sense. I'm gonna call my parameter new name.
05:51
Um,
05:54
sorry. New name.
05:57
And then I removed that this
06:00
and then we're going to set name to new name.
06:05
Your name.
06:06
Okay, Perfect. So what that will do is it will set the name of a student and which we will do in our tester class.
06:15
Now, I'm gonna go ahead and do the same that I did with the getter methods and complete the center methods. And then we will be right back and discuss how we exactly did that.
06:30
All right, so, going back to our center methods, what I have done here is I've made a corresponding set method for each Get method. So as you can see, we have get name, set, name get age, set age Get student numb set student, get school name
06:47
set school name and then get status
06:50
said status.
06:53
Now the reason all these methods are yellow is because there are in use right now. And where would you think they're being used there being used in the tester class.
07:04
Now, once we have all of our getter and center methods completed, we will go into our tester class And actually I can create a test case to see the result of our student class.
07:17
So I've made a new student and I've called It s one. And then I've used my set of center methods first to set the name of the student, the age, the no stew number, the school name and then the status.
07:30
And then what I've done here is I have made a simple print statement with using the get her methods for the student s one, and I've printed their name, age, student number, school name as well as their status.
07:46
Now, that is a simple test case for our program and simple test code that you can write up to see how your student class works and to see how your getters insiders work. Now, if we were to run this class,
07:59
then we should be seeing the open that you were seeing a few seconds ago. And I will go ahead and expanded. It says name Zelman, age 19. Student number 45 62 1 79 School named harder Harvard University and then status. Full time equals truth or colon troop.
08:16
So that is essentially how encapsulation works. You make getters and centers to make these private after attributes publicly available outside of your student class. And then we made a quick test her to see how this works.
08:30
Now, going back to our video lecture in this lab, we covered the implementation of encapsulation. And I hope you guys understood how we accomplish that.
08:41
Now, coming up next, we're gonna be starting Marshall seven, which is all about abstraction. As always. If you have any questions, feel free to contact me via Ellington, and I would be more than happy to answer any of your questions. Thank you very much for watching. And I look forward to seeing you in the next module.

Up Next

Introduction to Java

In this course, you will be learning about the fundamental and key concepts of Java 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