4.5 Parent/Child

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or

Already have an account? Sign In »

Time
4 hours 19 minutes
Difficulty
Beginner
CEU/CPE
4
Video Transcription
00:01
hi and welcome back to the course. I hope you guys are doing well. And in this lab, we're going to be going over method overriding and the implementation of method overwriting.
00:11
So, as always, I've listed out the main objective of this love, which I just mentioned a few seconds ago. But the way we're gonna be doing that or accomplishing that goal is by creating a parent class, a child class as well as a test class to see how our method overriding is being done as well as
00:30
what results that gives us and how it runs.
00:33
Now, At this point, I'd like you to go ahead and head over to intelligence
00:38
on an intelligent I've made three classes
00:42
and I've named this project method overriding so you can go ahead, create a new folder called Method Overwriting or whatever you may want to name it, and then you can create these three classes. Now the first class is just a simple class called child. So go ahead and create that first.
01:00
And then when you have to do is create a parent class,
01:04
and this parent class is also empty. It's just a regular class that whose name is parent,
01:10
and then I have a tester class. Now, in this tester class, we have a main method which we're gonna be using to actually test are parent and child class.
01:23
Now, what are we trying to do in, um, this lap? So we're trying to see how method overriding works. Now, what I want to do is create a method that prints something like, I am the parent.
01:41
Now, let's go ahead and do that. So the return type of the method is void because we're not returning anything. And let's name that method print info.
01:52
All right? Perfect. So I've named it print info,
01:57
and now I want to have a print seeping inside saying I am the parent. I am the parent.
02:07
All right, Perfect.
02:08
Now let's say we want to use the same method
02:14
to print.
02:15
I am a child
02:16
now, since we already have a method in here
02:21
called print info, we're gonna make use of this method in our child class. And how exactly organs are we gonna do that?
02:29
So we're gonna make the child class, extend
02:32
the parent class.
02:37
You know what
02:38
extends is doing is that it is giving you all of the functionalities on do all the things set. The parent class A class has. So in this case, the only thing that the parent class has is the method which is called print info.
02:53
I'm gonna you know, I'm gonna make use of this method and
02:58
override it.
03:00
Bye.
03:02
Um,
03:05
printing I am a child instead of I'm a parent because this is the child class.
03:09
So I am a
03:13
child. All right.
03:15
Perfect.
03:16
Now what we need to do is we need to make use of our tester class.
03:22
And the way we can do that is by using the main method and creating a new parent and creating a new child and seeing how that actually words. So let's go ahead and get into that. So, first of all on, I'm gonna be
03:39
creating a new child, and I will do that by
03:44
typing in child.
03:45
And then let's seem my first child see one
03:49
equals new child,
03:53
and then I'm gonna be creating a parent.
03:55
So it's creating a parent and name it p one equals new parent.
04:02
No. In order to see the results of our program and what we're trying to do I'm gonna
04:10
do you see one dot print info
04:13
and then p one dot print info.
04:16
If all goes well,
04:18
then I should see
04:20
I am a child, and then I'm a parent.
04:25
So let's go ahead and run our program by going over the right corner here,
04:30
and this will take a few seconds to build and compile.
04:35
Great. So, as you can see, it has printed by my child. And I'm I'm the parent.
04:43
Um, by making use of the print info method that we have an apparent class and then extending that
04:50
in the child class. Now, let's quickly at it
04:56
our sentence to be more accurate.
05:00
Um, all right.
05:03
Perfect. So it's right again.
05:06
And now we should be seeing is I'm the child and I'm the parent.
05:12
Perfect. So going back to our
05:15
video lecture
05:16
now
05:18
and this ah lab, we discussed the implementation of method overriding. And although it was a very simple example, it demonstrates what method overriding is and the basic idea of it, which we will need to move forward in the rest of our videos. And hopefully you guys,
05:38
I can understand what method overriding is about.
05:43
So the video coming up next is gonna be discussing a method overloading. And we're gonna be seeing some of the differences between method overriding and overloading, which people do tend to confuse sometimes.
05:57
Now, as always, if you have any questions at all, feel free to contact me via Lincoln. 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 video lecture.
Up Next