Preview

04-Abstraction and Inheritance

 1. In the example below, how is abstraction used?
class Planet:
   
    def __init__(self,x,y,radius):
        self.radius=radius #circles need a radius in order to be defined
        self.x=x
        self.y=y
            
    canvas = Screen()
    canvas.setup(800,800)
    turtle = Turtle()

  All details including ones that are irrelevant to the problem have been used

  The method distance_from_sun cannot be verified and is therefore abstract

  Only details that are relevant to the problem have been used (e.g. attributes x and y)

  The creation of a planet is an abstract act, as a planet cannot exist in space-time

 2. Abstraction is required to enable complex procedures or events to be modelled. Computer programs cannot account for every single element of an event as this would be ..

  extremely easy and time efficient

  processor heavy in terms of the cache development

  extremely complex and time consuming.

  hard on the CPU and RAM

 3. A common misconception is that abstraction removes detail. In a way it does, but the core concept is that it removes ______________ detail, allowing the programmer to focus on what is important and thereby making it more accessible

  unnecessary

  erroneous

  cumbersome

  ugly

 4. The terms encapsulation and abstraction (also data hiding) are often used as synonyms. They are nearly synonymous, i.e. abstraction is achieved though encapsulation

  False

  True

 5. Classes can inherit [from] other classes. A class that inherits from a superclass will automatically inherit the ....

  the behaviours and definitions of that class

  attributes and methods of that class

  variables and sub variables

  the objects and classes of that class

 6. In the example below, which is the parent/base class and which is the child/derived class?
class Toshiba:
    manufacturer="Toshiba Inc"
    contactWebsite="www.toshiba.com/contact"

    def contactDetails
    (self):
        print("To contact us, simply go to", self.contactWebsite)

class ToshLaptop(Toshiba): #this inherits from class Toshiba
#the Laptop class can have its own attributes and methods 
    def __init__(self):
        self.yearofManufacture=2017

    def manufactureDetails(self):
        print("created in the year {} by {}".
			  format(self.yearofManufacture, self.manufacturer))

toshiba_laptop=ToshLaptop() #creating an object for the class ToshLaptop
#use the object above to call the methods

#===================DEMONSTRATION OF SINGLE INHERITANCE =================
toshiba_laptop.manufactureDetails()
toshiba_laptop.contactDetails()

  contactDetails is the parent class and manufactureDetails is the child class

  ToshLaptop Class is the base/parent class, and Toshiba is the child or derived class.

  There are no classes defined in this example

  Toshiba is the base/parent class, and Laptop Class is the child or derived class.

 7. Which line shows the inheritance in action? What type of inheritance is this demonstrating?
class Toshiba:
    manufacturer="Toshiba Inc"
    contactWebsite="www.toshiba.com/contact"

    def contactDetails
    (self):
        print("To contact us, simply go to", self.contactWebsite)

class ToshLaptop(Toshiba): 
#the Laptop class can have its own attributes and methods 
    def __init__(self):
        self.yearofManufacture=2017

    def manufactureDetails(self):
        print("This Toshiba laptop was created in the year 
			  {} by {}".format(self.yearofManufacture, self.manufacturer))

  Line 9, Single Inheritance

  Line 9, Multilevel Inheritance

  Line 14, Single Inheritance

  Line 1, Multiple Inheritance

 8. In programming a derived class can have more than one base/parent class having access to all the relevant attributes and methods. A real life example would be a child inheriting abilities from two parents. What type of inheritance is this?

  Multilevel Inheritance

  Multiple Inheritance

  Two parent Inheritance

  Single Inheritance

 9. What is the definition of Multilevel inheritance? A real life example would be if your father played chess, you inherited this ability, but you also like to dance. Your son, if you have one, could inherit both your father's ability to play chess as well

  Mechanism by which inheritance is passed up rather than down to super classes

  Mechanism by which a base class derives from a derived class which has been inherited itself

  Mechanism by which a super class is split in half and derives half its attributes

  Mechanism by which a derived class inherits from a base class which has been derived from another base class

 10. What is an advantage associated with the feature of OOP that we call "inheritance"?

  all of the options here are potential advantages

  cutting out the need to rewrite attributes and methods for a child class

  you can add new methods to a class without modifying the existing class

  code re-use