Classes and objects,Object oriented programming terminology & Function Overloading
1. What is a Class?
A class is like a blueprint or template for creating objects. It defines the attributes (data) and methods (functions) that the objects created from the class will have.
Think of a class as a recipe for creating objects, and objects are the instances of that class.
Creating a Class:
In Python, you define a class using the class keyword, followed by the class name. Conventionally, class names are written in CamelCase (each word starts with a capital letter).
2. Creating Instance Objects
An object is an instance of a class. When you create an object, you're making a specific instance of the class with particular values for the attributes.
Creating an Object:
Here, my_dog is an object (an instance) of the Dog class. When we create my_dog, Python runs the __init__() method to initialize the name and age attributes.
3. Accessing Attributes and Methods
To access the attributes or methods of an object, we use the dot (.) notation:
- Accessing Attributes:
my_dog.name,my_dog.age - Calling Methods:
my_dog.display_info()
Object-Oriented Programming Terminology
4. Inheritance:
Inheritance is a fundamental concept in OOP. It allows one class (child class) to inherit the attributes and methods of another class (parent class). This promotes code reuse and allows the child class to extend or modify the behavior of the parent class.
Think of inheritance as creating a new type that is a specialized version of an existing type.
Creating a Parent and Child Class (Inheritance Example):
In this example:
Doginherits from theAnimalclass.- The
Dogclass inherits thespeciesattribute and thespeak()method fromAnimal. - We override the
speak()method in theDogclass to give it specific behavior for dogs (making the dog bark, "Woof!").
Creating Objects from the Child Class:
- The
Dogobject inherits thespeciesattribute fromAnimaland can call thespeak()method, but it also has its ownspeak()method that overrides the parent class's version.
5. Method Overriding:
Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its superclass. In our previous example, we saw method overriding in action when the Dog class provided its own version of the speak() method.
- Why is it useful?
- Overriding allows a subclass to provide a more specific behavior while still retaining the structure of the method in the parent class.
Example of Method Overriding:
Here, Dog provides a more specific sound() method than Animal, overriding the method.
6. Data Hiding (Encapsulation):
Data Hiding (or Encapsulation) is an important concept where we restrict access to certain details of an object and allow access only through specific methods (getters and setters). This helps prevent direct access to an object's internal state, which improves security and code maintainability.
In Python, we can hide attributes by using underscores. A single underscore (_) indicates that an attribute is protected (intended for internal use by the class or its subclasses), and a double underscore (__) makes the attribute private (more hidden).
Example of Data Hiding (Encapsulation):
__ageis a private attribute, so we cannot access it directly.- We use getter (
get_age()) and setter (set_age()) methods to interact with__age. - By using getter and setter methods, we can control how the attribute is accessed and modified, adding a layer of security to the class.
7. Function Overloading (Method Overloading):
Function overloading or method overloading refers to defining multiple methods with the same name but different parameter lists (number or type of parameters). However, Python does not support function overloading in the traditional sense like languages such as Java or C++.
In Python, we can achieve similar behavior by using default arguments or variable-length arguments (e.g., *args, **kwargs).
Example of Function Overloading (Using Default Arguments):
- If only one argument is passed to
add(), the second argument (b) defaults to0. - This mimics function overloading, where different behavior occurs based on the number of arguments passed.
Summary of Key Concepts:
Classes and Objects:
- Class: A blueprint for creating objects.
- Object: An instance of a class.
- We define methods (functions) inside classes to define behaviors.
Inheritance:
- A subclass inherits attributes and methods from a superclass.
- It can also override methods for specialized behavior.
Method Overriding:
- Subclass provides its own implementation of a method already defined in the superclass.
Data Hiding (Encapsulation):
- Restricting access to an object's internal state.
- Private and protected attributes are used to control access, usually with getters and setters.
Function Overloading:
- Python doesn't support traditional function overloading, but we can use default parameters or
*args/**kwargsto handle different numbers of arguments.
Example: A Simple Dog Class
In this example, we will create a simple Dog class that allows us to:
- Define a dog's name and age when we create a dog.
- Access the dog's information.
- Call a method to make the dog "speak" (i.e., print a message).
Explanation:
Class
Dog:- We define a class
Dogwith two attributes:nameandage. - The
__init__()method is the constructor, which is called when a new dog is created. It sets thenameandagefor each dog. - The
speak()method is used to make the dog say "Woof!" when called. - The
display_info()method shows the name and age of the dog.
- We define a class
Creating an Object:
- We create a
Dogobject calledmy_dogwith the name "Buddy" and age 3 by callingDog("Buddy", 3). - We then call
my_dog.display_info()to print Buddy's name and age, andmy_dog.speak()to make Buddy bark.
- We create a
Creating Another Object:
- We create another dog named "Max" with the age 5 and use the same methods to display information and make him speak.
Output:
Key Concepts Covered:
- Class: The
Dogclass is a blueprint for creating dog objects. - Object:
my_dogandanother_dogare objects (instances) of theDogclass. - Attributes:
nameandageare attributes that eachDogobject has. - Methods:
speak()anddisplay_info()are methods that perform actions using the object's data.
