Pitch your project

Random text






Blogs

0    
By admin Category Uncategorized, Posted February 16th, 2016
Object Oriented Programming in a nutshell

Programming methodologies and programming languages have evolved over the years since its early days. There have been a few generations of programming languages the most popular could be classified in two groups namely procedural or structured programming languages and object oriented programming (OOP) languages. Procedural programming languages reigned supreme for quite a while before it became abundantly clear to most computer scientists that the procedural programming languages presented some difficulties when trying to develop larger more complex systems. This triggered the conception of the Object Oriented Programming methodology which presented an opportunity to develop systems that better resembled real world objects and scenarios better and promoted re-usability of code which resulted in cutting down on the amount of Kilo Lines of Code (KLOC) one had to write for a system with increasing complexity. We are going to look at some of the advantages of OOP using Java as the language.

Inheritance

This is where objects inherit the properties of other objects. This is extremely useful as it mirrors real world objects quite accurately and helps manage smaller code. Take for example we have a class of objects called shapes that has properties that generally describe two dimensional shapes (e.g. they have a width and a height). If one was to create an instance or rather an object of class shape called triangle, triangle would automatically inherit the properties of shape then we would go further and define properties unique to the triangle such as its shape or colour. To put this in contrast, in procedural programming languages, one would have to repeatedly define all the properties individually from scratch, of each shape we want to represent which would result in bulky, tiresome code.

Example code:
    class twoDShape
    {
        double width;
        double height;

        void showDim()
        {
            System.out.println("Width and height are " +
                width + " and " + height);
        }
    }

    class Triangle extends twoDShape
    {
        String style;

        double area()
        {
            return width*height/2;
        }

        void showStyle()
        {
            System.out.println("Triangle is " + style);
        }
    }

In the code above, the class Triangle extends twoDShape which implies it will inherit all the properties of twoDShape. We can also see that a new property named style unique to triangle has been defined in the code as well as a new method that prints out the style property.

Encapsulation

Encapsulation basically means that code and the data it manipulates is binded together which allows the object to avoid interference and misuse from external factors. This is perhaps best demonstrated using the public and private keywords that are used in Java:

    private int height, width;

    access(int h, int w)
    {
        height=h;
        width=w;
    }

    public int area()
    {
        return height*width;
    }

In the code above, any variable and method names that are preceded with the keyword private are only accessible by other methods and other variables within the same class. All variables and methods preceded with the public keyword are accessible by methods and variables both inside and outside its class. In Java, the default access provided to methods and variables is public.

Method Overloading

This is one of the core advantages of OOP. Normally we would prefer it if some method that we’ve generated can be used with multiple data types. This would normally be the case in a real world scenario where we perform the same function on different kinds of data types. To understand this better let’s take a simple multiplication function where the arguments can be two numerical data types.

    public multiply(int num1, int num2)
    {
    return num1*num2;
    }

    public multiply(float num1, float num2)
    {
        return num1*num2;
    }

As you can see from the example above one can use the same method name on multiple methods as long as the parameters of the method are different. The parameters of a function are normally referred to as a signature in Java so two overloaded methods would naturally have two different signatures. In the example above, the method multiply has been declared twice but the parameter data types are different. One accepts integer parameters while another accepts float parameters. The advantage of this is when the method is called Java, will automatically determine which version of multiply is appropriate for use depending on the data types of the parameters. Method Overloading represents a concept in OOP methodology called Polymorphism where an interface can access a general class of actions and the specification is determined by the exact nature of the situation.

There are more advantages to using the OOP methodology but this was just a short introduction to some of the core advantages. Different programming languages might present them differently but they all have similar origins.

Posted by Cyrus Muriithi

Leave a Reply

Your email address will not be published. Required fields are marked *