From 889b572fee2d2a109d158dab6c986a4fe200af17 Mon Sep 17 00:00:00 2001
From: msaif28 <103564702+msaif28@users.noreply.github.com>
Date: Thu, 1 Dec 2022 23:55:28 +0300
Subject: [PATCH] Created using Colaboratory
---
session9_oop_inheritence_polimorism.ipynb | 1495 +++++++++++++++++++++
1 file changed, 1495 insertions(+)
create mode 100644 session9_oop_inheritence_polimorism.ipynb
diff --git a/session9_oop_inheritence_polimorism.ipynb b/session9_oop_inheritence_polimorism.ipynb
new file mode 100644
index 0000000000..a71e575e03
--- /dev/null
+++ b/session9_oop_inheritence_polimorism.ipynb
@@ -0,0 +1,1495 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": [],
+ "include_colab_link": true
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "view-in-github",
+ "colab_type": "text"
+ },
+ "source": [
+ "
"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "### Class Relationships\n",
+ "\n",
+ "- Aggregation\n",
+ "- Inheritance"
+ ],
+ "metadata": {
+ "id": "yqLLqLyj4r0z"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "### Aggregation(Has-A relationship)"
+ ],
+ "metadata": {
+ "id": "f69fS6U45Hqr"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# example\n",
+ "class Customer:\n",
+ "\n",
+ " def __init__(self,name,gender,address):\n",
+ " self.name = name\n",
+ " self.gender = gender\n",
+ " self.address = address\n",
+ "\n",
+ " def print_address(self):\n",
+ " print(self.address._Address__city,self.address.pin,self.address.state)\n",
+ "\n",
+ " def edit_profile(self,new_name,new_city,new_pin,new_state):\n",
+ " self.name = new_name\n",
+ " self.address.edit_address(new_city,new_pin,new_state)\n",
+ "\n",
+ "class Address:\n",
+ "\n",
+ " def __init__(self,city,pin,state):\n",
+ " self.__city = city\n",
+ " self.pin = pin\n",
+ " self.state = state\n",
+ "\n",
+ " def get_city(self):\n",
+ " return self.__city\n",
+ "\n",
+ " def edit_address(self,new_city,new_pin,new_state):\n",
+ " self.__city = new_city\n",
+ " self.pin = new_pin\n",
+ " self.state = new_state\n",
+ "\n",
+ "add1 = Address('gurgaon',122011,'haryana')\n",
+ "cust = Customer('nitish','male',add1)\n",
+ "\n",
+ "cust.print_address()\n",
+ "\n",
+ "cust.edit_profile('ankit','mumbai',111111,'maharastra')\n",
+ "cust.print_address()\n",
+ "# method example\n",
+ "# what about private attribute"
+ ],
+ "metadata": {
+ "id": "5kx2nO_m5GdR",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "25f52d86-29f9-447c-b9a0-7a6995a237a4"
+ },
+ "execution_count": 1,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "gurgaon 122011 haryana\n",
+ "mumbai 111111 maharastra\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "##### Aggregation class diagram"
+ ],
+ "metadata": {
+ "id": "iwHzHwcQ5L94"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "N8leEVkv5PQ1"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "### Inheritance\n",
+ "\n",
+ "- What is inheritance\n",
+ "- Example\n",
+ "- What gets inherited?"
+ ],
+ "metadata": {
+ "id": "ftkzTkLv64OS"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# Inheritance and it's benefits"
+ ],
+ "metadata": {
+ "id": "uSTDcEyh65cu"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# Example\n",
+ "\n",
+ "# parent\n",
+ "class User:\n",
+ "\n",
+ " def __init__(self):\n",
+ " self.name = 'nitish'\n",
+ " self.gender = 'male'\n",
+ "\n",
+ " def login(self):\n",
+ " print('login')\n",
+ "\n",
+ "# child\n",
+ "class Student(User):\n",
+ "\n",
+ " def __init__(self):\n",
+ " self.rollno = 100\n",
+ "\n",
+ " def enroll(self):\n",
+ " print('enroll into the course')\n",
+ "\n",
+ "u = User()\n",
+ "s = Student()\n",
+ "\n",
+ "print(s.name)\n",
+ "s.login()\n",
+ "s.enroll()"
+ ],
+ "metadata": {
+ "id": "k_BCUsIbPHud",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "ace03f99-3335-4da0-9ab6-37d6f980f637"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "nitish\n",
+ "login\n",
+ "enroll into the course\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# Class diagram"
+ ],
+ "metadata": {
+ "id": "Nh2Ug7mZSJMd"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "##### What gets inherited?\n",
+ "\n",
+ "- Constructor\n",
+ "- Non Private Attributes\n",
+ "- Non Private Methods"
+ ],
+ "metadata": {
+ "id": "CJEPRGHGPY9n"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# constructor example\n",
+ "\n",
+ "class Phone:\n",
+ " def __init__(self, price, brand, camera):\n",
+ " print (\"Inside phone constructor\")\n",
+ " self.price = price\n",
+ " self.brand = brand\n",
+ " self.camera = camera\n",
+ "\n",
+ " def buy(self):\n",
+ " print (\"Buying a phone\")\n",
+ "\n",
+ "class SmartPhone(Phone):\n",
+ " pass\n",
+ "\n",
+ "s=SmartPhone(20000, \"Apple\", 13)\n",
+ "s.buy()"
+ ],
+ "metadata": {
+ "id": "VGpL2aWUPX-C",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "5114dda8-4e7a-45ea-fd82-ce6e968cba82"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Inside phone constructor\n",
+ "Buying a phone\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# constructor example 2\n",
+ "\n",
+ "class Phone:\n",
+ " def __init__(self, price, brand, camera):\n",
+ " print (\"Inside phone constructor\")\n",
+ " self.__price = price\n",
+ " self.brand = brand\n",
+ " self.camera = camera\n",
+ "\n",
+ "class SmartPhone(Phone):\n",
+ " def __init__(self, os, ram):\n",
+ " self.os = os\n",
+ " self.ram = ram\n",
+ " print (\"Inside SmartPhone constructor\")\n",
+ "\n",
+ "s=SmartPhone(\"Android\", 2)\n",
+ "s.brand"
+ ],
+ "metadata": {
+ "id": "PMjCWA3wP_s-",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 223
+ },
+ "outputId": "9cf6eca4-444a-48ee-9a55-82eeca73e4e2"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Inside SmartPhone constructor\n"
+ ]
+ },
+ {
+ "output_type": "error",
+ "ename": "AttributeError",
+ "evalue": "ignored",
+ "traceback": [
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
+ "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
+ "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mSmartPhone\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Android\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 17\u001b[0;31m \u001b[0ms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbrand\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
+ "\u001b[0;31mAttributeError\u001b[0m: 'SmartPhone' object has no attribute 'brand'"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# child can't access private members of the class\n",
+ "\n",
+ "class Phone:\n",
+ " def __init__(self, price, brand, camera):\n",
+ " print (\"Inside phone constructor\")\n",
+ " self.__price = price\n",
+ " self.brand = brand\n",
+ " self.camera = camera\n",
+ "\n",
+ " #getter\n",
+ " def show(self):\n",
+ " print (self.__price)\n",
+ "\n",
+ "class SmartPhone(Phone):\n",
+ " def check(self):\n",
+ " print(self.__price)\n",
+ "\n",
+ "s=SmartPhone(20000, \"Apple\", 13)\n",
+ "s.show()"
+ ],
+ "metadata": {
+ "id": "OBJEbXw3QNs5",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "a6a7aa97-7b04-4f07-959f-290c1c82d2cd"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Inside phone constructor\n",
+ "20000\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "class Parent:\n",
+ "\n",
+ " def __init__(self,num):\n",
+ " self.__num=num\n",
+ "\n",
+ " def get_num(self):\n",
+ " return self.__num\n",
+ "\n",
+ "class Child(Parent):\n",
+ "\n",
+ " def show(self):\n",
+ " print(\"This is in child class\")\n",
+ " \n",
+ "son=Child(100)\n",
+ "print(son.get_num())\n",
+ "son.show()"
+ ],
+ "metadata": {
+ "id": "4ehYsT2BS5Cm",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "5e27f6d1-0f60-4246-fd59-977e41dfd4f9"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "100\n",
+ "This is in child class\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "class Parent:\n",
+ "\n",
+ " def __init__(self,num):\n",
+ " self.__num=num\n",
+ "\n",
+ " def get_num(self):\n",
+ " return self.__num\n",
+ "\n",
+ "class Child(Parent):\n",
+ "\n",
+ " def __init__(self,val,num):\n",
+ " self.__val=val\n",
+ "\n",
+ " def get_val(self):\n",
+ " return self.__val\n",
+ " \n",
+ "son=Child(100,10)\n",
+ "print(\"Parent: Num:\",son.get_num())\n",
+ "print(\"Child: Val:\",son.get_val())"
+ ],
+ "metadata": {
+ "id": "__jK3DNZS7-g",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 348
+ },
+ "outputId": "1cb66ea1-97e5-4589-d792-ec804c3ddb81"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "error",
+ "ename": "AttributeError",
+ "evalue": "ignored",
+ "traceback": [
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
+ "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
+ "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[0mson\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mChild\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m100\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 18\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Parent: Num:\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mson\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_num\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 19\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Child: Val:\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mson\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_val\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
+ "\u001b[0;32m\u001b[0m in \u001b[0;36mget_num\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mget_num\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__num\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0;32mclass\u001b[0m \u001b[0mChild\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mParent\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
+ "\u001b[0;31mAttributeError\u001b[0m: 'Child' object has no attribute '_Parent__num'"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "class A:\n",
+ " def __init__(self):\n",
+ " self.var1=100\n",
+ "\n",
+ " def display1(self,var1):\n",
+ " print(\"class A :\", self.var1)\n",
+ "class B(A):\n",
+ " \n",
+ " def display2(self,var1):\n",
+ " print(\"class B :\", self.var1)\n",
+ "\n",
+ "obj=B()\n",
+ "obj.display1(200)"
+ ],
+ "metadata": {
+ "id": "nyRDS-91TJA_",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "ca0f5671-e73a-467b-8c96-45f825e88eaa"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "class A : 200\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# Method Overriding\n",
+ "class Phone:\n",
+ " def __init__(self, price, brand, camera):\n",
+ " print (\"Inside phone constructor\")\n",
+ " self.__price = price\n",
+ " self.brand = brand\n",
+ " self.camera = camera\n",
+ "\n",
+ " def buy(self):\n",
+ " print (\"Buying a phone\")\n",
+ "\n",
+ "class SmartPhone(Phone):\n",
+ " def buy(self):\n",
+ " print (\"Buying a smartphone\")\n",
+ "\n",
+ "s=SmartPhone(20000, \"Apple\", 13)\n",
+ "\n",
+ "s.buy()"
+ ],
+ "metadata": {
+ "id": "EgDguVtCTlsY",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "e9a17cfc-e48d-4967-90df-164a9fb9906e"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Inside phone constructor\n",
+ "Buying a smartphone\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "### Super Keyword"
+ ],
+ "metadata": {
+ "id": "O85epdv770M-"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "class Phone:\n",
+ " def __init__(self, price, brand, camera):\n",
+ " print (\"Inside phone constructor\")\n",
+ " self.__price = price\n",
+ " self.brand = brand\n",
+ " self.camera = camera\n",
+ "\n",
+ " def buy(self):\n",
+ " print (\"Buying a phone\")\n",
+ "\n",
+ "class SmartPhone(Phone):\n",
+ " def buy(self):\n",
+ " print (\"Buying a smartphone\")\n",
+ " # syntax to call parent ka buy method\n",
+ " super().buy()\n",
+ "\n",
+ "s=SmartPhone(20000, \"Apple\", 13)\n",
+ "\n",
+ "s.buy()"
+ ],
+ "metadata": {
+ "id": "NR4F_NIo71hS",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "31d11020-10c1-47cd-81e0-6335ee6fc1f0"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Inside phone constructor\n",
+ "Buying a smartphone\n",
+ "Buying a phone\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# using super outside the class\n",
+ "class Phone:\n",
+ " def __init__(self, price, brand, camera):\n",
+ " print (\"Inside phone constructor\")\n",
+ " self.__price = price\n",
+ " self.brand = brand\n",
+ " self.camera = camera\n",
+ "\n",
+ " def buy(self):\n",
+ " print (\"Buying a phone\")\n",
+ "\n",
+ "class SmartPhone(Phone):\n",
+ " def buy(self):\n",
+ " print (\"Buying a smartphone\")\n",
+ " # syntax to call parent ka buy method\n",
+ " super().buy()\n",
+ "\n",
+ "s=SmartPhone(20000, \"Apple\", 13)\n",
+ "\n",
+ "s.buy()"
+ ],
+ "metadata": {
+ "id": "USJ2YcPLT68v",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 223
+ },
+ "outputId": "9039f621-2f56-4d1b-f4ed-8abc36ac4cd8"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Inside phone constructor\n"
+ ]
+ },
+ {
+ "output_type": "error",
+ "ename": "RuntimeError",
+ "evalue": "ignored",
+ "traceback": [
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
+ "\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)",
+ "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mSmartPhone\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m20000\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"Apple\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m13\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 19\u001b[0;31m \u001b[0msuper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbuy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
+ "\u001b[0;31mRuntimeError\u001b[0m: super(): no arguments"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# can super access parent ka data?\n",
+ "# using super outside the class\n",
+ "class Phone:\n",
+ " def __init__(self, price, brand, camera):\n",
+ " print (\"Inside phone constructor\")\n",
+ " self.__price = price\n",
+ " self.brand = brand\n",
+ " self.camera = camera\n",
+ "\n",
+ " def buy(self):\n",
+ " print (\"Buying a phone\")\n",
+ "\n",
+ "class SmartPhone(Phone):\n",
+ " def buy(self):\n",
+ " print (\"Buying a smartphone\")\n",
+ " # syntax to call parent ka buy method\n",
+ " print(super().brand)\n",
+ "\n",
+ "s=SmartPhone(20000, \"Apple\", 13)\n",
+ "\n",
+ "s.buy()"
+ ],
+ "metadata": {
+ "id": "KGIWVYdYUBAd",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 366
+ },
+ "outputId": "8e4228f8-af9c-4c72-d18c-6016808df3de"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Inside phone constructor\n",
+ "Buying a smartphone\n"
+ ]
+ },
+ {
+ "output_type": "error",
+ "ename": "AttributeError",
+ "evalue": "ignored",
+ "traceback": [
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
+ "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
+ "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mSmartPhone\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m20000\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"Apple\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m13\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 20\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 21\u001b[0;31m \u001b[0ms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbuy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
+ "\u001b[0;32m\u001b[0m in \u001b[0;36mbuy\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m\"Buying a smartphone\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0;31m# syntax to call parent ka buy method\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 17\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msuper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbrand\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 18\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mSmartPhone\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m20000\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"Apple\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m13\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
+ "\u001b[0;31mAttributeError\u001b[0m: 'super' object has no attribute 'brand'"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# super -> constuctor\n",
+ "class Phone:\n",
+ " def __init__(self, price, brand, camera):\n",
+ " print (\"Inside phone constructor\")\n",
+ " self.__price = price\n",
+ " self.brand = brand\n",
+ " self.camera = camera\n",
+ "\n",
+ "class SmartPhone(Phone):\n",
+ " def __init__(self, price, brand, camera, os, ram):\n",
+ " print('Inside smartphone constructor')\n",
+ " super().__init__(price, brand, camera)\n",
+ " self.os = os\n",
+ " self.ram = ram\n",
+ " print (\"Inside smartphone constructor\")\n",
+ "\n",
+ "s=SmartPhone(20000, \"Samsung\", 12, \"Android\", 2)\n",
+ "\n",
+ "print(s.os)\n",
+ "print(s.brand)"
+ ],
+ "metadata": {
+ "id": "jPW3DV5GUKNJ",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "5e1dfad5-6f71-48dd-ceaa-7d5211136968"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Inside smartphone constructor\n",
+ "Inside phone constructor\n",
+ "Inside smartphone constructor\n",
+ "Android\n",
+ "Samsung\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "##### Inheritance in summary\n",
+ "\n",
+ "- A class can inherit from another class.\n",
+ "\n",
+ "- Inheritance improves code reuse\n",
+ "\n",
+ "- Constructor, attributes, methods get inherited to the child class\n",
+ "\n",
+ "- The parent has no access to the child class\n",
+ "\n",
+ "- Private properties of parent are not accessible directly in child class\n",
+ "\n",
+ "- Child class can override the attributes or methods. This is called method overriding\n",
+ "\n",
+ "- super() is an inbuilt function which is used to invoke the parent class methods and constructor"
+ ],
+ "metadata": {
+ "id": "_YE_kj_0Ufe4"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "class Parent:\n",
+ "\n",
+ " def __init__(self,num):\n",
+ " self.__num=num\n",
+ "\n",
+ " def get_num(self):\n",
+ " return self.__num\n",
+ "\n",
+ "class Child(Parent):\n",
+ " \n",
+ " def __init__(self,num,val):\n",
+ " super().__init__(num)\n",
+ " self.__val=val\n",
+ "\n",
+ " def get_val(self):\n",
+ " return self.__val\n",
+ " \n",
+ "son=Child(100,200)\n",
+ "print(son.get_num())\n",
+ "print(son.get_val())"
+ ],
+ "metadata": {
+ "id": "iHFjwJtBUuK3",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "0fc43374-9b51-498d-d1ba-12defba202f3"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "100\n",
+ "200\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "class Parent:\n",
+ " def __init__(self):\n",
+ " self.num=100\n",
+ "\n",
+ "class Child(Parent):\n",
+ "\n",
+ " def __init__(self):\n",
+ " super().__init__()\n",
+ " self.var=200\n",
+ " \n",
+ " def show(self):\n",
+ " print(self.num)\n",
+ " print(self.var)\n",
+ "\n",
+ "son=Child()\n",
+ "son.show()"
+ ],
+ "metadata": {
+ "id": "szLznZ0bUvRj",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "0306224c-7467-420d-dbdb-d13e2c4e6a05"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "100\n",
+ "200\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "class Parent:\n",
+ " def __init__(self):\n",
+ " self.__num=100\n",
+ "\n",
+ " def show(self):\n",
+ " print(\"Parent:\",self.__num)\n",
+ "\n",
+ "class Child(Parent):\n",
+ " def __init__(self):\n",
+ " super().__init__()\n",
+ " self.__var=10\n",
+ "\n",
+ " def show(self):\n",
+ " print(\"Child:\",self.__var)\n",
+ "\n",
+ "obj=Child()\n",
+ "obj.show()"
+ ],
+ "metadata": {
+ "id": "1v9P77SdU5z9",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "d81815d0-a87f-4335-d486-744e6c5e239d"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Child: 10\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "class Parent:\n",
+ " def __init__(self):\n",
+ " self.__num=100\n",
+ "\n",
+ " def show(self):\n",
+ " print(\"Parent:\",self.__num)\n",
+ "\n",
+ "class Child(Parent):\n",
+ " def __init__(self):\n",
+ " super().__init__()\n",
+ " self.__var=10\n",
+ "\n",
+ " def show(self):\n",
+ " print(\"Child:\",self.__var)\n",
+ "\n",
+ "obj=Child()\n",
+ "obj.show()"
+ ],
+ "metadata": {
+ "id": "nc1DNBfxU6fc",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "6d7a25b3-a25f-42c0-a9d7-60cc7de72412"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Child: 10\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "### Types of Inheritance\n",
+ "\n",
+ "- Single Inheritance\n",
+ "- Multilevel Inheritance\n",
+ "- Hierarchical Inheritance\n",
+ "- Multiple Inheritance(Diamond Problem)\n",
+ "- Hybrid Inheritance"
+ ],
+ "metadata": {
+ "id": "q9WZMWV18G4Q"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# single inheritance\n",
+ "class Phone:\n",
+ " def __init__(self, price, brand, camera):\n",
+ " print (\"Inside phone constructor\")\n",
+ " self.__price = price\n",
+ " self.brand = brand\n",
+ " self.camera = camera\n",
+ "\n",
+ " def buy(self):\n",
+ " print (\"Buying a phone\")\n",
+ "\n",
+ "class SmartPhone(Phone):\n",
+ " pass\n",
+ "\n",
+ "SmartPhone(1000,\"Apple\",\"13px\").buy()"
+ ],
+ "metadata": {
+ "id": "OehIWuKS8JZm",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "69dde2a0-8b7e-49fd-f7a7-e86f4c2261e1"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Inside phone constructor\n",
+ "Buying a phone\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# multilevel\n",
+ "class Product:\n",
+ " def review(self):\n",
+ " print (\"Product customer review\")\n",
+ "\n",
+ "class Phone(Product):\n",
+ " def __init__(self, price, brand, camera):\n",
+ " print (\"Inside phone constructor\")\n",
+ " self.__price = price\n",
+ " self.brand = brand\n",
+ " self.camera = camera\n",
+ "\n",
+ " def buy(self):\n",
+ " print (\"Buying a phone\")\n",
+ "\n",
+ "class SmartPhone(Phone):\n",
+ " pass\n",
+ "\n",
+ "s=SmartPhone(20000, \"Apple\", 12)\n",
+ "\n",
+ "s.buy()\n",
+ "s.review()"
+ ],
+ "metadata": {
+ "id": "jGcAw7QtV1vN",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "9800414e-1785-4f14-9a1d-514fb3f03b39"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Inside phone constructor\n",
+ "Buying a phone\n",
+ "Product customer review\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# Hierarchical\n",
+ "class Phone:\n",
+ " def __init__(self, price, brand, camera):\n",
+ " print (\"Inside phone constructor\")\n",
+ " self.__price = price\n",
+ " self.brand = brand\n",
+ " self.camera = camera\n",
+ "\n",
+ " def buy(self):\n",
+ " print (\"Buying a phone\")\n",
+ "\n",
+ "class SmartPhone(Phone):\n",
+ " pass\n",
+ "\n",
+ "class FeaturePhone(Phone):\n",
+ " pass\n",
+ "\n",
+ "SmartPhone(1000,\"Apple\",\"13px\").buy()\n",
+ "FeaturePhone(10,\"Lava\",\"1px\").buy()"
+ ],
+ "metadata": {
+ "id": "axM6rE1HWA2d",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "0a5896e9-8301-4be2-8b2e-599354183b64"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Inside phone constructor\n",
+ "Buying a phone\n",
+ "Inside phone constructor\n",
+ "Buying a phone\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# Multiple\n",
+ "class Phone:\n",
+ " def __init__(self, price, brand, camera):\n",
+ " print (\"Inside phone constructor\")\n",
+ " self.__price = price\n",
+ " self.brand = brand\n",
+ " self.camera = camera\n",
+ "\n",
+ " def buy(self):\n",
+ " print (\"Buying a phone\")\n",
+ "\n",
+ "class Product:\n",
+ " def review(self):\n",
+ " print (\"Customer review\")\n",
+ "\n",
+ "class SmartPhone(Phone, Product):\n",
+ " pass\n",
+ "\n",
+ "s=SmartPhone(20000, \"Apple\", 12)\n",
+ "\n",
+ "s.buy()\n",
+ "s.review()\n"
+ ],
+ "metadata": {
+ "id": "MPY34iq8WWGw",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "58b6e426-a8ba-4a9e-c997-a1a65a768de2"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Inside phone constructor\n",
+ "Buying a phone\n",
+ "Customer review\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# the diamond problem\n",
+ "# https://stackoverflow.com/questions/56361048/what-is-the-diamond-problem-in-python-and-why-its-not-appear-in-python2\n",
+ "class Phone:\n",
+ " def __init__(self, price, brand, camera):\n",
+ " print (\"Inside phone constructor\")\n",
+ " self.__price = price\n",
+ " self.brand = brand\n",
+ " self.camera = camera\n",
+ "\n",
+ " def buy(self):\n",
+ " print (\"Buying a phone\")\n",
+ "\n",
+ "class Product:\n",
+ " def buy(self):\n",
+ " print (\"Product buy method\")\n",
+ "\n",
+ "# Method resolution order\n",
+ "class SmartPhone(Phone,Product):\n",
+ " pass\n",
+ "\n",
+ "s=SmartPhone(20000, \"Apple\", 12)\n",
+ "\n",
+ "s.buy()"
+ ],
+ "metadata": {
+ "id": "I2ixigsSWagF",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "ecde3fef-f9a7-469e-b0c4-ff0ef60d3b3b"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Inside phone constructor\n",
+ "Buying a phone\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "class A:\n",
+ "\n",
+ " def m1(self):\n",
+ " return 20\n",
+ "\n",
+ "class B(A):\n",
+ "\n",
+ " def m1(self):\n",
+ " return 30\n",
+ "\n",
+ " def m2(self):\n",
+ " return 40\n",
+ "\n",
+ "class C(B):\n",
+ " \n",
+ " def m2(self):\n",
+ " return 20\n",
+ "obj1=A()\n",
+ "obj2=B()\n",
+ "obj3=C()\n",
+ "print(obj1.m1() + obj3.m1()+ obj3.m2())"
+ ],
+ "metadata": {
+ "id": "sA_UVGoRWr7W",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "e71fe50b-c725-4c5f-a1d0-0dbc186fcfd0"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "70\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "class A:\n",
+ "\n",
+ " def m1(self):\n",
+ " return 20\n",
+ "\n",
+ "class B(A):\n",
+ "\n",
+ " def m1(self):\n",
+ " val=super().m1()+30\n",
+ " return val\n",
+ "\n",
+ "class C(B):\n",
+ " \n",
+ " def m1(self):\n",
+ " val=self.m1()+20\n",
+ " return val\n",
+ "obj=C()\n",
+ "print(obj.m1())"
+ ],
+ "metadata": {
+ "id": "ogbluGw-Wu3c",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 390
+ },
+ "outputId": "a08d2384-c4c3-4bd7-b7ac-896b22d98711"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "error",
+ "ename": "RecursionError",
+ "evalue": "ignored",
+ "traceback": [
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
+ "\u001b[0;31mRecursionError\u001b[0m Traceback (most recent call last)",
+ "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mval\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[0mobj\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mC\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 18\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mm1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
+ "\u001b[0;32m\u001b[0m in \u001b[0;36mm1\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mm1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 15\u001b[0;31m \u001b[0mval\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mm1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m20\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 16\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mval\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[0mobj\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mC\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
+ "... last 1 frames repeated, from the frame below ...\n",
+ "\u001b[0;32m\u001b[0m in \u001b[0;36mm1\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mm1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 15\u001b[0;31m \u001b[0mval\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mm1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m20\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 16\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mval\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[0mobj\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mC\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
+ "\u001b[0;31mRecursionError\u001b[0m: maximum recursion depth exceeded"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "### Polymorphism\n",
+ "\n",
+ "- Method Overriding\n",
+ "- Method Overloading\n",
+ "- Operator Overloading"
+ ],
+ "metadata": {
+ "id": "TvZX-Zw9FtKB"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "class Shape:\n",
+ "\n",
+ " def area(self,a,b=0):\n",
+ " if b == 0:\n",
+ " return 3.14*a*a\n",
+ " else:\n",
+ " return a*b\n",
+ "\n",
+ "s = Shape()\n",
+ "\n",
+ "print(s.area(2))\n",
+ "print(s.area(3,4))"
+ ],
+ "metadata": {
+ "id": "wuVzSLONF0ha",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "c0ef3d86-9a4b-4d2f-f6b4-2ca8b7d42764"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "12.56\n",
+ "12\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "'hello' + 'world'"
+ ],
+ "metadata": {
+ "id": "3-SJbwpMXE4J",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 36
+ },
+ "outputId": "1fb14d61-32b2-4267-e997-edbd98de1c22"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "'helloworld'"
+ ],
+ "application/vnd.google.colaboratory.intrinsic+json": {
+ "type": "string"
+ }
+ },
+ "metadata": {},
+ "execution_count": 61
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "4 + 5"
+ ],
+ "metadata": {
+ "id": "XIhrt6ffXFOq",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "5936ff62-3649-470b-8080-27e0932d795c"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "9"
+ ]
+ },
+ "metadata": {},
+ "execution_count": 62
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "[1,2,3] + [4,5]"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "ykLFszsHfrKi",
+ "outputId": "b6dce7af-c9d9-4414-bd3e-dbdacc9df9e1"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ "[1, 2, 3, 4, 5]"
+ ]
+ },
+ "metadata": {},
+ "execution_count": 63
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "### Abstraction"
+ ],
+ "metadata": {
+ "id": "FHZlflrSF1Df"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "from abc import ABC,abstractmethod\n",
+ "class BankApp(ABC):\n",
+ "\n",
+ " def database(self):\n",
+ " print('connected to database')\n",
+ "\n",
+ " @abstractmethod\n",
+ " def security(self):\n",
+ " pass\n",
+ "\n",
+ " @abstractmethod\n",
+ " def display(self):\n",
+ " pass\n"
+ ],
+ "metadata": {
+ "id": "EFrfU_45F2wx"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "class MobileApp(BankApp):\n",
+ "\n",
+ " def mobile_login(self):\n",
+ " print('login into mobile')\n",
+ "\n",
+ " def security(self):\n",
+ " print('mobile security')\n",
+ "\n",
+ " def display(self):\n",
+ " print('display')"
+ ],
+ "metadata": {
+ "id": "_KULGDqYxtwk"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "mob = MobileApp()"
+ ],
+ "metadata": {
+ "id": "WlGxjLHz6J94"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "mob.security()"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "e-ygxfhV6MR9",
+ "outputId": "93969604-41c2-4966-d322-ffaadcd85e4f"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "mobile security\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "obj = BankApp()"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 169
+ },
+ "id": "hvVMYalv6v3j",
+ "outputId": "2809badf-e568-4a87-ddfb-1f64d944900d"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "error",
+ "ename": "TypeError",
+ "evalue": "ignored",
+ "traceback": [
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
+ "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
+ "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mobj\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mBankApp\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
+ "\u001b[0;31mTypeError\u001b[0m: Can't instantiate abstract class BankApp with abstract methods display, security"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [],
+ "metadata": {
+ "id": "OvdixiRJ7kDn"
+ },
+ "execution_count": null,
+ "outputs": []
+ }
+ ]
+}
\ No newline at end of file