Fundamental Concepts of Object Oriented Programming

Greg's Study Journal
24 followers
23 Video Views·Nov 8, 2022
Fundamental Concepts of Object Oriented Programming
The four fundamental concepts of object-oriented programming
are abstraction, encapsulation, inheritance, and polymorphism.
Before considering what they mean, first consider what is an object.
An object is a thing from the real world.
It could be a car, a boat, or a book.
But it might not be something physical that you can touch.
It could be a dental appointment, a seat reservation for the cinema, or a bank account.
In the realm of object-oriented programming,
an object is anything of interest to the software application that you're building.
It's anything that you want to store and process data about.
Another name for an object is an entity.
This leads us to the first fundamental concept of object-oriented programming,
namely abstraction.
Abstraction means to simplify reality.
For example, a person is an object.
But if you were designing a new application to process data about a person,
it's unlikely that you'd be interested in everything there is to know about a person.
Rather, you would only concern yourself with the data that are relevant
and the tasks that you want to perform with those data.
To create objects programmatically, you need a class.
A class is a template for creating objects.
A class is code written by a programmer to define the attributes and the operations of an object.
Attributes describe the object.
They're sometimes referred to as fields because they contain data.
Most programmers know them as properties.
Properties are coded within the class either as public variables or as property procedures.
Operations are actions that can be done to or performed by the object.
They're sometimes referred to as behaviors.
But more commonly, they're known as methods.
Methods are programs within the class that are coded either as procedures or functions.
A class is a template for creating objects.
And it's often compared with a pastry cutter
because once it's been written, it can be used to create many objects of the same type.
In fact, a class is sometimes referred to as a type.
Each object is an instance of a class in the computer's memory.
Creating an object from a class is therefore known as instantiation.
Once these objects have been created, their properties can be assigned values,
making each object of the same type a unique entity.
Each property is defined in the class by a property procedure
which may include code to validate a property value while it's being assigned.
This helps to ensure the integrity of the data contained within the object.
The property values that have been assigned to an object
are collectively known as the state of the object.
It's also possible to assign values to properties while an object is being instantiated
by means of a special method called new.
This method is known as the constructor.
The second fundamental concept of object-oriented programming is encapsulation.
This means to hide the complexity of the inner workings of an object
from the programs and the programmers that make use of it.
It's often referred to as information hiding
because the data contained within an object
and the functions that manipulate the data are bound together
and therefore safe from outside interference.
In some big software development projects,
it's common for more experienced programmers
to write the classes that will be used by the junior programmers.
A class might be made available in the form of a class library.
Indeed, some software development companies
specialize in developing new classes to be used by other software developers.
Compiled class libraries protect their intellectual property.
To write code that will create an object from a class,
then set its properties and call its methods,
it's not necessary to understand the inner workings of the class.
All a programmer needs to know is the name of the class,
the properties and methods available,
and any data that need to be supplied when they're called.
In other words, all our programmer really needs to know about
is the interface of the class.
The implementation code of those properties and methods can remain a mystery.
This greatly simplifies the use of objects
and helps to ensure that the data and operations encapsulated within are safe.
The third fundamental concept of object-oriented programming is inheritance.
This means that a class can derive its methods and properties from another class.
Inheritance can result in a hierarchy of classes.
For example, this person class defines the methods and properties of a person object.
An employee in a business is also a person.
So, through inheritance, an employee class
derives the methods and properties of the person class.
An employee is a type of person.
A customer of the business is also a person.
So, through inheritance, a customer class
also derives the methods and properties of the person class.
A customer is a type of person.
An employee class can have some extra properties and methods of its own.
It can extend the person class.
And so can a customer.
But it need not stop there.
A programmer is a type of employee.
And so is a manager and a cleaner.
Inheritance defines type of relationships.
The class at the start of the inheritance hierarchy is called the base class.
Any class that derives from another class is called a subclass.
Any class that is derived from is called a superclass.
The final fundamental concept of object-oriented programming is polymorphism.
Polymorphism means that a class can implement an inherited method in its own way.
The person class at the base of this hierarchy has a method
which will save details of any object created from the person class.
Perhaps to a database.
Thanks to inheritance, all of the classes in this hierarchy do exactly the same thing.
But it may be necessary for a customer's details to be saved differently.
Perhaps to a different database.
Polymorphism allows for this.
The customer class can override the workings of any method or property
that it inherits with a new version of its own.
Different forms of the same type of object with the same interface can behave in different ways.
Polymorphism literally means many forms.
To recap the fundamental concepts of object-oriented programming.
Abstraction means to simplify reality
and focus only on the data and processes that are relevant to the application being built.
Encapsulation means that data and the programs that manipulate those data
are bound together and their complexity is hidden.
Inheritance means that a class can derive its methods and properties from another class.
This might result in an extensive hierarchy of superclasses and subclasses.
Polymorphism means that different subclasses of the same superclass
which therefore share the same interface can implement those interfaces in their own ways
by overriding the code of the methods they inherit.
