Page 414 - Computer_Science_F5
P. 414

dog = Dog()
                   # Calling methods of the Dog class

                   dog.speak()  # Output: Animal speaks
                   dog.bark()   # Output: Dog barks                                                Chapter Six: Object oriented programming with Python
          FOR ONLINE READING ONLY

                Output:










                                   Figure 6.9: Output of inheritance program code


              Abstraction in Python

              Abstraction in Python involves hiding the complex implementation details of a class
              and showing only the necessary features of an object. It allows programmers to
              focus on the essential aspects of an object while hiding the irrelevant details. The
              following is an example of defining the syntax for abstraction.

                   from abc import ABC, abstractmethod
                   class MyAbstractClass(ABC):
                       #abstractmethod
                       def my_abstract_method(self):
                           pass


              The following Program Example 6.6 demonstrates abstraction in Python.
                       Program Example 6.6:

               Program codes to demonstrate abstraction


                    from abc import ABC, abstractmethod
                    # Abstract class
                    class Animal(ABC):
                        @abstractmethod
                        def make_sound(self):
                            pass
                    # Concrete classes
                    class Dog(Animal):


                                                    405
               Student’s Book  Form Five



     Computer Science Form 5.indd   405                                                     23/07/2024   12:34
   409   410   411   412   413   414   415   416   417   418   419