GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — PythonScript/Class/5.InheritanceInPythonClass.py at master · chavarera/PythonScript

Expand file tree

/

5.InheritanceInPythonClass.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
61 lines (44 loc) · 2.51 KB
/

5.InheritanceInPythonClass.py

Copy path

File metadata and controls

61 lines (44 loc) · 2.51 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Inheritance
# inheritance means creating a new class who access variables and methods from parent class
# A class can inherit attributes and behaviour methods from another class, called the superclass. A class which inherits from a superclass is called a subclass, also called heir class or child class
# simple Example
# laptop -has atrributes brand,screen,hdd,ram,
# gaming laptop-has attributes brand,screen ,hdd,ram,graphicscard,processing clock speed
# You can derive initailization from parent class in two way
# 1. super().__init__(Variable from parent claas)
# 2.Classname.__init__(self,Variable from parent claas)
# Now First Create a Class for normal Laptop
# this is base class/parent class
class Laptop():
def __init__(self, brand, screen, hdd, ram):
self.screen = screen
self.ram = ram
self.brand = brand
self.hdd = hdd
def showinfo(self):
print('The laptop brand is {} whose screen size is {}. And who has {} TB hdd and {} gb ram'.format(self.brand,
self.screen,
self.hdd,
self.ram))
def data(self):
print('data is for normal laptop')
# Now Create Another class for GamingLaptop
# but gaming laptop has some attributes which are already present in laptop class
# so create new class with name GamingLaptop which inherits laptop class inside it
# this is derived class
class GamingLaptop(Laptop): # add parent class name inside bracket
def __init__(self, brand, screen, hdd, ram, graphics, cpuspeed):
super().__init__(brand, screen, hdd,
ram) # call parent class method __init__() to define brand,screen,hdd,ram from base class or parent class Laptop
# now define remaining attributes here
self.graphics = graphics
self.cpuspeed = cpuspeed
def data(self):
print('data is for Gaming laptop')
# Now create a object of derived class and call method from parent class
lenovogaming = GamingLaptop('Lenovo', 15.6, 1, 8, 4, '3.0ghz')
lenovogaming.showinfo()
# method overriding
# derived class has a same name method as compare to parent class then only derived class methood is called
# both class has same method data()
lenovogaming.data() # this will call method of GamingLaptop Class
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.