Saturday 19 October 2013

Abstract class vs Interface

Abstract class:
  • cannot be instantiated.
  • is a special type of class in which you can have members without implementation.
  • as we know in C#/VB/Java, a class can only inherit from 1 class. This also applies for Abstract Class.
  • normally used for framework-type library classes: providing default behavior for some of its class members, but forcing the developer to implement others.
  • is believed to be faster in Java,
  • the aim: making sure something is *eventually* implemented.
  • A class can inherit an abstract class without implementing all its abstract method.
    However only a class that has all its method implemented can be instantiated to an object.
  • IS-A relationship.
  • e.g. Student IS A Person, Employee IS A Person.
// 'framework library' for a person
// a person can enrol and submit
// however, the class that consume this framework library
// need to provide 'where' the paperwork need to be sent
public abstract Person
{
    public abstract SendPaperWork(string paperwork)

    public void Enrol()
    {
        SendPaperWork("enrolment");
    }

    public void Submit()
    {
        SendPaperWork("report");
    }
}

// by inheriting Person abstract class
// we are enabling student to enrol and submit
// however, SendPaperWork need to be implemented
// because we need to tell it explicitly 'where' 
// to send the enrolment/ submission
public class Student : Person
{
    public override SendPaperWork(string paperwork)
    {
        School.Send(paperwork);
    }
}

// an employee send the paperwork to a different 'place' than student
public class Employee : Person
{
    public override SendPaperWork(string paperwork)
    {
        Company.Send(paperwork);
    }
}
Interface:
  • cannot be instantiated.
  • is a special type of abstract class in which all the members do not have any implementations.
  • enables polymorphism. A class can implement more than 1 Interfaces.
  • normally used for application classes: providing contract for ensuring interactibility.
  • the aim: making sure something is interchangeable.
  • A class that implements an Interface need to contain all the implementation, otherwise the compiler will throw an error.
  • CAN-DO relationship.
  • e.g. Student CAN enrol, Student CAN submit assignment.
public interface ICanEnrol
{
    void Enrol();
}

public interface ICanSubmit
{
    void Submit();
}

public class Student : ICanEnrol, ICanSubmit
{
    public void Enrol()
    {
        School.Send("enrolment");
    }

    public void Submit()
    {
        School.Send("report");
    }
}

public class Employee : ICanEnrol, ICanSubmit
{
    public void Enrol()
    {
        Company.Send("enrolment");
    }

    public void Submit()
    {
        Company.Send("report");
    }
}

public class MailServer
{
    public void SendAllSubmissions()
    {
        // AllSubmitters is a collection of students and employees
        foreach (ICanSubmit submitter in AllSubmitters)
        {
            // The MailServer does not care if 
            // the submitter is a student
            // or an employee, as long as it can submit
            submitter.Submit()
        }
    }
}

1 comment: