Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+5 votes

What I mean by this is for example lets take the classes from routine practice 19. If we wanted to call the class Coins in the class Food, is that a possibility?

asked in CSC201 Spring 2021 by (1 point)

1 Answer

+5 votes
 
Best answer

There is a concept in OOD called Inheritance. In Python, you can have a derived class - a class created or derived from another existing class. It inherits all the methods and properties from another class.

# Creating a Base class 
class Base: 
  
    # Declaring public method
    def fun(self):
        print("Public method")
  
    # Declaring private method
    def __fun(self):
        print("Private method")
  
# Creating a derived class 
class Derived(Base): 
    def __init__(self): 
          
        # Calling constructor of 
        # Base class 
        Base.__init__(self) 
          
    def call_public(self):
          
        # Calling public method of base class
        print("\nInside derived class")
        self.fun()
          
    def call_private(self):
          
        # Calling private method of base class
        self.__fun()
  
# Driver code 
obj1 = Base()
  
# Calling public method
obj1.fun()
  
obj2 = Derived()
obj2.call_public()
answered by (1 point)
selected by
...