Java and Polymorphism

Posted: October 30, 2012 in Game Development, Java, Polymorphism, Uncategorized
Tags: , , , ,

I have been wanting to learn Java for a while. I recently decided to choose a project to stimulate my learning of the language. Give my learning some focus and attention. If you have been ready this blog, I am developing a 4X strategy game. Well, as I have been reading on Java to remind myself and to understand some of the basics of Object Oriented Programming (OOP) I have grokked the concept of polymorphism. Well, not grokked, but I understand it now. I see a purpose and a reason for it to exist. Here is how I understand polymorphism. If anyone has anything to add or clarify, go ahead and post something. I would love to understand or have clarified anything I have wrong.

Polymorphism of SHIP to FREIGHTER

Polymorphism of SHIP to FREIGHTER

Polymorphism is a way to reuse a class. If a new class had to be made for everything, then the language definition, let alone any complex solution to a computer problem would be huge. The purpose for polymorphism is to allow reuse and redefinition of a class. In my case here is how polymorphism helps. Any 4X Space game will have ships, lots of ships. If I made a class for every ship and every modified ship, then I would have a real bloated program, but with polymorphism, I can do some very cool things. Here are some basics. I can define a basic SHIP class that has the properies of hit points, attack strength, defense strength, owner, location, and speed. All ships in my game will have these properties. So instead of writing methods for each ship to move, attack and repair, I can write it once in the SHIP class. So ALL ships will move, attack, defend, and repair the same basic way. This helps to add consitency to my program. I can still have varieties of ships by extending the SHIP class. One way to do that is to create a Freighter and add cargo space. I will later write methods that will load cargo and remove cargo for the freighter class.
I am still prototyping the classes and ideas, but I am looking forward to developing this because it is giving me some concrete examples of how to use items in OOP that I previously didn’t realize how to use them.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s