Please follow me here!

Saturday, June 1, 2013

JAVA Program: The Nintendo GameCube Game Class [Inheritance]

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!

Google+ Followers

Popular Posts by Gregory