Generic Object Type in Java
Generic Object Type in Java . In this Article i will explain what is a Generic Object in Java by stepping through the Sample code created by me.
What is a Java Generic Class
- Java Generic Class, restrict the Type of related class which can only be used within a Class.
- The Generics functionality provide compile-time type safety that allows programmers to catch invalid types during compile time.
The Sample Code
The Sample Code consist of 4 class:
- gameCharacter
- armory
- weaponry
- recruitSoldier
Class gameCharacter
package genericExample; public class gameCharacter <T,A>{ // Types of Input T weapon; A armor; // THis is a Constructor public gameCharacter(T weapon, A armor) { super(); this.weapon = weapon; this.armor = armor; } // Get Info ( armor) public A getArmor() { return armor; } // Set Info ( armor) public void setArmor(A armor) { this.armor = armor; } // Get Info ( armor) public T getWeapon() { return weapon; } // Set Info ( armor) public void setWeapon(T weapon) { this.weapon = weapon; } }
Class armory
package genericExample; public class armory { public String armorName; // This is a constructor public armory(String armorName) { super(); this.armorName = armorName; } // Get Info public String getArmorName() { return armorName; } // Set Info public void setArmorName(String armorName) { this.armorName = armorName; } }
Class weaponry
package genericExample; public class weaponry { public String weaponName; //This is a Constructor public weaponry(String weaponName) { super(); this.weaponName = weaponName; } //Get Info public String getWeaponName() { return weaponName; } // Set Info public void setWeaponName(String weaponName) { this.weaponName = weaponName; } }
Class recruitSoldier
package genericExample; public class recruitSoldier { public static void main(String[] args) { weaponry sacredSword = new weaponry("Sword Of Gray Skull"); armory noArmor = new armory("Naked"); gameCharacter<weaponry,armory> Heman = new gameCharacter<weaponry,armory>(sacredSword,noArmor); String HemanWeapon = Heman.weapon.getWeaponName(); String HemanArmor = Heman.armor.getArmorName(); System.out.println("Heman owns a weapon named "+HemanWeapon); System.out.println("Heman Armor is "+HemanArmor); } }
The Explanation
- Initially i have created a class called “gameCharacter”
- This Class only accept 2 types “
” - T for weapon type , A for armor type
- Then i created a Constructor/ getter and setter for “gameCharacter “
- The Second and third class which i created were the “weaponry and armory ” class
- Constructor, Getter and Setter were created for both class
- The final class i created was “recruitSoldier ” Class this class will be used to access the ” gameCharacter , armory, weaponry” class
- “weaponry sacredSword = new weaponry(“Sword Of Gray Skull”); ” Create a new weapon object named “sacredSword” and pass a name to the new Weapon “”Sword Of Gray Skull””
- “armory noArmor = new armory(“Naked”); ” Create a new amory object named “noArmor” and pass a name to the new armor”Naked”
- ” gameCharacter
Heman = new gameCharacter (sacredSword,noArmor);”Cretate a new gameCharacter called” Heman , equipped with the new object Type being created above - ” String HemanWeapon = Heman.weapon.getWeaponName(); ” Retrieve the Name of the Weapon Assign to Heman
- ” String HemanArmor = Heman.armor.getArmorName(); ” Retrieve the Name of the Armor Assign to Heman
- Finally print out Heman weapon and armor
Check out what is Up Casting and Down Casting Here