so in a previous previous video we started the solution or I started the solution to the
challenge set challenge lets to continue that now and we need to do is create
sub classes I mentioned that I was going to create some sub classes we want one
for planet one for dwarf planet and one for moon so the sub classes is going to
be very simple at least in this example now in a real application modeling objects in
the universe is probably a lot more information held for the various types of
heavenly bodies and different types of bodies have different fields in
addition to the common ones
now we're not gonna do that we gonna keep it pretty simple so we will create three
subclasses because obviously be one for each heavenly body type let's start with Planet go
ahead and create the plant class....and we're going to do is extends.....
....
...constructor is almost correct but what we want to do is we wanna
change that parameter we don't want to accept body types as a parameter
on the constructor for planet but rather what we want to do is accept the name of the orbital
period and the reasons we know there's a planet and we want to pass the enum constant for planets
.....then the other thing we want to override is
override the add satellite and you will see why we gonna do that shortly let's go
ahead and do that we are gonna override override the add satellite and
you can see that function has been written by default with the right parameters
which is good what we wanted to do here is we now for a particular planet
the body types can be satellites can only be moon so for our planet it is
valid to do a test for moons so we are gonna add that code in why we didn't do that before in the generic
heavenly body class it makes sense to do that test here so we're gonna put.....
.....
......
...so you can see that we're actually
performing the add by calling the super which is the add satellite method from
the heavenly body class but again we are checking to see whether this is a moon
because in terms of satellites a moon is valid for a planet ok so that's
planet and the next one is too dwarf planet lets go ahead and add that...
and do the same extends.....
our constructor is gonna be similar to what we created for the planet....
.....
...which is ultimately going to call the heavenly body constructor because we know what
type we are dwarf planet and that's all we're going to do we aren't going to override
the add satellites and moon is going to be very similar so.....
....and extends heavenly body again
....and our constructor...
...so that is our three sub classes defined
so what we can do now is go back to the original main where we had our code in
and this is the original code that we had when we are testing and
obviously we got some errors now because we changed parameters but the one we want to look
at is this first one here on line 13 and what I'm gonna do now is comment out the rest of
this code
and what we wanna do is where we've got heavenly body temp = new HeavenlyBody and we are passing
the 2 parameters we want to change that we want to put heavenly body temp equals new
in this case you want to a planet so do planet to make it valid that is now correctly using the
sub class and the error goes away its because remembering the third parameter is in
the constructor of planet which ultimately calls the heavenly body with
that right
3rd parameter being the body type so lets fix this now so they are all valid come down
here we are going to un-comment this again and let's go ahead and actually replace and
I actually need to un comment this code as well so what we can do their is take a copy of planet
and paste it in here for all the planets that is a moon we don't want to do that yet
planet moons of Jupiter planet we got Saturn planet planet planet Pluto is technically a dwarf
dwarf planet I'll put that as a planet for now and what we want do now
is for the other ones for the moons let's actually add those make that a moon and fix the others well......so we should have all the errors fixed at the moment now
lets comment out the rest of the code we got a few other things we want to fix and notice we need to fix this one their is a second Pluto
in the original code and just incidental there's nothing stopping us
from leaving that as a heavenly body then adding the third parameter....
.....that's
valid as well just wanted to point that but I think in this case to be
consistent lets changed this and make it a planet so now we got them all being added and we've got
various printouts what we can do is change remember we add the .toString
so we can actually start using this in some of our outputs so let's go ahead
and do that so down here for example after we added Pluto what we can do is
in that for loop we can change that and do something like print.....and by doing that its
going to automatically invoke our toString method and of course we have overriden that if you
recall so if you run that.....and notice how the enum is printing I
mentioned that that would print out rather than printing out a number or
something like that you get the actual description when that's quite handy so
you can see that that's all working and we added the various planets moons and then
at least all the moons so everything seems to be working so in other words test
case one passes and because we made the second pluto a planet and correctly
overridden
equals and hashCode you can see there hasn't been added to our planets again so its not appearing twice
so test case 3 has also pass again going back to those tests
case 1 was the planets and moons we added in the previous video or
before we started the challenge should appear in the solar system collection
and in a sets of moon so we got that working we also establish that
attempting to add a duplicate to a set must result in no change to the set so the
original value is replaced by a new one so we obviously got that working as well
which is cool because if you recall from the original running of this
program
we had Pluto originally defined with 248 for the orbital period and down here we
actually added it as a 824 and by running it Pluto 248 so we can confirm
that the second duplicate of Pluto hasn't overriden the 1st one
so to consequently test case 3 has passed as well so though an argument can be made now
for allowing objects of type heavenly body to be created and perhaps to cater
for new bodies that have not been discovered yet it's probably better to make
heavenly body abstract and one problem is that our second Pluto can have
planets added to its set of satellites in fact it could even have the Sun as a satellite which
can lead to a strange solar system so before we check that Pluto
can be added again to the set if it's a dwarf plant rather than a planet
we're going to make the heavenly body class abstract so lets go back and do
that and make public abstract class heavenly body and we need to go back to
main and we will probably have an error here now rebuild this now if I went back here and I change this to planet but if I put this back to
heavenly body we find we get an error know even if we have the right parameter if we change this third
parameter and made it.....
...we are going to get an error now and quite correctly IntelliJ is saying HeavenlyBody is
is abstract and cannot be instantiated so
what we need to do then in that case is changed this to make this a dwarf planet
...and then delete the third parameter so that is now valid
now Pluto we should run this we should find that Pluto will appear twice
because we've changed the body type to be dwarf planet and obviously
in the original definition by default we've added it as a planet so that should
be a different body type so if we run that to confirm that works we now correctly got
Pluto appearing as a planet with a different orbital period and the second
example which is clearly the new one that we've created from line
98 which is now off type dwarf planets so it added those which means that
our equals method is actually working just fine and it means part passing test case 5
now test case 2 going back to test case 2 which is a.equals(b)
must equal the same result as b.equals(a) we need to actually test that
and make sure equals is symmetric
so go about doing that what we want to do is actually pretty easy to check really
we need to create two objects to compare them both ways around so lets add the code....
i'm gonna delete this bit of object code here so what we need to do here is put....
.....
so lets run this to confirm and they are both true as you can see down there which means obviously
their symmetric because both earth 1 equals earth 2 and earth 2 is equal to earth 1 and just to be
complete to make sure that it is doing the right thing we can also do a
comparison to test to see if..... obviously that should be a false....
......should also be false
ok so run that and you can see it's false as well so the code is
actually working that's good so that's test 2 to make sure that that is now
working as well so I'm going to end this video here because we have still got an issue
I will talk about it in next video the sets are behaving as they should but when we come to
look at the solar system map there's a problem that we need to solve so I'm gonna
talk about that and how to get around it and show you the code for that in the
next video