• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to simplify classes by using interfaces and/or inheritance

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to figure out how I can most easily make it easier to make new types of units in my game. I have buildings and ships, and would like to know how I could make it easy to add new units. I have been recently told about interfaces, and have worked with inheritance a little bit.

What I would like to able to do is have it so that all of the variables and methods common to all ships could be stored in a superclass or interface, and same with the buildings. I would also like to be able to assign behaviours to the buildings and ships, maybe as interfaces, which could contain all of the methods and variables required for the functions of that ship or building.

For example, creating a new type of building that can shoot, build ships, and can regenerate nearby ships. So it would possible inherit all of the variables and methods common to all buildings, such as health, image, x, y, getX(), getY() etc. But it would then also gain the variables and methods essential for its functionality, such as shootRange, shoot(), regenRate, etc.

How could this best be achieved?
 
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could probably structure your code around a base class for the units providing the common attributes and functionality to all units and provide specialized functionality through interfaces that aren't common to all units.

Example:

We know everything in our game is a unit so we make a base as follows:



Now we can create a family of classes tying a relationship with the unit class as follows:





Now say we have created the game and for the next release we now decide to include ships with shooting functionality then we create an interface to provide this function as follows:



By declaring this interface any object that needs shooting functionality can implement this interface to provide the shooting functions. And since we know for now that ships can shoot we create the following class:



By following this design we can expand our game based on an abstraction that is loosely coupled making it easy to extend as requirements change.

If we have an aircraft that can shoot we would create it like this:

First create the flyable interface



Now we create an abstraction for all aircrafts



Then create the specialized shooting aircraft



Now to put these units in action we create a ship and an aircraft in our Game class and shoot everything in sight


 
Alfie Noakes
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, that seems as though it could simplify it a fair bit. But the FighterJet would still need to have those methods and variables in its class pertaining to shooting and flying, wouldn't it? But it wouldn't need to include the methods and variables that were in the Aircraft class?

Also, for methods that have been implemented, are you required to have @Override?

Thanks for your response.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alfie Noakes wrote:Ok, that seems as though it could simplify it a fair bit. But the FighterJet would still need to have those methods and variables in its class pertaining to shooting and flying, wouldn't it? But it wouldn't need to include the methods and variables that were in the Aircraft class?



This illustration was not meant to be taken literally as a solid design to copy. It was mainly trying to demonstrate how to go about supplying abstractions and the way in which you can model a system to promote extensibility. To answer your question, when you go about designing a system you must first do a thorough analysis to determine the requirements which will supply a solid foundation which you can build on. It is after this phase you can determine how you would structure your system to know where variables and methods will go. Although the first design may not be complete, you must initially design it in a way to promote extensibility making the code easy to modify, this was my purpose of illustration.

Alfie Noakes wrote:Also, for methods that have been implemented, are you required to have @Override?



This annotation (@Override) is not required but is recommended to catch errors at compile time with regards to method overriding.
 
Alfie Noakes
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an idea of what I want, but I need to figure out how I can make it easy to add to.

I would like it so that in a new class of structure, say a Teleporter, all I would have to do is implement an interface or extend a class. Then all I would have to do is add extra methods and variables required for the teleporting feature.

I'm just wondering how that could be achieved, if it can be.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alfie Noakes wrote:I would like it so that in a new class of structure, say a Teleporter, all I would have to do is implement an interface or extend a class. Then all I would have to do is add extra methods and variables required for the teleporting feature.



In that case the pattern I've provided should suffice for your requirements, but since you sound a bit shaky in terms of object oriented design you should read a book on the subject matter to gain a firm understanding instead of trying to acquire ready-made solutions. You can try this one >> Object-Oriented Analysis and Design for Information Systems
 
Alfie Noakes
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I should be able to access a book such as that. Thanks for your your help.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alfie Noakes wrote:I should be able to access a book such as that. Thanks for your your help.


The main thing whenever you're doing something like this is to think before you code.

With all these class hierarchies and interfaces, you potentially have a very complex structure, so you need to write it down (possibly in diagram form) so that you can keep track of it in your head. UML seems to be the most prevalent standard these days, and it has a number of diagrams that you might find useful. In you case, I suspect a Class diagram may be what you want.
However, if they look too complicated right now, just draw something that has a meaning to you.

Another tip: Do one thing at at time. Write a Building class and test it thoroughly; then write a Ship class, ditto; then (and ONLY then) maybe try a Shootable interface and a MissileCruiser.

If you try to do everything at once you'll overload your brain.

HIH

Winston
 
They gave me pumpkin ice cream. It was not pumpkin pie ice cream. Wiping my tongue on this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic