Concepts in Object Orientated Programming

in #programming5 years ago

The ideas within object orientated programme can be split into 4 sections - Encapsulation, Abstraction, Inheritance and Polymorphism.

The “object” can be thought of as a self contained piece of code that has its own properties and methods. A function is a structure that contains code that will take in arguments in order to complete a task and can return a value. A method is like a function that is associated to an object.

Object orientated programming aims to contain pieces of code that carry out different tasks and keep them separate from other objects within the programme. If the object is required in the programme, it can be called and used.

Encapsulation

This is achieved when an object remains private within a programme. It is self contained, possibly within another file and other objects cannot access it. In order to work with it, methods can be called that have been defined and written within the object itself.

An object can have private variables that only exist within it. They can take in arguments like you would use in the case of a function. Methods that work with the object are typically public and can be utilised in various ways.

Abstraction

This means to hide the inner workings of the object, and to provide the minimum that is needed to use it. A user may not need to know the workings of the object, so it is not provided.

An example could be seen in a coffee machine, you do not need to know how it makes coffee or the process it will go through to the final output. The user only needs to know how to choose what they want.

Inheritance

The process where objects can be reused, in the case of child objects attributes can be added to them to modify it. Code is reused where common themes appear, negating the need to rewrite the same code for situations that may be similar.

For example a ‘person’ class may have some properties and methods - a teacher and a student will share these attributes but have their own, so why make new objects for them when you can inherit the original class and make some changes.