Teaser Image

qnoid

Markos Charatzas - London, UK




public interface Person
{
    /**
     * We can both make posts. Woohoo.
     */
    public void makePost(Post post);
}


/**
 * Abstract class encapsulating the highly sophisticated algorithm of writing a
 * post. Developer, Designer, doesn't matter. 
 */
public abstract class AbstractPerson implements Person
{
    protected abstract void title(Post post);

    protected abstract void pasteCode(Post post);
    
    protected abstract void description(Post post);
    
    protected abstract void tags(Post post);
    
    @Override
    public void makePost(Post post)
    {
        title(post);
        pasteCode(post);
        description(post);
        tags(post);
    }
}

/**
 * Developer
 */
public class Developer extends AbstractPerson implements Person
{
    @Override
    protected void title(Post post)
    {
        post.title("I'm a developer, you are a designer, we get along. We are both Person(s) after all.");
    }

    @Override
    protected void pasteCode(Post post)
    {
        //Java code about abstract classes and interfaces. Didn't want to create an endless recursion.
    }

    @Override
    protected void description(Post post)
    {
        /*
         *What's the difference between an Abstract class and an Interface? 
         *Radical. A common misunderstanding and a source of confusion. 
         */
    }

    @Override
    protected void tags(Post post)
    {
        post.tags("java");
    }
}

/**
 * Designer
 */
public class Designer extends AbstractPerson implements Person
{
    @Override
    protected void title(Post post)
    {
        post.title("Padded column Compass mixin");
    }

    @Override
    protected void pasteCode(Post post)
    {
        /*
         * cool CSS snippet
         */
    }

    @Override
    protected void description(Post post)
    {
        /*
         * Unfortunately, Compass @column mixin is a bit inefficient, since it 
         * just sets width so it requires a nested div to set padding without 
         * breaking the grid.
         */
    }

    @Override
    protected void tags(Post post)
    {
        post.tags("CSS");
    }
}

What's the difference between an Abstract class and an Interface? Radical. A common misunderstanding and a source of confusion.

I'm a developer, you are a designer. That's fine. We should both be able to make posts in Forrst. We are Person(s) after all.

An interface is all about behaviour. What can both a Developer and a Designer do in Forrst? That's right, make posts. That's our common behaviour here in Forrst, what makes us interchangeable.

Now I will be most likely writing about Java and you will be showcasing your million dollar design, but we both have to go through the same process of writing our post.

  • Specify a title
  • Paste some code
  • Enter a description
  • Put some tags

Now that's a pretty nifty algorithm that we both have to go through to write a post and that's what a base class is good at. Encapsulating common code.

However, since we won't be writing the same stuff anyway though (title, code, description, tags), it's destined to be abstract.

A follow up post titled Why I don't drive a Porsche tries to put the final nail in the coffin on when/how to use an interface compared to an abstract class

Disclaimer: Another trivial example, don't put that in production code please. Special thanks: @sugarenia for the designer's code