• Mr R

Classes of Classy Classes

In the prior post on classes we discussed their basic usages of classes and how they support the object-oriented programming (OOP) paradigm, allowing for the abstraction of real world concepts into self contained units within your system.


In this section we will discuss some of the types of classes that we may use in our programming to facilitate development. We are going to breakdown the main categories of classes and describe their usages, basing our examples and terminology on the java ecosystem (though the basic concepts will be applicable across all OOP capable languages).


Basic Class Concepts


Inheritance


As OOP is a method that uses abstraction of concepts into our systems to build a cohesive codebase and reduce redundancy, we sometimes find that multiple classes may have common attributes; sometimes they may even be types of the same thing. In this case we can use inheritance to encapsulate the common functionality.


To use inheritance, we must create a parent class, which contains all the common variables and methods that the child classes need. These can then be inherited by the children. In java we use the extends keyword in the class declaration to inherit from a parent. A class may only have one parent it extends.


A child class' object is also considered to be a member of the parent class.


Interfaces


If we want to inherit from multiple classes, which we cannot do, there is another form of reducing redundancy, these are interfaces. An interface can be viewed as a type of contract, a promise to implement all the members of the interface.


To incorporate interfaces in java we use the implements keyword in the class declaration. A class can implement multiple interfaces, and does not count as being a member of the interface's class.


Class Types


Concrete Class


The concrete class is the basic class type. Most scenarios will require the use of this type of class. Concrete classes can both inherit and be inherited by other classes, and can implement contracts.


To build a concrete class the class must be defined without any other modifiers as follows:

public class HelloWorld()
{
    
    private String greeting;
    
    /**
     * Constructor HelloWorld
     *
     * @param greeting - Greeting to be displayed
    */
    public HelloWorld(String greeting)
    {
        this.greeting = greeting;
    }
    
    /**
     * function output - Outputs the message
    */
    public void output()
    {
        System.out.println('Hello world! ' + this.greeting);
    }
    
}

Abstract Class


The keyword abstract, refers to the inability to use the class or method as it is (the fancy work we use for this when it comes to classes is instantiate). We can think of abstract classes as blueprints for other classes. An abstract class is created to be inherited by another in order to remove redundancy of code.


To build a abstract class the class must be defined with the abstract keyword as follows:

public abstract class AbstractMessageSender()
{
    
    protected String message;
       
    /**
     * function outputMessage - Outputs the message
    */
    public void outputMessage()
    {
        System.out.println(this.message);
    }
    
}

public class HelloWorld extends AbstractMessageSender()
{    
    /**
     * function sayHelloWorld- uses parent function to output message
    */
    public void sayHelloWorld()
    {
        this.message = 'Hello World!';
        this.output;
    }
    
}

We use the protected keyword when declaring the variable to make it mutable by the child class which inherits the variable.


Static Class


The static keyword is used to indicate when something will not change from instance to instance. Though most commonly used on variables and methods, there are certain uses for the static class, though there are certain limitations to these classes:


  • You cannot instantiate an object of a static class.

  • They can only access the static members of their outer classes.


We may use a static class as follows:

public class WebsiteVisitor()
{
    
    private static int counter;
    private int visitorNumber;
    
    /**
     * Constructor WebsiteVisitors
    */
    public WebsiteVisitors()
    {
        this.counter += 1;
        this.visitorNumber = this.counter;
    }
    
    /**
     * Constructor WebsiteReadout
    */
    public static class WebsiteReadout()
    {
        
        /**
         * function readout - reads out the static contents
        */
        public void readout()
        {
            System.out.println(counter);
        }
        
    }
    
}

This system will maintain the same counter value for all instances while the other variables may differ. The readout is used to view the contents of the static variable.


Singleton Class


As the name implies, there can only be one instance of a singleton class. If a new object instance of a class is created it will only create a pointer to the original instance. Use these if you want there to at any point only be one of a class instantiated.


To build a singleton class there are a couple things we must remember to do:

  • Make the constructor private.

  • Use a static method to initialise the class.

This can be done as follows:

public class SingletonExample()
{ 
    private static Singleton instance = null;
    
    /**
     * Constructor SingletonExample
    */
    private SingletonExample()
    {
        System.out.println('Created Singleton');
    }
    
    /**
     * function getInstance - returns instance of singleton class
    */
    public static SingletonExample getInstance()
    {
    
        if(this.instance == null)
        {
            try {
                this.instance = new SingletonExample();
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    
        return this.instance;
        
    }
    
}

Final Class


A final class is one which is designed to be immutable (unchanging), you may find it easier to consider this the constant of classes. This also means that the class cannot be extended/inherited. Once the class is instantiated into an object the values will remain constant for the lifespan of the object.


To create a final class it is as simple as using the final keyword:

public final class FinalClass()
{ 
    
    /**
     * function printMesage - prints a message
    */
    public void printMesage()
    {
        System.out.println('A message');
    }
    
}


Food for thought

These keywords can also be used on variables and methods, how do you think these may be useful in conjunction with the information we have just acquired.

Recent Posts

See All

Getting Classy with Classes

A brief introduction of Object-Oriented programming, focussing on classes, methods and objects.