Object Oriented Programming(OOP) Series: Classes and Objects

Object Oriented Programming(OOP) Series: Classes and Objects

Classes and Objects in simple terms with illustrations

In this series we'll be brushing up OOP concepts in the most simplest form with examples in PHP, Javascript and Dart

Object-oriented programming (OOP) is a programming style based on the idea of representing real life scenarios using "objects", with their unique or common description been represented as attributes and the set or defined way of their interaction or behavior referred to as methods.

One of the essential qualities of object oriented programming is the access to class attribute and methods by a copy of it's own object through inheritance based on the access method in use.

There are numerous OOP languages in the field of technology such as C++, Java, PHP, Javascript, Python, Dart, Go etc.

Object-oriented programming simply represent real life situation in form of objects that represents the members or attributes and it's behavior.

The need to writing codes using the OOP style is driven majorly by reuse and separation of concern. For example if we needed to define an object called car. The things we can identify are used to define it's attributes and methods (behavior). Attribute or object members of a car include door, tires(4), steering, seats(4 or more) while the method (action or behavior) of a car includes driving, horning, reversing, headlight effect, air conditioner, wiper etc.

In OOP different objects attributes and methods can be bundled together in a class which in turn can be an object. Let's give more illustration using the car. A car is a general name for a transport system with 4 tires. Benz or Toyota is an object(example or instance) of this car class. Further more different models are instances or examples of Benz or Toyota which makes Toyota a class.

Class Car

car.jpg

Example (object) of Car class: Toyota

toyota model1.png

Simply put, an object is an instance of a class. Objects themselves can become classes if instances are created from them.

For example the Toyota 4Runner can become an object of Toyota.

toyota 4runner.png

An instance is like an example or similar occurrence that can be likened to something else. Toyota is an instance of a Car. This instance called Toyota is what we refer to as an object.

A Toyota(an object) is a combination of the attributes(members or variables), methods(behaviors or actions), style of arrangement in the class of Car.

A better way is to relate this to the Grade or Class in high school. Class 6 or Grade III is a general name for all the students who fall under that category, basically a student in Class 6 or Grade III is an example of that class which can be called an instance because the student must posses one or more characters and behaviors that distinguishes the class or grade from other classes.

Class: a code template that is extensible to have more code templates inheriting it's member and behavior by providing their default or initial format of state(features or attributes) and actions(methods)

Awesome!!! that's all!

Practice! Practice!! Practice!!! : let's look at practical application of classes and objects in some programming languages briefly

In subsequent part of these series we'll be looking at other part of these codes. For now please let's keep it simple and be objective(for the purpose of learning classes and objects).

Simple Classes and Objects in Javascript :

Classes in JS are built on prototypes(similar copy or instance of an something) but also have some syntax(rules) and semantics(meaning) that are not shared with ES5(2015 version of Javascript) class-like semantics.

class Car{
    constructor(type, model) {
        this.type = type;
        this.model = model;
    }

    speed() {
        return `${this.type} ${this.model} has speed`;
    }

}

let toyota = new Car("Toyota", "Camry");

console.log(toyota);

console.log(toyota.speed());

In the example above

The name of the class is Car .

let toyota = new Car("Toyota", "Camry"); this line of code creates an instance of the Car class and store it in a variable.

You can run the above snippets on a browser console or command line prompt if you've node installed.

js-classes-obj-on-console.png

Classes and Objects in Dart:

class Car{
   var type;
   var model;

  info(){
    print("Car ${type} model is: ${model}");
  }
}

void main(){
  var toyota = new Toyota();
  toyota.type = "Toyota";
  toyota.model = 2019;
  toyota.info();
}

In the dart example above

The name of the class is Car.

var toyota = new Car(); this line of code creates an instance of the Car class and store it in a variable called toyota.

The output for the code above is: Car Toyota model is: 2019

Classes and Objects in PHP:

<?php

class Car {
    public $type, $model;
    function info() {
        print 'Car '.$this->type.' is: '.$this->model.' ';
    }
}

$toyota = new Car;
$toyota->type = 'Toyota';
$toyota->model = '2020';
$toyota->info();

?>

In the PHP example above

The name of the class is Car.

**let toyota = new Car; ** this line of code creates an instance of the Car class and store it in a variable called toyota.

The output for the code above is: **Car Toyota model is: 2020 **

Stay tune for the next article on the series OOP Series: Attributes and Methods

Find this helpful or resourceful?? kindly share and feel free to use the comment section for questions, answers, and contributions.

💡
Follow me on Hashnode: Alemsbaja X: Alemsbaja | Youtube: Tech with Alemsbaja to stay updated on more articles

Did you find this article valuable?

Support Alemoh Rapheal Baja by becoming a sponsor. Any amount is appreciated!