Programming For Non-Techies - Interface Types

Programming For Non-Techies - Interface Types

Programming For Non-Techies is a weekly feature for non-techies to learn to program every Friday.  This week’s lesson continues the discussion about variables and we will look at Interface Types.   If you’re a non-techie and you want to learn to program, or maybe you want a job in tech and need a better understanding of programming, these posts will help you!

If you’re new here, I strongly suggest looking at the previous programming posts since they build on each other.

What is an interface type?

An interface is a contract for a class.  They are abstract types that you can’t instantiate.  The way you use them is a class implements a type and you instantiate the class.  The easiest way to look at this is to see it in code.

I modified the football example that we used before.  Now there are multiple players on the field and we can handle different kinds of players.  I changed FootballPlayer to an interface and added Linebacker and Quarterback classes to implement it.

There’s a new method in FootballPlayer called doJob().  See how Linebacker and Quarterback implement it differently?  As long as these classes implement the methods specified in the interface, they can do whatever they want so we have a few small differences between them.

Now, look at FootballField.  I changed it to handle an array of FootballPlayer.  In the main() method we create the players.  We set them all up as FootballPlayer, but when we call new to instantiate them we use the class that implements the interface.  On line 54, we use the doJob() method from the interface.  We implement that differently in Quarterback.  This lets you have consistent logic that changes based on the implementing class.

On line 55, we cast the player to a Linebacker.  This is because of strong typing and the players array being FootballPlayer.  When we are 100% sure that the implementing class is a certain type then we can cast to that type to make the other methods in the class available.  You can also check the type with the instanceof keyword.  There’s an example in Linebacker.

What’s next?

Know that there is a fourth reference type called a Type Variable.  It’s basically what we’re doing here with the field or players variables, so I won’t write an entire post on it.  The important thing is that we have gone through enough to write a program.  Copy the example code into Eclipse and run it.  What do you get?  Now try to add a new type of player.

Over the next few weeks, I’ll show you Object Oriented Programming concepts.  It’s a lot more abstract than the basics of a programming language, but there will still be Java code examples.

Example Code

/**
 * Football Field example
 */

/**
 * @author techfornontechies.com
 *
 */
public class FootballField {
	FootballPlayer[] players;

	public FootballPlayer[] getPlayers() {
		return players;
	}

	public void setPlayers(FootballPlayer[] players) {
		this.players = players;
	}
	
	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("FootballField has " + players.length + " players.");
		for(int i=0; i<players.length; i++)
		{
			sb.append("   ");
			sb.append(players[i]);
		}
		return sb.toString();
	}

	public static void main(String[] args) {
		FootballField field = new FootballField();
		
		FootballPlayer[] players = new FootballPlayer[3];
		
		FootballPlayer player = new Linebacker();
		player.setName("Mercilus");
		player.setNumber("59");
		players[0] = player;
		
		player = new Linebacker();
		player.setName("McKinney");
		player.setNumber("55");
		players[1] = player;
		
		player = new Quarterback();
		player.setName("Luck");
		player.setNumber("12");
		players[2] = player;
		
		player = null;
		players[0].doJob(players[2]);
		((Linebacker)players[1]).tackle(players[2]);
		
		field.setPlayers(players);
		
		System.out.println(field);
	}
}
/**
 * Football Player example
 */

/**
 * @author techfornontechies.com
 *
 */
public interface FootballPlayer {
	public String getNumber();
	public void setNumber(String number);
	public String getName();
	public void setName(String name);
	public void doJob(FootballPlayer player);
}
/**
 * Football Player example
 */

/**
 * @author techfornontechies.com
 *
 */
public class Linebacker implements FootballPlayer{
	private String number;
	private String name;
	private int tackles = 0;
	
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void doJob(FootballPlayer player) {
		tackle(player);
	}
	public boolean tackle(FootballPlayer player) {
		//add tackle code
		tackles++;
		if(player instanceof Quarterback) {
			((Quarterback)player).addSack();
		}
		return true;
	}
	@Override
	public String toString() {
		return "FootballPlayer [number=" + number + ", name=" + name + ", tackles=" + tackles +"]";
	}
}
/**
 * Football Player example
 */

/**
 * @author techfornontechies.com
 *
 */
public class Quarterback implements FootballPlayer{
	private String number;
	private String name;
	private int passes = 0;
	private int sacks = 0;
	
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void doJob(FootballPlayer player) {
		throwPass(player);
	}
	public boolean throwPass(FootballPlayer receiver) {
		//add code to decide if it's a catch
		return false;
	}
	public void addSack() {
		sacks++;
	}
	@Override
	public String toString() {
		return "FootballPlayer [number=" + number + ", name=" + name + ", passes=" + passes + ", sacks=" + sacks + "]";
	}
}

Leave a Comment