Last active
May 16, 2021 12:27
-
-
Save Abhayparashar31/c739742f932c0ebcc9d0b51d23b09825 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Employee: | |
| def __init__(self,name,age,exp,salary): ## Defining The Constructor With Common data | |
| ## Instance Attributes ## instance attributes that are only accessable by the object of the class | |
| self.name = name | |
| self.age = age | |
| self.exp = exp | |
| self.salary = salary | |
| def show(self): ## A Simple method that prints the data | |
| print(self.name,self.age,self.exp,self.salary) ## Printing all the variables | |
| class Engineers(Employee): ## Parent class Employee inherit by child Enginners | |
| def __init__(self,name,age,exp,salary,level): ## Constructor of Enginners class(child) | |
| super().__init__(name,age,exp,salary) ## accessing the parent class constructor and instance attributes with the help of super method | |
| self.level = level ## Assigning a new attribute level for the Enginner class | |
| def print_data(self): ## Creating a New Method for the Enginner class that is only accessable by the object of Enginner class | |
| print(self.level) ## Printing the level of the Enginner | |
| class Designers(Employee): ## Parent class Employee inherit by child Designers | |
| def __init__(self,name,age,exp,salary,position): | |
| super().__init__(name,age,exp,salary) | |
| self.position = position ## Extending the attributes of parent class by adding a new attribute position | |
| def show(self): ## A Simple Method Belong to the Designer Class that is used to print the data | |
| super().show() ## Accessing parent class method and extending it to print the position also | |
| print(self.position) ## Printing the position of the Designer | |
| obj_E = Engineers("Alex",35,10,50000,'JR Developer') ## Creating an object for the Enginners class, Need to paas arguments because It is a inherit class | |
| obj_E.show() ## accessing parent method show with the help of the child class object | |
| obj_E.print_data() ## accessing child class method debug, that is only access by the Designers object | |
| obj_D = Designers("Karl",45,20,55000,"UI Designer") | |
| obj_D.show() ## printing all the data | |
| ''' | |
| obj_E.debug() ## AttributeError: 'Engineers' object has no attribute 'debug' ,debug is not a member of the Enginners class | |
| Employee('Garry',35,10,50000).show() ## Garry 35 10 50000 ,because a class can access class attributes without the need of an object | |
| obj = Employee('Garry',35,10,50000) | |
| obj.debug() ## AttributeError: 'Employee' object has no attribute 'debug' , A parent can not access a child class | |
| ''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment