Object Oriented Programming(OOP) Series: Attributes and Methods
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 to writing programs that are structured on data members(attributes) and behaviour (methods) with the creation of objects. A class is basically a blueprint or layout for the creation of objects. These have their own attributes (characteristics), and methods (behaviour). 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 includes: tire, door, seats, license plate, headlight, wheel, handle, make, and year. 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. Its 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 its 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 layout the behavior for objects that will be created. For instance, if a car owner presses the horn button it’ll perform an operation 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 behaviour (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
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.
Attributes and Methods in Dart:
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
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 determined.
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 tune for the next article on the OOP Series: Constructor and Destructor
Thank you for reading this article. Please feel free to use the comment section for questions, answers and contributions.
Do you love this article?? please follow me on hashnode or Twitter @alemsbaja to stay updated for more on these OOP series.
Originally published at https://alemsbaja.hashnode.dev.