What is Java generics? It is the ability defining a scope without really knowing the scope, but knowing limitations to the scope. Generics was the obvious follow up tot the concept of interfaces. Taking abstract to a whole new level of awesomeness!!!
Let me explain in the most simplest ways I can think off:
1. You have a hosepipe
2. You want to fit odds and ends to the hosepipe (sprayGun)
3. you need to develop a middle object connector, interface for these objects to fit onto the hosepipe
4. you want the connector to be able to do a specific set of access ways (connectToPipe, connectToConnector, sendWater, recieveWater)
5. you want the connector to have a set expected input, and expected outputs (waterFlow,5mmWaterFlow,3mmWaterFlow)
So we begin:
HosePipe.java
public class HosePipe extends Connector<WaterFlow5mm>{
public void sendWaterflow(WaterFlow5mm waterflow){
// Do whatever to the water that was sent to me
System.out.println(waterflow.getWater().toString(););
}
public WaterFlow5mm recieveWaterflow(){
return new WaterFlow5mm();
}
}
SprayGun.java
public class SprayGun extends Connector<WaterFlow5mm>{
public void sendWaterflow(WaterFlow5mm waterflow){
// Do whatever to the water that was sent to me
System.out.println(waterflow.getWater().toString(););
}
public WaterFlow5mm recieveWaterflow(){
return return new WaterFlow5mm();
}
}
Water.java
public class Water(){
private int oxygen = 1;
private int hydrogen = 2
public int getOxygen(){
return oxygen;
}
public int getHydrogen (){
return hydrogen ;
}
}
WaterFlow.java
public abstract class WaterFlow(){
public Water[] getWater();
}
WaterFlow5mm.java
public class WaterFlow5mm(){
public Water[] getWater(){
return new Water[5];
}
}
Connector.java
public abstract class Connector<A extends WaterFlow> implements Connectable<A>{
boolean connectToPipe(){
//click
};
boolean connectToOdds(){
//click
};
public abstract void sendWater(A waterflow);
public abstract A recieveWaterflow();
}
Connectable.java
public interface Connectable<A extends WaterFlow>{
boolean connectToPipe();
boolean connectToOdds();
void sendWaterflow(A waterflow);
A recieveWaterflow();
}
Now there are many more examples how generics can be used to simplify 'n project, but also be careful NOT to over complicate your project with generics.

