Python program to illustrate the working of abstract method

Here, we will see a Python program to illustrate the working of abstract methods using a percent calculation program.
Submitted by Shivang Yadav, on March 12, 2021

Abstract class is a special type of class that does not contain the implementation of its functions i.e. it contains only declarations. And then the inheriting classes will provide the implementation of the methods.

Formally, a class with one or more abstract methods is known as abstract class.

Abstract method is a method whose definition is not present.

Program to illustrate the working of abstract method

from abc import ABC,abstractmethod

class Student(ABC):
    @abstractmethod
    def Get(self):
        pass
    @abstractmethod
    def Put(self):
        pass

class Bsc(Student):
    def Get(self):
        self.__roll = input("Enter Roll : ")
        self.__name = input("Enter Name : ")

        self.__p = int(input("Enter Physics : "))
        self.__c = int(input("Enter Chemistry : "))
        self.__m = int(input("Enter Maths : "))

    def Put(self):
        print(self.__roll, self.__name)
        print("Percentage : ", ((self.__p + self.__c + self.__m)/3))

class Ba(Student):
    def Get(self):
        self.__roll = input("Enter Roll : ")
        self.__name = input("Enter Name : ")

        self.__g = int(input("Enter Geo : "))
        self.__e = int(input("Enter Economics : "))


    def Put(self):
        print(self.__roll, self.__name)
        print("Percentage : ", ((self.__g + self.__e)/2))

student=Bsc()
student.Get()
student.Put()
student=Ba()
student.Get()
student.Put()

Output:

Enter Roll : 32
Enter Name : John
Enter Physics : 43
Enter Chemistry : 67
Enter Maths : 91
32 John
Percentage :  67.0
Enter Roll : 76
Enter Name : JNW  ane
Enter Geo : 89
Enter Economics : 93
76 Jane
Percentage :  91.0

Explanation:

In the above code, we have created an abstract class named student with abstract methods Get() and Put() which are then redefined in child classes, Bsc and Ba. Then we have asked the user for inputs and printed the student's details and percentage.

Python class & object programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.