Classing{js} Tutorial

Interfaces

What is an Interface?

Interfaces are very similar to abstract classes, they are structures that cannot be instantiated but can be implemented. Interfaces contains only method , and these methods are abstract by default. So , we say that intefcaes are used to define a behaviour that some group of classes will share but each of these classes have different implementation of this behaviour. So, the main difference between abstract classes and interfaces is that abstrcat classes define a characterization (which includes traits and behaviour) while interfaces only define behaviour.

Take, for example, a company that hire full time employees and part time employees. Both types share the behaviour of getting paid but each of them is getting piad in a different way than the other : the full time employee has a fixed monthly salary but the part time employee gets paid with an hourly rate . So, the behaviour of getting paid is represented by an interface, and both the full time and part time employees are classes that implements that interface.

How to Create an Interface?

creating an intrface is done by calling the function classing.Interface in the following manner:

var InterfaceName = classing.Interface({
	/* interface methods go here */
});
				

The methods of the interfaces are just the signatures of the methods : the name and the parameter configuration only.So, an interface method must have an empty body (just like the abstrcat functions).Also, note that threre are no access modiferes in the defintion of an interface, all the methods of the interface are public.

Here's an example using the company analogy we used above:

var IPayable = classing.Interface({
	getPaid : function(){}
});
				

How to Implement Interfaces?

Implementing interfaces is done using classing.Class.Implements in the following manner:

var ClassNames = classing.Class.Implements(/* reference  to interface(s) to implement */)({
	/* class' defintion goes here */
});
				

Continuing with the company example , here are the empolyee classes that implement the IPayable interface:

var FullTimeEmployee = classing.Class.Implements(IPayable)({
	private : {
		fixedSalary : 5000,
		balance : 0,
	},
	public : {
		getPaid : function() {
			this.balance += this.fixedSalary;
		}
	}
});

var PartTimeEmployee = classing.Class.Implements(IPayable)({
	private : {
		hourlyRate : 10,
		hoursWorked: 0,
		balance : 0,
	},
	public : {
		addToHoursWorked : function(hrs) { this.hoursWorked += hrs; },
		getPaid : function() {
			this.balance += this.hoursWorked*this.hourlyRate;
			this.hoursWorked = 0;
		}
	}
});
				

A class can implement mutilple interfaces. Assuming that getting promoted is a behaviour the FullTimeEmployee have, we can write the following:

var IPromotable = classing.Interface({
	getPromotred : function(){}
});
var FullTimeEmployee = classing.Class.Implements(IPayable , IPromotable)({
	private : {
		fixedSalary : 5000,
		balance : 0,
	},
	public : {
		getPaid : function() {
			this.balance += this.fixedSalary;
		},
		getPromotred : function() {
			this.fixedSalary += 1000;
		}
	}
});

				

A class can inherit form another class and also implement mutiple interfaces like in the following example:

var Person = classing.Class({
	public : {
		name : null,
		age : null
	}
});
var FullTimeEmployee = classing.Class.Extends(Person).Implements(IPayable , IPromotable)({
	private : {
		fixedSalary : 5000,
		balance : 0,
	},
	public : {
		getPaid : function() {
			this.balance += this.fixedSalary;
		},
		getPromotred : function() {
			this.fixedSalary += 1000;
		}
	}
});

				

Heads Up!

  • Interface's methods can be regular javascript functions or Classing{js} overloaded functions.
  • In case of overloaded functions, each instance must have an empty body (like in abstract methods).
  • In a class that boths extends and implements, the Extends(...) must come before the Implements(...). In other words, write : classing.Class.Extends(...).Implements(...). Writing them in the reverse order will throw an error.