Create a base class Person and a derived class Student with Person as its base class.Add two methods setname() (which takes the parameter self and name)and getname() which prints the name in the base class.Add two methods in the derived class: setage() (which takes the parameters self and age) which sets the age and getage() which prints the age.Create an instance of Student and name it as s1.Take name and age as inputs from the console.Call the setname() and setage() on this instance by passing the name and age parameters.Call the getname() and getage() on this class, which prints the passed parametersNotice that we have created an object of derived class but it could access the methods of base class.This is inheritance.
Question
Create a base class Person and a derived class Student with Person as its base class.Add two methods setname() (which takes the parameter self and name)and getname() which prints the name in the base class.Add two methods in the derived class: setage() (which takes the parameters self and age) which sets the age and getage() which prints the age.Create an instance of Student and name it as s1.Take name and age as inputs from the console.Call the setname() and setage() on this instance by passing the name and age parameters.Call the getname() and getage() on this class, which prints the passed parametersNotice that we have created an object of derived class but it could access the methods of base class.This is inheritance.
Solution
Here is the Python code that implements the above instructions:
class Person:
def setname(self, name):
self.name = name
def getname(self):
print(self.name)
class Student(Person):
def setage(self, age):
self.age = age
def getage(self):
print(self.age)
s1 = Student()
name = input("Enter name: ")
age = int(input("Enter age: "))
s1.setname(name)
s1.setage(age)
s1.getname()
s1.getage()
In this code:
- We first define a base class
Personwith methodssetname()andgetname(). - Then we define a derived class
Studentthat inherits fromPersonand adds two more methods:setage()andgetage(). - We create an instance
s1of theStudentclass. - We take the name and age as inputs from the console.
- We call the
setname()andsetage()methods ons1, passing the name and age as parameters. - Finally, we call the
getname()andgetage()methods ons1, which print the name and age.
Similar Questions
Define a class StudentDefine __init__ method with arguments self, name and age.Inside the __init__ method, set self.name = name, self.age = age.Take name and age from the console by calling input and store them in the variables s1_name, s1_ageCreate an instance Stud_1 of class Student by passing the values as argumentsTake name and age from the console by calling input and store them in the variables s2_name, s2_ageCreate an instance Stud_2 of class Student by passing the values as argumentsPrint Stud_1.namePrint Stud_2.name
Write a java program to define a class Person having name of the person , age of the person and gender of the person. Include accept() and toString() method to display the data. Create 3 objects and display name of the person who is youngest
Student class is defined with public variables name, age, group and reportDefine setDetails() method that sets the student name, age, group and report values.Define getDetails(self) method that prints all the details of the student.Create an instance of class StudentSet the name, age, group and report values of the student by taking the input from the student and call the setDetails() valuesNow print the details of Student by calling the method showDetails()Sample Test Cases
What is the correct syntax to create a class named Student that will inherit properties and methods from a class named Person?
Create a class named 'Student' with a string variable 'name' and an integer variable 'roll_no'. Assign the value of roll_no as '2' and that of name as "John" by creating an object of the class Student.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.