Gregory Desrosiers Montreal

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Saturday, 1 June 2013

JAVA Program: The Nintendo GameCube Game Class [Inheritance]

Posted on 06:42 by Unknown
Good morning, everyone!

It's a warm overcast morning here in Montreal; we're at 24°C, winds 6 km/h to the south, 67% humidity, and pressure is at 101.3 kilopascals.

Anyway, I want to demonstrate to you guys what inheritance in JAVA Programming is all about.

Let's say you have a class that serves a general purpose. For example, a car. Each car would consist of many different parts; the name, the year of manufacturing, the chassis, engine block, wheels, transmission, fuel, and drivetrain. If you would want to design a class that is designed for Formula 1 racing cars, consider their special equipment: front and back wings, tire type and pressure, ride height, suspension, gear ratios, brake balance, steering, differential, and additional aerodynamics.

Instead of designing a class where it carries data fields for both car and Formula 1, why not expand the car class to a class designed for Formula 1? It's a relatively simple process, just as long as you follow the rules of inheritance.

Here's an example. This is the code for a mutable class designed to instantiate and carry information on Nintendo GameCube games. It is an extension of the ComputerGame class, which you can see right here: http://gregorypdesrosiersmontreal.blogspot.ca/2013/05/demonstration-of-java-classes.html

The advantages of inheritance is simply that you can go into much more specifics out of a generalized class that it can handle the type of objects in the real world you want to design the class for. The extended class will carry all the methods and data fields from the original class where it's being expanded by this class.


Take note that the equals method, originally defined in the Object class, is not overridden in this class. It's simply a boolean method where instead of returning whether or not two objects have the same reference address, it checks the contents of the objects and determines whether or not they are equal.

This involves using polymorphism, generic programming, and the instanceof operator, which checks to see if the actual type of the object, at the type of construction, is the specified class on the right side of the operator. It can even reference the class you are currently in.

The key to expanding an original class to more specifics is by using the key word extends.

public class NintendoGamecubeGame extends ComputerGame
{
    private String gameMemorySize;
    private boolean gameIsLicensed;
  
    public static void main(String[] args) // Testing purposes only
    {
        NintendoGamecubeGame game = new NintendoGamecubeGame("The Legend of Zelda: The Wind Waker", "Nintendo EAD",
                "Nintendo of America, Inc.", "Nintendo of America, Inc.", (short)2003, "1.4 GB", true);
      
        System.out.println(game + "\n\nModifying object...\n\n");
      
        game.setName("Super Mario Sunshine");
        game.setYearOfRelease((short)2002);
      
        System.out.println(game + "\n\n" + game.getGameDeveloper() + " " + game.getGamePlatform());
    }
  
    public NintendoGamecubeGame()
    {
        super("", "Nintendo Gamecube", "", "", "Nintendo of America, Inc.", (short)2001);
        gameMemorySize = "1.4 GB";
        gameIsLicensed = true;
    }
  
    public NintendoGamecubeGame(String gameName, String developer, String publisher,
            String distributor, short releaseYear, String memorySize, boolean licensedGame)
    {
        super(gameName, "Nintendo Gamecube", developer, publisher, distributor, releaseYear);
        gameMemorySize = memorySize;
        gameIsLicensed = licensedGame;
    }
  
    public void setGameMemorySize(String gameMemorySize)
    {
        this.gameMemorySize = gameMemorySize;
    }
  
    public void setGameIsLicensedFlag(boolean gameIsLicensed)
    {
        this.gameIsLicensed = gameIsLicensed;
    }
  
    public String getGameMemorySize()
    {
        return gameMemorySize;
    }
  
    public boolean getGameIsLicensedFlag()
    {
        return gameIsLicensed;
    }
  
    public String toString()
    {
        String messageToPrint = super.toString() + "\n\nMemory Size : " + gameMemorySize + "\nIs the game licensed? ";
      
        if (gameIsLicensed)
            messageToPrint += "Yes";
        else
            messageToPrint += "No";
      
        return messageToPrint;
    }
  
    public String superToString()
    {
        return Object.class.toString();
    }
}

Invoking "super" in the constructors will call the superclass' own constructors explicitly with the parameters passed. When a subclass object is constructed, the superclass constructors are called first before the rest of the construction operation can begin, so it is a must to use super as the first statement in a subclass constructor.

You will notice that the method toString is also defined in the ComputerGame class; this is technically called overriding where it is invoked differently than the method defined in the superclass. This means that by constructing an object from this class and calling the toString() method, it will print differently than the toString method defined in the ComputerGame class.

Sadly, we can't explain what overloading means because it does not exist in this code.


Anyway, moving on to something else, by designing a subclass that extends the generalized "superclass," you'll be able to hold more information on the specifics that subclass is designed for.

So what will be your two classes to design? Please let me know; anyway, good luck, and I'll see you later!
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • The Quebec Ministerial Exam of College English (English Exit Exam)
    Hello, everyone. How you doing? I'm good. There is some unfortunate news for me to deliver, though I am planning to try to work myself a...
  • Computer Game Collection
    Hello, people! Before I happen to leave this very foundation of Champlain College Saint-Lambert (that's where I'm learning to get wh...
  • String Letter Counter (JAVA Program)
    Hello, guys. This is another JAVA Program I want to share with you guys. It's a counter for a specific letter in an entered string strai...
  • DOS Games to Play
    Hello, people. If there's one thing that I want to do when I have lots of money, it's to play full version DOS Games on my Windows P...
  • JAVA Program: Command-based String Remover in Text Files
    Hello, everyone. Let me share with you the code on one of the programs I did back in March 2013, while doing my Data Structures class. This ...
  • The Black Glove [Short Poem by me]
    This is a poem written by me back in April 2010. I'm not sure if this was for an assignment, but this is what I got. It may not be good ...
  • Fundraising Project (To Autism Society Canada)
    Hi, everyone! How are you people doing? I am feeling tired and stuff, but there are a couple of things I want to bring up with you. Tomorrow...
  • Best VGM Series
    Hello, guys. I want to present to you a series of YouTube imports of video game music, with some of them already recorded but in a form that...
  • Princess Peach In India [Story Idea]
    Here's a really fancy story I have made, although it is more of an initial idea and comes from a visual I had more than two years ago. E...
  • Super Mario Script 1 (v 2.3) [Super Mario Galaxy font, with a few photos]
    Hello there. Do you recognize the font of this text? I'll give you two clues. The one below is the a sample sentence of different sizes....

Blog Archive

  • ▼  2013 (38)
    • ►  August (2)
    • ►  July (9)
    • ▼  June (12)
      • "The Asperger Computer" outline and Spreading the ...
      • JAVA Program - Essay Class (From Chapter 11 of Ton...
      • JAVA Program: Circle Cannon Shooter (Shooting Game...
      • Message from my College English teacher
      • How to Play a DOS Game using DOSBox
      • JAVA Program: Finding Smallest Number (Demonstrati...
      • JAVA Program: Entering Non-Duplicate Numbers to a ...
      • Need some help on publicity with my e-book
      • JAVA Program: The PlayStation Class [SonyPlaystati...
      • The Asperger Computer - Second Edition Preview: Th...
      • Am I Happy on My Brother's 21st Birthday?
      • JAVA Program: The Nintendo GameCube Game Class [In...
    • ►  May (12)
    • ►  April (2)
    • ►  January (1)
  • ►  2012 (24)
    • ►  December (13)
    • ►  November (3)
    • ►  October (5)
    • ►  September (1)
    • ►  August (2)
Powered by Blogger.

About Me

Unknown
View my complete profile