Interfaces

Interfaces are a key source of flexibility and re-usability in Object oriented systems. They look very similar to regular Java classes yet they provide no real functionality within them and consist of a group of related methods with empty bodies. I.e Methods that have no functional code within them. They are essentially blueprints to developers working in teams as they tell any developers exactly what methods must be implemented in any new classes that inherit from them.

Through using interfaces we allow classes to become more formal about the behavior that it promises to provide. So if one of your classes implements an interface, all the methods within that interface must be defined by said class.

Implementing an Interface:

When implement a class that implements the only rule we must stick to is that if a method has been defined in an interface then it must be implemented in this newly created class.

IEnemy – Enemy Interface Class:

public interface Enemy {
	
	
	public void attack();
	
	public double getHealth();
	
	public void setHealth(double newHealth);
	
}

Infantry Class:

public class InfantryImpl implements Enemy{

	double health;
	int ammo;
	
	@Override
	public void attack() {
		// TODO Auto-generated method stub
		System.out.println("Infantry Attack!");
	}

	@Override
	public double getHealth() {
		// TODO Auto-generated method stub
		return this.health;
	}

	@Override
	public void setHealth(double newHealth) {
		// TODO Auto-generated method stub
		this.health = newHealth;
	}

}

Abstract Classes

An abstract class is one that represents a generalization and provides functionality but is only intended to be extended and not instantiated. Any abstract classes that we build are meant to be super classes that will provide the base functionality for a group of concrete classes.

Rules:

  • Abstract classes cannot be instantiated independently from their derived classes.
  • Abstract class constructors are called only by their derived concrete classes.
  • Abstract classes can include both static and instance variables
  • Deriving from an abstract class uses up a subclass’s one and only base class option. You cannot inherit from any other classes.
  • The following UML diagram showcases the sort of relationship an abstract class should have with concrete classes. In this example we have the Enemy abstract class, note that Enemy is in italics as is custom for abstract classes in UMLs. The Enemy class implements both the getHealth() and setHealth(arg : double) methods but again denotes that the Attack() method is an abstract method and as a result, this should be implemented in the Sniper class.

Enemy Class

public abstract class Enemy {

	double health;
	int ammo;
	
	public abstract void attack();
	
	public Double getHealth(){
		return this.health;
	}
	
	public void setHealth(double newHealth){
		this.health = newHealth;
	}
	
}

Sniper Class

public class Sniper extends Enemy{
	
	@Override
	public void attack() {
		System.out.println("Sniper Attack");
	}
	
}