{"id":9,"date":"2018-12-26T06:14:00","date_gmt":"2018-12-26T06:14:00","guid":{"rendered":""},"modified":"2020-07-25T21:38:33","modified_gmt":"2020-07-25T19:38:33","slug":"object-oriented-programming-with-python","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/object-oriented-programming-with-python\/","title":{"rendered":"Object Oriented Programming With Python"},"content":{"rendered":"<p>In this lesson, we would cover the following:<\/p>\n<ol>\n<li><a href=\"https:\/\/kindsonthegenius.com\/blog\/object-oriented-programming-with-python#t1\">Object Oriented Programming Terms<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/blog\/object-oriented-programming-with-python#t2\">Creating a Class in Python<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/blog\/object-oriented-programming-with-python#t3\">Inheritance: Creating a Subclass in Python<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/blog\/object-oriented-programming-with-python#t4\">Instantiating a Class<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<div style=\"clear: both; text-align: center;\"><a style=\"margin-left: 1em; margin-right: 1em;\" href=\"https:\/\/1.bp.blogspot.com\/-pCf9dtliR1U\/XCMbdhKxT5I\/AAAAAAAACgo\/pQ9u9Dzqvj8KKjHrvAPEj6OaKfM9qmO0gCLcBGAs\/s1600\/Inheritance%2Bin%2BPython-blog.jpg\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/1.bp.blogspot.com\/-pCf9dtliR1U\/XCMbdhKxT5I\/AAAAAAAACgo\/pQ9u9Dzqvj8KKjHrvAPEj6OaKfM9qmO0gCLcBGAs\/s320\/Inheritance%2Bin%2BPython-blog.jpg\" width=\"320\" height=\"169\" border=\"0\" data-original-height=\"502\" data-original-width=\"945\" \/><\/a><\/div>\n<div><\/div>\n<p>&nbsp;<\/p>\n<h4><b id=\"t1\">1. Object Oriented Programming Terms<\/b><\/h4>\n<ul>\n<li><b>Class:<\/b> A class is a user-defined type that could be used to model object in real world. A class defines the attributes and methods of an object. It serves as the blueprint from which objects are created<\/li>\n<li><b>Class Variable:<\/b> This is a variable that belongs to the class. This variable is used by all the objects created from that class<\/li>\n<li><b>Instance Variable: <\/b>This is a variable that belongs to one instance of the class. It belongs to the object created from the class and so is also called object variable<\/li>\n<li><b>Inheritance: <\/b>A feature that allows a class to inherit the features (variables and methods) of another class<\/li>\n<li><b>Overloading<\/b>: A feature that allows two or more functions to have the same name but behave differently depending on the parameters.<\/li>\n<li><b>Instantiation:<\/b> Creating an object from a class<\/li>\n<li><b>Derived Class<\/b> (Sub-class or child class): A class that inherits from another class.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h4><b id=\"t2\">2. Creating a Class in Python<\/b><\/h4>\n<p>We create a class in python using the class keyword. Inside the class, we may define a constructor using the <i>def __init__<\/i> syntax.<br \/>\nAn example of a class definition to model an Employee is given in Listing 1.0.<\/p>\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #888888;\"># The employee class in python<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Employee<\/span>:\r\n    employeeCount <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0\r\n<\/span><span style=\"color: #888888;\"># This is a constructor (initializer)<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">__init__<\/span>(<span style=\"color: #007020;\">self<\/span>, firstname, lastname, department, salary):\r\n<span style=\"color: #007020;\">self<\/span><span style=\"color: #333333;\">.<\/span>firstname <span style=\"color: #333333;\">=<\/span> firstname\r\n<span style=\"color: #007020;\">self<\/span><span style=\"color: #333333;\">.<\/span>lastname <span style=\"color: #333333;\">=<\/span> lastname\r\n<span style=\"color: #007020;\">self<\/span><span style=\"color: #333333;\">.<\/span>department <span style=\"color: #333333;\">=<\/span> department\r\n<span style=\"color: #007020;\">self<\/span><span style=\"color: #333333;\">.<\/span>salary <span style=\"color: #333333;\">=<\/span> salary\r\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">display<\/span>(<span style=\"color: #007020;\">self<\/span>):\r\n<span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"Name: \"<\/span> <span style=\"color: #333333;\">+<\/span> <span style=\"color: #007020;\">self<\/span><span style=\"color: #333333;\">.<\/span>firstname <span style=\"color: #333333;\">+<\/span> <span style=\"background-color: #fff0f0;\">\", \"<\/span> <span style=\"color: #333333;\">+<\/span> <span style=\"color: #007020;\">self<\/span><span style=\"color: #333333;\">.<\/span>lastname)<\/pre>\n<p>Listing 1.0: Employee class in Python<\/p>\n<p>&nbsp;<\/p>\n<h4><b id=\"t3\">3. Inheritance: Creating a Subclass<\/b><\/h4>\n<p>Let&#8217;s say in a company there are different categories of employees. For example Driver. A driver is also an employee and has all the attributes of an employee.<br \/>\nTo create a driver class we can simply inherit the features from the employee class by creating a sub class(derived class or child class). In python, this is achieved by placing the parent class inside parenthesis<br \/>\nHow to do this is given in Listing 1.1<\/p>\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Driver<\/span>(Employee):\r\n    car <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\" \"\r\n<\/span><span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">display<\/span>(<span style=\"color: #007020;\">self<\/span>):\r\n<span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"Name: \"<\/span> <span style=\"color: #333333;\">+<\/span> <span style=\"color: #007020;\">self<\/span><span style=\"color: #333333;\">.<\/span>firstname <span style=\"color: #333333;\">+<\/span> <span style=\"background-color: #fff0f0;\">\", \"<\/span> <span style=\"color: #333333;\">+<\/span> <span style=\"color: #007020;\">self<\/span><span style=\"color: #333333;\">.<\/span>lastname)\r\n<span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"Assigned car: \"<\/span> <span style=\"color: #333333;\">+<\/span> <span style=\"color: #007020;\">self<\/span><span style=\"color: #333333;\">.<\/span>car)<\/pre>\n<p>Listing 1.1: Driver class derives from Employee class<\/p>\n<p>&nbsp;<\/p>\n<p>Notice that listing 1.1 also includes a definition of the display method exactly with the same name as in the Employee class. This is called method overriding. This is because the method in the subclass overrides the method in the superclass.<\/p>\n<p>&nbsp;<\/p>\n<h4><b id=\"t4\">4. Instantiating a Class<\/b><\/h4>\n<p>To instantiate a class(create an object of a class), we simply assign the name of the class to a variable and provided the necessary attributes of the object.<br \/>\nIn Listing 1.2 we instantiate both the Driver and the Employee class<\/p>\n<pre style=\"line-height: 125%; margin: 0;\">employee1 <span style=\"color: #333333;\">=<\/span> Employee(<span style=\"background-color: #fff0f0;\">\"Osondu\"<\/span>, <span style=\"background-color: #fff0f0;\">\"Munonye\"<\/span>,<span style=\"background-color: #fff0f0;\">\"Admin\"<\/span>,<span style=\"background-color: #fff0f0;\">\"5000\"<\/span>)\r\nemployee2 <span style=\"color: #333333;\">=<\/span> Employee(<span style=\"background-color: #fff0f0;\">\"Adaku\"<\/span>, <span style=\"background-color: #fff0f0;\">\"Okeke\"<\/span>, <span style=\"background-color: #fff0f0;\">\"Human Resource\"<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">5400<\/span>)\r\ndriver1 <span style=\"color: #333333;\">=<\/span> Employee(<span style=\"background-color: #fff0f0;\">\"Jackson\"<\/span>, <span style=\"background-color: #fff0f0;\">\"Bills\"<\/span>,<span style=\"background-color: #fff0f0;\">\"Assets\"<\/span>,<span style=\"color: #0000dd; font-weight: bold;\">4400<\/span>)\r\ndriver2 <span style=\"color: #333333;\">=<\/span> Employee(<span style=\"background-color: #fff0f0;\">\"Okereke\"<\/span>, <span style=\"background-color: #fff0f0;\">\"Joseph\"<\/span>, <span style=\"background-color: #fff0f0;\">\"Maintenance\"<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">3300<\/span>)<\/pre>\n<p>Listing 1.2: Instantiating classes<\/p>\n<p>&nbsp;<\/p>\n<p>In the next tutorials, we would examine how to access attributes of a class<\/p>\n<p>Object Oriented Programming &#8211; Video Lessons<br \/>\n<a href=\"https:\/\/www.youtube.com\/watch?v=ElTmwzH2res\" target=\"_blank\" rel=\"noopener\">How to Create a Class in Python<\/a><br \/>\n<a href=\"https:\/\/www.youtube.com\/watch?v=oUwkf0obbUM\" target=\"_blank\" rel=\"noopener\">Instantiating a Class in Python<\/a><br \/>\n<a href=\"https:\/\/www.youtube.com\/watch?v=YGZgdrU3EsE\" target=\"_blank\" rel=\"noopener\">Accessing Attributes of Objects<\/a><br \/>\n<a href=\"https:\/\/www.youtube.com\/watch?v=RNp-xDplTjU\" target=\"_blank\" rel=\"noopener\">Inbuilt Attribute and How to Create Subclasses<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this lesson, we would cover the following: Object Oriented Programming Terms Creating a Class in Python Inheritance: Creating a Subclass in Python Instantiating a &hellip; <\/p>\n","protected":false},"author":2,"featured_media":280,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/9"}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=9"}],"version-history":[{"count":14,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/9\/revisions"}],"predecessor-version":[{"id":1066,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/9\/revisions\/1066"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media\/280"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=9"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=9"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=9"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}