Pitch your project

Random text






Blogs

0    
By admin Category Uncategorized, Posted March 21st, 2016
OOP Basics

Object Oriented Programming has been the mainstay in software development as they present a very easy way of understanding a project wholly as well as conceptualise the type of work in a split second.

C++ in its early days advanced this concept and fortunately it was a welcome thing that’s why nowadays grasping concept-sis not as difficult (except for pointers).

Picture this analogy.

Toyota (it’s big here in East Africa; I would kill for a Range Rover Sport/BMW M5/Merc-E-Class; wont die for it though), is a car manufacturer with different models. All this cars have the same functionality (open the door turn ignition on starts the engine, engage gear and drive off; yeah drive it fast like you stole it ).Even though there may be variations like AT/MT transmission or electric seats etc, the backbone procedure remains the same.

That analogy is can be explained demonstrated with OOP principles.

In this article will look at Objects, classes and Inheritance with respect to the Toyota analogies.

Object

An object are used to represent real life objects like a cat , a tree or cars. Objects must have a state and behaviour. A car is an object thus we expect its state to be things like colour and name and behaviour would be making a drift at a sharp corner and braking.

Classes

This is a collection definitions and actions which make up a class. A general class with functions and initialisation will define how a basic car will be. For example set the initial gear to neutral and doors shut.

Codewise:

class Car {

    boolean doors_shut = true;
    int speed = 0;
    int gear = 1;

    void changeGear(int newValue) {
         gear = newValue;
    }

    void speedUp(int increment) {
         speed = speed + increment;
    }

    void applyBrakes(int decrement) {
         speed = speed - decrement;
    }

    void printStates() {
         System.out.println("Doors are Shut:" +
             doors_shut + " speed:" +
             speed + " gear:" + gear);
    }
}

This is defining what all cars can do.

Inheritance

As the name suggests, we borrow already existing instances and functions then we use it as-is or with a little more modification(method overloading).

Thus an AT/MT car will function the same way even though one of them does not use the stick. this happens when it inherits the class Car and uses whats provided with a twist.

Codewise:

class AT extends Car{
  void changeGear() {
         gear++; //increases gear by one
    }


}

This means that the gear is not set but shifts to the next higher one. And all previous instances of gear and doors are gotten from the main class Car.

So from the above we can say:
Cars are objects which have state(Toyota) and behaviour(brakes, shift gear, drift). Classes define the objects explicitly in terms of behaviour using functions and if Toyota will release a new model of a car they will inherit the Car class and overload some methods to achieve a different behaviour other than the one stipulated in the parent class. Ciao!!!

Posted by Marvin Ngondi

Leave a Reply

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