If you are a complete beginner, you probably for the first time hear about “Object Oriented Programming and Class.” Let us give a brief introduction to Object Oriented Programming (OOP).
10.1 – Object Oriented Methodology
It is based on real world programming. An object is a representation of a real world entity. If there is an object, there must be a class or blueprint behind it. In that class the behaviour of that object is designed or described in detail. These details consist of all the properties and actions that the object performs. There could be many types of objects coming from different classes and they might have relationships. It could be very complicated but you can always break those objects from one another and make some changes. The advantage of Object Orientation is that when you work on a part of a big, complicated project the other part remains unaffected. Our goal is simple. We want to join different objects to create big, complicated software. At the same time we want to make the relations of those objects as loose as possible.
A car object is built of many other objects like tyre, wheel, engine, accelerator etcetera. If you get a flat tyre does the engine stop? It is interrelated and depends on one another. But finally you can work on them individually without affecting other. That is Object Orientation.
Consider an object ‘GoodHuman’. This object must be different from another object ‘BadHuman’. Both come from ‘Human’ class. Now these two objects might have interrelationships and data interactions. Can you imagine how many kinds of properties and methods are there in ‘Human’ class? It could be very complex. Imagine a situation where a ‘BadHuman’ does something ugly. At the same time a ‘GoodHuman’ does some good thing. Whoever does whatever thing the life goes on and that is also Object Orientation.
The Foundation of Object Orientation
Object Orientation is a type of methodology used for building software applications. An Object Oriented program consists of classes, objects and methods. The Object Oriented methodology in software development revolves around a single concept called the object. You can develop software by breaking the application into component objects. These objects interact with each other when the whole application is put together. An object is a combination of messages and data. The object receives and sends messages and those messages contain data or information.
You (an object) interact with your Television (another object) via messages sent through a remote controller (another object).
Consider another real world example of a football. A football has a boundary. It has a specific defined property like bouncing. You can direct or apply few specific actions by kicking it or throwing it.
An object has a state. It may display behaviour. It has a unique ID.
The difference between an object and class is subtle but important. Whereas a class is an abstract concept, an object is a concrete entity. From a class objects with specific properties can be created or instantiated. That is why an object is often called an instance of a class.
One of the major feature of Object Oriented Programming is ‘Polymorphism’. Polymorphism is the capability of something to assume different forms. In Object Oriented Programming polymorphism is the property that a message can mean different things depending on the objects receiving it. The message ‘Accelerate’ means one thing if it sent to an object “OldCar”. But it means different thing to if it is sent to the object “NewCar”. It is a natural concept that can be applied to objects. It also means that similar objects often accept the same message but do different things.
Consider a web page. It is an object. There are billions of such objects around us. When you send a request to an object like web page you actually apply a verb “GET” to a noun “WebPage”. Now every “WebPage” object does not behave the same way when the “GET” verb is applied. Someone open up a PDF file, someone simply shows some texts and pictures and someone may harm your computer. When you double-click a file. It may execute if it is an executable file. Or it may opens up in a text editor if it is a text file. The message is same. That is “Double Click”. But the behaviour displayed by the file object depends on the object itself.
This is Polymorphism. You will learn it heart as you progress through this chapter.
The advantage of Python classes is that it provides all the standard features of Object Oriented Programming. It has the class inheritance mechanism. That allows multiple base classes, a derived class can override any methods of its base class or classes, and a method can call the method of a base class with the same name. Objects can contain arbitrary amounts and kinds of data.
Finally remember, in Python everything is an object. It means there is an abstraction or encapsulation behind it. Your first job is understand that avstraction and the final job is create your own abstraction.