Define an abstract class Staff with protected members id and name. Define a parameterized constructor. Define one subclass OfficeStaff with member department. Create n objects of OfficeStaff and display all details.
import java.io.*;
import java.util.*;
abstract class Staff
{
String name;
int id;
}
class OfficeStaff extends Staff
{
String dept;
public void accept() throws IOException
{
Scanner sc =new Scanner(System.in);
System.out.println("Enter name, id and department :");
name=sc.nextLine();
id=sc.nextInt();
dept=sc.next();
}
public void display() throws IOException
{
System.out.println("Name : "+name);
System.out.println("id : "+id);
System.out.println("Department : "+dept);
System.out.println("-------------------------------------");
}
}
public class Main
{
public static void main(String [] args) throws IOException
{
int i;
Scanner s=new Scanner(System.in);
System.out.println("Enter no. of Office Staff : ");
int n=s.nextInt();
OfficeStaff[] l=new OfficeStaff[n];
for (i=0; i<n; i++)
{
l[i]=new OfficeStaff();
l[i].accept();
}
for (i=0; i<n; i++)
{
l[i].display();
}
}
}
Comments
Post a Comment