Person-Student Inheritance

Problem Statement:

Let's write a simple program:

  • A base class Person and a derived class Student with Person as its base class.
  • Add two methods in the base class: setname()-takes the parameter name which sets the name value, and getname() which prints the name.
  • Add two methods in the derived class: setage()-takes the parameter age which sets the age value, 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 parameters
Sample Input:
Govardhan
37
Sample Output:
Govardhan
37
Solution in JAVA PYTHON
import java.util.Scanner;
class Person{
  String name;
  public void setName(String name){
    this.name = name;
  }
  public void getName(){
    System.out.println(name);
  }
}
public class Student extends Person{
  int age;
  public void setAge(int age){
    this.age = age;
  }
  public void getAge(){
    System.out.println(age);
  }
  public static void main(String args[]){
    Scanner sc = new Scanner(System.in);
    String name = sc.nextLine();
    int age = sc.nextInt();
    Student s1 = new Student();
    s1.setName(name);
    s1.setAge(age);
    s1.getName();
    s1.getAge();
  }
}