Skip to content

Ankit-Khule/OOPs-in-Python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 

Repository files navigation

Object oriented programming in pythons

  • One of the ways of solving programming problems is by creating objects.

  • In python objects has two characteristics

    • attributes - features of the objects
    • behaviour - functions of the objects
  • Object consist of Class. Class is a blueprint of the object defined. class

  • Here we have created class animal.
  • animalcount is a class variable. It can be used inside and outside the class as an attribute
  • It consist of instance attributes, means which are created on the go and not predefined.
  • A function sound is defined under the class,which is method used by the object or behaviour of the object.
  • __init__ is a constructor to create the instances for the particular class

Built in class attributes

  • dict − Dictionary which contains the class's namespace.
  • doc − Documentation string inside the class. if undefined it returns none.
  • name − Retuns Class name.
  • module − Returns Module name in which the class is defined. The attribute is "main" in interactive mode.
  • bases − Retuns the base classes, in the order of their occurrence in the base class list.

builtin

Inheritance

  • Inhertance is acquiring attributes and methods from another class.

  • class subclass(parent class) -->all the attributes of the parent class is inherited by the subclass. Inheritance

  • if child class doesn't have a __init__ of its own it will inherit it from parent. if the constructor is present it will overide the constructor of parent class Inheritanceinit

  • super() --> the function is used to inherit the attributes and methods from parent class even if subclass consist of init method to overide it. super

Types of Inheritance

  • Single Inheritance - One parent --> one child Single

  • Multilevel Inheritance - Parent--> sub Parent-->child Multilevel

  • Multiple Inheritance - Two parents --> one child Multiple

  • Hierarchical Inheritance - One parent--> two child hierarchical

  • Hybrid Inheritance - two or more types of inheritance. Hybrid

Encapsulation

  • Protecting attributes and methods inside the class to be used outside the class Encapsulation

Here you can see if i am directly calling the parent attribute it is not accessible. It throws an attribute error. But when i call it from the child class which consist of parent class method. the value is accesible.

Method Overriding

  • Creating similar method in the child class with respect to parent class, overrides the method of parent class. Means it will replace the method of parent class and execute its own method Method