Our last blog post talked about creating Java objects, their properties, their methods and utilizing finally utilizing them. Although the zipcoders have long moved on from that point, we will continue to take this blog in linear motion for at least one more blog post. The next step in Java for you readers, is expanding on the objects you learned about and created last week. The following concepts we will discuss are fairly simple and straightforward.
[code]
[/code]
For the scientists we have:
- ScientificProject favoriteProject
- useScientificMethod(Project project)
- solveReallyComplexMathProblem(
) - receiveAward(Award award)
- DangerousMission favoriteMission
- performWeaponTraining()
- arrestPerp(Perp perp)
- receiveAward(Award award)
- Artwork favoriteArtwork
- createElaborateArtwork()
- addStyle(Object object)
- receiveAward(Award award)
We will now introduce another concept in OOP called
inheritance. You inherit traits from your parents. In other words, you share characteristics with them. All humans inherit from the mammal group as do other mammals, obviously. Cars and boats share similarities – they are both vehicles that transport objects.
Everything can be traced back to a top level entity or abstraction – inheritance.
In OOP, inheritance is ubiquitous. Why? Well let’s look at our Person class and our persons again. We need a way to separate the different persons into their own class object but still remain Person objects. We can create standalone classes similar to the way we created a Person class but then we would have to repeat methods and fields in each class. That’s a waste of memory and time. Besides, if we do that, there would be no way to show the relationship between the different persons.
In reality, we may be so different at a glance but we are all still persons and we can see that by tracing our steps back in history, religion, science, art, e.t.c. Now before this post turns into something straight out of Buddha’s journal, here are the three separate classes all inheriting from the Person class:
[code]
[/code]
[code]
[/code]
[code]
[/code]
subclasses. With extension done, you have access to anything public in the Person class such as the describeSelf() method as well as the receiveAward(Award award) I just placed in below. I did this because all Persons (for now at least) can receive awards. Here is the updated Person class:
[code]
[/code]
[code]
[/code]
polymorphism in OOP. By overriding it’s
superclass’s method, it can provide a much more specific implementation which Java will use rather than that of its superclass.
[content_band inner_container=”true” padding_top=”10px” padding_bottom=”10px” border=”none” bg_pattern=”http://www.zipcodewilmington.com/wp-content/uploads/Triangles__1441830476_50.73.209.90-compressor.jpg”]
Want to learn more Java? Check out more about our curriculum and apply today!
[/content_band]