Advertisement
Shailrshah

Student and Instructor Classes Extending Person Class

Oct 29th, 2013
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.30 KB | None | 0 0
  1. import java.util.Scanner;
  2. class Person{
  3.     String name;
  4.     int year;
  5.     void getCommonDetails(){
  6.         Scanner sc = new Scanner(System.in);
  7.         System.out.print("Enter the name: ");
  8.         name = sc.next();
  9.         System.out.print("Enter the year of birth: ");
  10.         year = sc.nextInt();
  11.     }
  12. }
  13. class Student extends Person{
  14.     String major;
  15.     Student(){
  16.         System.out.println("A Student Object has been created.");
  17.     }
  18.     void getDetails(){
  19.         getCommonDetails();
  20.         Scanner sc = new Scanner(System.in);
  21.         System.out.print("Enter the subject the student is majoring in: ");
  22.         major = sc.next();
  23.     }
  24.     void printDetails(){
  25.         System.out.println(name+", born in "+year+" is majoring in "+major);
  26.     }
  27. }
  28. class Instructor extends Person{
  29.     int salary;
  30.     Instructor(){
  31.         System.out.println("An Instructor Object has been created.");
  32.     }
  33.     void getDetails(){
  34.         getCommonDetails();
  35.         Scanner sc = new Scanner(System.in);
  36.         System.out.print("Enter the salary of the instructor: ");
  37.         salary = sc.nextInt();
  38.     }
  39.     void printDetails(){
  40.         System.out.println(name+", born in "+year+" is earns Rs."+salary);
  41.     }
  42. }
  43. class People{
  44.     public static void main(String[] args){
  45.         Student s = new Student();
  46.         s.getDetails();
  47.         Instructor i = new Instructor();
  48.         i.getDetails();
  49.         System.out.println("\n\n");
  50.         s.printDetails();
  51.         i.printDetails();
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement