Oop in
Oop in
An object is a real word entity that contains data and code. It allows binding data and code
together.
The aim of OOP is to implement real-world entities like inheritance, hiding, polymorphism in
programming. The main purpose of OOP is to bind together the data and the functions that
operate on them so that no other part of the code can access this data except that function
Inheritance
Encapsulation
Polymorphism
Data Abstraction
OOPS ,helps in writing a complex piece of code easily, but it also allows users to handle
and maintain them easily as well. Not only that, the main pillar of OOPs - Data Abstraction,
Encapsulation, Inheritance, and Polymorphism, makes it easy for programmers to solve complex
scenarios. As a result of these, OOPs is so popular.
Advantages of OOP
Disadvantages of OOP
The programming language is called pure object-oriented language that treats everything
inside the program as an object. The primitive types are not supported by the pure OOPs
language. There are some other features that must satisfy by a pure object-oriented
language:
Encapsulation
Inheritance
Polymorphism
Abstraction
All predefined types are objects
All user-defined types are objects
All operations performed on objects must be only through methods exposed to the objects.
Object: An object is a real-world entity that has attributes, behavior, and properties. It
is referred to as an instance of the class. It contains member functions, variables that we
have defined in the class. It occupies space in the memory. Different objects have different
states or attributes, and behaviors.
The method is a function that is associated with an object. In Python, a method is not
unique to class instances. Any object type can have methods.
Inheritance
Inheritance is the capability of one class to derive or inherit the properties from another
class. The class that derives properties is called the derived class or child class and the
class from which the properties are being derived is called the base class or parent class
Types of Inheritance –
Single Inheritance:
Single-level inheritance enables a derived class to inherit characteristics from a
single-parent class.
Multilevel Inheritance:
Multi-level inheritance enables a derived class to inherit properties from an immediate
parent class which in turn inherits properties from his parent class.
Hierarchical Inheritance:
Hierarchical level inheritance enables more than one derived class to inherit properties
from a parent class.
Multiple Inheritance:
Multiple level inheritance enables one derived class to inherit properties from more than
one base class.
Hybrid inheritance: This form combines more than one form of inheritance. Basically, it is a
blend of more than one type of inheritance.
There are 2 built-in functions in Python that are related to inheritance. They are:
2. issubclass(): It checks whether a specific class is the child class of another class or
not. Its syntax is:
issubclass(childclass_name, parentclass_name)
It would return True if the entered child class is actually derived from the entered parent
class else, it returns False.
Overriding a method means redefining a method in the subclass when it has already been
defined in some other class.
A method in the subclass would be called as overridden only when there exists another method
with the same name and same set of parameters in the superclass.
A superclass or base class is a class that acts as a parent to some other class or classes.
Encapsulation
Encapsulation refers to binding the data and the code that works on that together in a
single unit.
Access specifiers or access modifiers are keywords that determine the accessibility of
methods, classes, etc in OOPs. These access specifiers allow the implementation of
encapsulation. The most common access specifiers are public, private and protected.
Polymorphism
The word polymorphism means having many forms. In programming, polymorphism means the same
function name (but different signatures) being used for different types.
By polymorphism, we understand that one task can be performed in different ways.
Static polymorphism (static binding) is a kind of polymorphism that occurs at compile time.
An example of compile-time polymorphism is method overloading.
Method overloading is a feature of OOPs which makes it possible to give the same name to
more than one methods within a class if the arguments passed differ.
Method overriding is a feature of OOPs by which the child class or the subclass can redefine
methods present in the base class or parent class. Here, the method that is overridden has
the same name as well as the signature meaning the arguments passed and the return type.
Operator overloading refers to implementing operators using user-defined types based on the
arguments passed along with it.
Data abstraction
Abstraction is used to hide internal details and show only functionalities.
Abstract class
Abstract method
An abstract class is a class that consists of abstract methods. These methods are basically
declared but not defined. If these methods are to be used in some subclass, they need to be
exclusively defined in the subclass.
Advantages Of Abstraction
Only you can make changes to your data or function and no one else can.
Makes the application secure by not allowing anyone else to see the background details.
Increases reusability of the code.
Avoids duplication of your code.
Data Abstraction
Hiding the details about the data is called data abstraction.
Control Abstraction
Hiding the details about the implementation is called control abstraction.
A constructor is a special type of method that has the same name as the class and is used to
initialize objects of that class.
delattr(obj, name): If you need to delete a specific attribute in a class, then make use of
this inbuilt function
hasattr(obj, name): Last but not least, if you need to see if a particular object contains
an attribute, then make use of this function. Upon execution, this will return true if an
attribute is present in a function.
__dict__: By using this you can view the dictionary that contains information regarding the
class namespace.
__name__: Use this attribute, if you need to view the name of the current class.
__doc__: This attribute contains a string, which has the documentation for the current
class.
__module__: If you need to access the module in which the class is defined make use of this
inbuilt attribute.
__bases__: If you need to view the tuple which includes all the base classes, then use this
function.
The self-parameter refers to the current instance of the class and accesses the class
variables. We can use anything instead of self, but it must be the first parameter of any
function which belongs to the class.
Manipulators are helping functions. It is used to manipulate or modify the input or output
stream. The modification is possible by using the insertion (<<) and extraction (>>)
operators.
Composition is one of the vital concepts in OOP. It describes a class that references one or
more objects of other classes in instance variables. It allows us to model a has-a
association between objects.
Reference Variable: It is a variable that points to an object of the class. It points to the
location of the object that is stored in the memory.