Object Oriented Programming(OOP) Series: Attributes and Methods

Object Oriented Programming(OOP) Series: Attributes and Methods

Attributes and Methods in simple terms with illustrations

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

💡
In the first part of this series we looked at Classes and Objects.

Object Oriented Programming is a style or an approach towards writing programs that are structured on data members(attributes) and behavior(methods) with creation of objects. A class is basically a blueprint or layout for creation of objects. These have their own attributes (characteristics), and methods (behavior). A Class can be a parent of many objects while an object is a child of a class.

Inside the class closure**{}** variables are called attributes(data members) and the functions inside that get or set values are called methods.

Attributes

Attributes are data members inside a class or an object that represent the different features of the class. They can also be referred to as characteristics of the class that can be accessed from other objects or differentiate a class from other classes. For example a car attribute include: tire, door, seats, license plate, headlight, wheel, handle, make, year. The essence of defining variables in a class is to keep the code simple and maintainable. In the case of a car the attributes mentioned above are similar hence the need of defining them while creating the car class.

Instance Attribute

An instance attribute is the data member of an object. It's scope of access is within the object creation and is defined inside the constructor(automatically executed method when an object is created from a class) of a class.

Class Attribute

The attributes defined in a class that can be shared or accessed by it's objects and is defined outside the constructor.

Methods

In classes methods are responsible to modify or define the behavior of the class and it's objects. A car method includes start, headLightOn, drive, stop, openDoor, horn etc. The methods in a class can either access(get or set) an attribute or perform a specific operation. It's a logic or procedure defined in a class to do something. Methods in classes lay out the behavior of objects that will be created. For instance, if a car owner presses the horn button it'll operate by horning. Similar to attributes the essence of defining methods in a class is to enable consistency, readability, simplicity, maintainability and ease of reuse with modification by objects where necessary. The syntax of a method is similar to a function but is defined and maintained by the class. Similar behavior(action or methods) of cars can be defined in the car class instead of having to define separate functions for every car that's been created.

In the next part of this series, we'll be looking at constructors and destructor for more understanding.

Simple attributes and methods in Javascript

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.type);

//OR

console.log(toyota.['type']);

console.log(toyota.speed());

Explanation:

constructor(type, model) the attributes inside the constructor method are referred to as instance attribute.

speed() is a method of the car class

toyota.type or toyota.['type'] the dot operator is used to access the attributes of a class in Javascript.

toyota.speed() accessing the method of the car class through the toyota object.

💡
Checkout My Tech Youtube Channel for more tutorials and guides

Attributes and Methods 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();
}

Explanation: ** var type; var model;** are examples of class attributes

The main() method is a constructor in Dart that automatically executes the code inside it's closure.

info() is a method of the car class

** toyota.type = "Toyota"; ** the dot operator is used by the object to access the attribute and define it's value.

** toyota.info();** the toyota object can make use of the info() method in the class.

Attributes and Methods 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();

?>

Subsequently we'll have a look at access modifier (public, private protected and internal(in some languages))

Here in PHP when defining the attributes of a class the access modification has to be defined.

public $type, $model; the public declaration of the attributes of the car class.

$toyota->type = 'Toyota'; the access to the attribute and setting it's value

$toyota->info(); the access to the info method of the car class

Stay tuned for the next article on the OOP Series: Constructor and Destructor

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!