0% found this document useful (0 votes)
3 views3 pages

Inheritance 2

The document contains a Java program that demonstrates inheritance through three classes: student, teacher, and staff, all derived from a base class called info. Each derived class has its own specific attributes and methods for inputting and displaying data related to students, teachers, and staff members. The main method orchestrates the creation of objects for each class and facilitates data entry and output for each type of individual.

Uploaded by

Shahid Iqbal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Inheritance 2

The document contains a Java program that demonstrates inheritance through three classes: student, teacher, and staff, all derived from a base class called info. Each derived class has its own specific attributes and methods for inputting and displaying data related to students, teachers, and staff members. The main method orchestrates the creation of objects for each class and facilitates data entry and output for each type of individual.

Uploaded by

Shahid Iqbal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.util.

Scanner;

class info
{ Scanner ob = new Scanner(System.in);
String name;
String fname;
int phoneNo;

void getdata( )
{ System.out.print(" Put name: ");
name = ob.nextLine();
System.out.print(" Put Father Name: ");
fname = ob.nextLine();
System.out.print(" Phone No: ");
phoneNo = ob.nextInt();
}
void putData( )
{ System.out.println(" Name: "+name);
System.out.println(" Father Name: "+fname);
System.out.println(" Phone No: "+phoneNo);
}
}

class student extends info // info is base class and student is derived class
{ int ID;
String major;
float GPA;

void getStData( )
{
System.out.print(" Put ID: ");
ID = ob.nextInt();
ob.nextLine();// to clear input buffer
System.out.print(" Put Major Feild: ");
major = ob.nextLine();
System.out.print(" GPA: ");
GPA = ob.nextFloat();
}

void putStData( )
{ System.out.println(" Student ID: "+ID);
System.out.println(" Major Field: "+major);
System.out.println(" Grade: "+GPA);
}

class teacher extends info // info is base class and teacher is derived
class
{ int teacherID;
String jobTitle;
int salary;

void getTeacherData( )
{
System.out.print(" Put Teacher's ID: ");
teacherID = ob.nextInt();
ob.nextLine();// to clear input buffer
System.out.print(" Put Position: ");
jobTitle = ob.nextLine();
System.out.print(" Salary: ");
salary = ob.nextInt();
}

void putTeacherData( )
{ System.out.println(" Teacher ID: "+teacherID);
System.out.println(" Position: "+jobTitle);
System.out.println(" Salary: "+salary);
}

class staff extends info // info is base class and staff is derived class
{ int Staff_ID;
String job;
String college_name;

void getStaffData( )
{
System.out.print(" Put Staff_ID: ");
Staff_ID = ob.nextInt();
ob.nextLine();// to clear input buffer
System.out.print(" Put Job Title of Staff: ");
job = ob.nextLine();
System.out.print(" Put College_Name: ");
college_name = ob.nextLine();
}

void putStaffData( )
{ System.out.println(" Staff_ID: "+Staff_ID);
System.out.println(" Job Title : "+job);
System.out.println(" College_Name: "+college_name);
}

public class Inheritance2 {

public static void main(String[] args) {

// here info is the base class.


student st = new student(); // object of student class, which also has all
properties of info class as well.
teacher tch = new teacher(); // object of teacher class, which also has all
properties of info class
staff ss = new staff(); // object of staff class, which also has all
properties of info class

System.out.println("\n Put data for Student: ");


System.out.println(" ~~~~~~~~~~~~~~~~~~~~~~~ ");
st.getdata( ); // function from base class
st.getStData( ); // function from derived class

System.out.println("\n Put data for Teacher: ");


System.out.println(" ~~~~~~~~~~~~~~~~~~~~~~~~~ ");
tch.getdata( ); // function from base class
tch.getTeacherData( ); // function from derived class

System.out.println("\n Put data for Staff:");


System.out.println(" ~~~~~~~~~~~~~~~~~~~~~ ");
ss.getdata( ); // function from base class
ss.getStaffData( ); // function from derived class

System.out.println("\n\n\n Information of Student ");


System.out.println(" ~~~~~~~~~~~~~~~~~~~~~~~ ");
st.putData( ); // function from base class
st.putStData( ); // function from derived class

System.out.println("\n\n\n Information of Teacher ");


System.out.println(" ~~~~~~~~~~~~~~~~~~~~~~~ ");
tch.putData( ); // function from base class
tch.putTeacherData( ); // function from derived class

System.out.println("\n\n\n Information of Staff ");


System.out.println(" ~~~~~~~~~~~~~~~~~~~~~~~ ");
ss.putData( ); // function from base class
ss.putStaffData( ); // function from derived class
}

You might also like