Generic Object Type in Java

Generic Object Type in Java

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:

  1. gameCharacter
  2. armory
  3. weaponry
  4. 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

  1. Initially i have created a class called “gameCharacter”
  2. This Class only accept 2 types “
  3. T for weapon type , A for armor type
  4. Then i created a Constructor/ getter and setter for “gameCharacter “
  5. The Second and third class which i created were the “weaponry and armory ” class
  6. Constructor, Getter and Setter were created for both class
  7. The final class i created was “recruitSoldier ” Class this class will be used to access the ” gameCharacter , armory, weaponry” class
  8. “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””
  9. “armory noArmor = new armory(“Naked”); ” Create a new amory object named “noArmor” and pass a name to the new armor”Naked”
  10. ” gameCharacter Heman = new gameCharacter(sacredSword,noArmor);”Cretate a new gameCharacter called” Heman , equipped with the new object Type being created above
  11. ” String HemanWeapon = Heman.weapon.getWeaponName(); ” Retrieve the Name of the Weapon Assign to Heman
  12. ” String HemanArmor = Heman.armor.getArmorName(); ” Retrieve the Name of the Armor Assign to Heman
  13. Finally print out Heman weapon and armor

 

Check out what is Up Casting and Down Casting Here

Leave a Reply

Your email address will not be published. Required fields are marked *

one × 2 =