Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Python
  1  class Grandfather:
  2      last = 'wilson'
  3      citizenship = 'american'
  4      residence = 'Detroit'
  5      def __init__(self,first,college,age,career,workplace):
  6          self.first = first
  7          self.college = college
  8          self.age = age
  9          self.career = career
 10          self.workplace = workplace
 11          self.email = first + '-' + Grandfather.last + '@email.com'
 12      @property
 13      def fullname(self):
 14          return '{} {}'.format(self.first,Grandfather.last)
 15      def __str__(self):
 16          return '{} {}'.format(self.first,self.last)
 17      @property
 18      def history(self):
 19         return 'my name is : {}, i graduated from {} and work as {} in {}'.format\
 20             (self.fullname,self.college,self.career,self.workplace)
 21      @classmethod
 22      def set_residence(cls,newR):
 23          cls.residence = newR
 24      @classmethod
 25      def set_cit(cls,new_cit):
 26          cls.citizenship = new_cit
 27      
 28  
 29  
 30  
 31  class Father(Grandfather):
 32      def __init__(self,first,college,age,career,workplace,sport,wife):
 33          super().__init__(first,college,age,career,workplace)
 34          self.sport = sport
 35          self.wife = wife
 36  
 37  
 38  class Son(Father):
 39      def __init__(self,first,college,age,career,workplace,Vedio_Game,Car):
 40          super().__init__(first,college,age,career,workplace)
 41          self.Vedio_Game = Vedio_Game
 42          self.Car = Car
 43  
 44  john = Grandfather('john','stanford',70,'engineer','Ford')
 45  mark = Father('mark','MIT',40,'CScientist','SONY','Tennis','Sara')
 46  Anna = Aunt('Anna','Cambridge',38,'Teacher','school','black','Basketball')
 47  Maik = Son('Maik','MIT',26,'SoftwareEngineer','Google','MineCraft','Toyota')
 48  if __name__ == '__main__':
 49      print(john.fullname)
 50      print(john)
 51      print(john.history)
 52      Grandfather.set_residence('Frankfurt')
 53      print(Grandfather.residence)
 54      print('############')
 55      print(mark.fullname)
 56      print(mark)
 57      print(mark.history)
 58      print(mark.residence)
 59      print('###########')
 60      print(Maik.fullname)
 61      print(Maik)
 62      print(Maik.residence)


What I have tried:

i tried to execute the program but it throws the error :
Traceback (most recent call last):
  File "my_Errors.py", line 66, in <module>
    Maik = Son('Maik','MIT',26,'SoftwareEngineer','Google','MineCraft','Toyota')
  File "my_Errors.py", line 40, in __init__
    super().__init__(first,college,age,career,workplace)
TypeError: __init__() missing 2 required positional arguments: 'sport' and 'wife'
Posted
Updated 19-Jun-20 23:26pm
v2
Comments
Richard MacCutchan 20-Jun-20 4:51am    
Also edited your question so we can actually see which are lines 66 and 40. Although there are obviously some lines missing.

This is the same issue as your previous question at Why the multilevel inheritance doest work ?[^]. You must pass the correct number of parameters to the super class, as clearly stated in the error message. If you do not understand how classes and inheritance work then see 9. Classes — Python 3.7.8rc1 documentation[^].
 
Share this answer
 
Comments
Ahmad Qassym 20-Jun-20 6:24am    
thanx for the answer but its not the same, in the previous one i provided the the son class with all instance attributes of the father class(the existing attributes of Son in that question and sport and wife)
what am trying to know through that quesiton can we write the son class without all attributes of the Father class (sport and wife)
Richard MacCutchan 20-Jun-20 8:50am    
Yes it is exactly the same issue. The problem is, as markkuk suggests below, you still do not understand what inheritance is for.
I think you don't really understand what "inheritance" in object-oriented programming means (it's not a way to describe family trees). Inheritance describes an "is-a" relationship between the subclass and parent class. In your code the class hierarchy would require that each "Son" is also a "Father" and a "Grandfather". In this case it appears you are desribing a "has-a" type relationship, which is described using composition[^] instead of inheritance[^]
 
Share this answer
 
Comments
Ahmad Qassym 20-Jun-20 6:36am    
thanx for the answer
what am trying to know through that quesiton can we write the son class without all attributes of the Father class (sport and wife)
markkuk 20-Jun-20 6:40am    
No, a subclass can only add properties or functionality to parent class, not remove them.
Ahmad Qassym 9-Jul-20 13:48pm    
i saw that example on internet:
u said that subclass can only add properties on the superclass but not remove them..but in htis example it removed properties.
do u know why ?
class quadriLateral:
def __init__(self, a, b, c, d):
self.side1=a
self.side2=b
self.side3=c
self.side4=d
class rectangle(quadriLateral):
def __init__(self, a, b):
super().__init__(a, b, a, b)


here is the link
https://www.tutorialsteacher.com/python/inheritance-in-python
Ahmad Qassym 20-Jun-20 8:27am    
u mean tht we cannot just write
def __init__(self,first,college,age,career,workplace,Vedio_Game,Car):
we must add to them (sport,wife) attributes that exist in the superclass father ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900