The above add function will always take 2 numbers as input, but what if you want to add 3 numbers at once or maybe 4 numbers. This is capable of executing function calls like: > add(4,5) In python we will define it as, def add(a, b): For example, consider a function add(), which adds all its parameters and returns the result. All we have to do is, create different functions with same name having different parameters. In OOP, it is possible to make a function act differently using function overloading. Polymorphism can be achieved in tow different forms, they are: Function Overloading Precisely, Polymorphism is the property of any function or operator that can behave differently depending upon the input that they are fed with. Polymorphism, or Poly + Morph, means "many formsb. We will be covering these in details in inheritance tutorial. Python supports Simple, Multiple and MultiLevel Inheritance. ![]() InheritanceĪs explained in the previous tutorial, inheritance is about defining a set of core properties and functions in one place and then re-using them by inheriting the class in which they are defined. Here we try to encapsulate the data and functions together which belongs to the same class. Much as it may sound like a capsule, it is pretty much the same. Since the beginning we have been talking about objects, its data, functions, privacy etc., now it's time to know how is all this kept bounded. EncapsulationĮncapsulation, is one of the core reason for the existence of an object. The user needs to focus on what a class does rather than how it does. ![]() A class encapsulates the relevant data and functions that operate on data by hiding the complex implementation details from the user. Abstraction of DataĬlasses use the concept of abstraction. Protected data or functions, more or less act like public but should not be accessed from outside. Private data or function are the ones that cannot be accessed or seen from outside the class whereas, public data or functions can be accessed from anywhere. In OOP, data inside the classes can be defined as public, private or protected. Also, hiding, or setting up privacy levels, can be done for functions as well. The reason behind doing this is to create several levels of accessing an object's data and prevent it from accidental modification. Now that all the basic features are defined in our class Car, we can create its objects by setting values for properties name, model etc, and the object of the class Car will be able to use the functions defined in it.ĭata hiding helps us to define the privacy of data from the outside world or to be precise, from other classes. In class, both data, and the functions that will be operating on that data are bundled as a unit.įor example, let's say there is a class named Car, which has some basic data like - name, model, brand, date of manufacture, engine etc, and some functions like turn the engine on, apply brakes, accelerate, change gear, blow horn etc. An object is the fundamental concept of OOP but classes provide an ability to define similar type of objects. For example, if Mahatma Gandhi, Sachin Tendulkar, you and me are objects, then Human Being is a class. ![]() Taking the example forward, like a student can read/write, the car can accelerate and so on.Ī class is a blueprint where attributes and behaviour is defined. In OOP it is possible to assign some functions to objects of a class.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |