Table of Contents
Introduction Prerequisites to know Structure or building blocks Methods Principles Abstraction Benefits of abstraction Inheritance Polymorphism Other principles Object oriented programming languages Advantages of OOP ConclusionWhat is Object Oriented Programming (OOP)
Object oriented programming is the structure and style of writing the code, which focuses more on the elements that are objects and not on their functions. In this blog, we will discuss object-oriented programming, prerequisites you should be aware of before diving deep into oops, building blocks of oops, principles, examples, some of the languages supporting oops structure, and advantages and limitations of object oriented programming in Python.
Introduction
Object-oriented programming is the computer programming model followed for the organization of all the structure and design of a program of software that revolves around its data. The main focus is making the data the center point of consideration instead of logic and functions.
Objects in Object oriented programming in Python is considered to be data field containing some individualistic behavior and attributes. The intention of OOP lies in binding together the functions and data, which results in accessing the particular data by that specific function only.
Prerequisites to know
Before you dive deep into the concepts of object oriented programming it is very much needed to be thorough with the below concepts which involve declaration and message passing of methods.
- Access modifiers – These are responsible for deciding from where the methods can be accessed in the program and can have one of the four values – public, protected, private, and default.
- Return type – This specifies the data type of the returned value from the particular method.
- Method name – Certain rules are followed as a convention to give names of methods that are similar to the naming convention of fields with certain exceptions.
- List of parameters – You must specify a comma-separated list of specified parameters along with their data types. All of the parameters should be enclosed inside the parenthesis.
- List of exceptions – This consist of a comma-separated list of all the possible exceptions that the method might throw while running.
- Body of method – It consists of the logical part consisting of the block of code that executes to perform intended functionality.
Structure or building blocks
Four essential blocks of oops help build its structure: classes, objects, attributes, and methods.
Classes – The class can be defined as the blueprint or template for object generation. We need to define the methods and the attributes(variables) inside the class which the corresponding objects can reference. If we consider the example of a bird, then we have to define the class of bird and all the properties and functions that would be encased for the bird object.
Object – Objects are an instance of the class, consisting of the data fields having a specifically defined structure and data type per class. Whenever a call is made to the object the instance of the class is created immediately. While calling the object, you must pass the required information by the class to create an object. For example, the object of the bird class can be created by passing the color, size, and name of the bird using the below syntax –
parrot = Bird (“green”,”4-40 inches”, “parrot”)
Further, you can give the call to the fly method of Birds class using the parrot object and observe its execution by calling it using the below syntax –
parrot.fly()
Attributes – These are also called variables, fields, or properties that tell us about a particular object’s characteristics. The attributes of class and the instance of object help define the differences between multiple objects. Let us consider an example to see how the attributes of bird class are defined –
Def init(self, color, size, name):
self.color = color
self.size = size
self.name = name
Three attributes are defined in the bird class: color, name, and size.
Methods
Methods inside the class help define different functions that can be called, usually using the object of the class to execute. Three types of methods can be defined, which are as below
Class methods – This type of method is usually created to modify and manipulate the state of the class and allow only class variables to be accessed.
Instance Methods – We can only go for manipulating and access the attributes of the object when we make use of instance methods. Instance methods are methods where the use of instance variables is made inside them.
Static Methods – If we want to carry out a particular task in isolation, then static methods are used as a general utility. However, as the class attributes are not accessible to the static methods, we cannot use the class or instance variables inside them.
Principles
There are four basic principles of object oriented programming, also referred to as the four pillars of oops: abstraction, encapsulation, inheritance, and polymorphism. While doing programming, we often observe that there are two paradigms to programming, which include the main data elements and the other one is operations on that data which in oops, are considered as objects and methods.
Whenever we want to manipulate data objects using the object-oriented programming structure, it becomes effortless to carry any modifications even in the future while programming. Let us discuss the pillars of oops in detail.
Abstraction
The abstraction feature deals with hiding all the details and complexities of the feature implementation and only showing the offered services and features. Let us consider a small analogy to understand it better. When we visit the ATM, we only see the available services of the ATM platform, such as withdrawal, deposit, bank transfer, etc., while the actual implementation of that task such as how a transaction is being carried out is hidden from us.
Benefits of abstraction
Having abstraction proves beneficial in security as only authorized persons can take a look and have access to internal implementation. It also improves the enhancement and manipulation of internal tasks easily without external things getting affected and also improves maintainability. We can use features of the platform without knowing about the nitty-gritty of it.
Encapsulation – Encapsulation refers to binding together the data and its attributes or operations and thus forming a single unit. Encapsulation results in increased data security as all the things related to one task are grouped and allowed access to it as per requirement.
This is possible only because of data hiding. Encapsulation is also referred to as Data hiding and abstraction followed together. In data hiding, the data is kept to have restricted access and safe from the outside world, such as declaring private attributes or methods. We can access the private elements only inside that class and not outside the class.
Inheritance
We can get the same features of a particular class into a new class by using inheritance. The class from which all the features are being acquired is called as the base class, parent class, or superclass while the class which is acquiring the features is the derived class, child class, or subclass.
We can also add new features and redefine the existing features in the child class apart from having the parent class’s features in it. For this, the child class needs to inherit the parent class.
There are different types of inheritance which again depend on the supported types by the programming language being used. Some of the common inheritance types include –
- Single inheritance – When there is one base class and one derived class of it.
- Multiple inheritances – When a single derived class is inheriting from two or more base classes.
- Multilevel inheritance – It involves multiple levels of inheritance mechanism such as Class car to Class SUV to Class Tata_nexon
- Hierarchical inheritance – When multiple derived classes are being derived from the single base class.
Polymorphism
The word poly means multiple and morph means forms which indicates that polymorphism stands for the same element with different forms. When a method or an object in object oriented programming has multiple names associated with them then it is termed polymorphism.
Polymorphism in objects is found when the object has a reference to the parent object. For methods polymorphism can occur in two types – static binding also referred to as Compile time polymorphism which is achieved by method overloading and dynamic binding which stands for run time polymorphism that comes through method overriding.
Other principles
Other principles of oops include Coupling, cohesion, Composition, Aggregation, and Association which makes the structure even easier, simple, segregated yet connected.
Object oriented programming languages
Here is the list of Object oriented programming languages:
Ruby
Scala
JADE
Emerald
These are some of the pure OOP languages while languages that are designed especially by considering OOPS structure are Java, Python, and C++. Other programming languages that support OOP include PHP, JavaScript, and Visual Basic .Net.
Advantages
Advantages of OOP
- Security
- Productivity
- Flexibility
- Reusability
- Modularity
- Interface description
- Scalable
- Upgradable
Conclusion
In this blog, we learned about the programming paradigm of object-oriented programming and discussed some of the prerequisites needed to understand it. Further, we also learned about the building blocks of OOPS and its principles of OOPS. At last, we looked at some of the programming languages that support OOP implementation and the advantages of Object oriented programming.
For structured learning, Codingal offers a Python for Kids course. Our Python classes are a fun and exciting way to build a foundation of skills that prepares kids for the future.
Our curriculum prepares students with complete mastery of the Python environment and prepares them to advance into high-level programming. Codingal motivates kids to bring their imagination to life by engaging in Python for Kids course activities individually and in groups.
Coding for kids is the 21st-century skill that constitutes an entirely different empowered relationship with technology and supports various skills, such as logical thinking, communication, creativity, and even math proficiency. You can go ahead and try a free class today!