JAVA Inheritance With Concept and Examples / Output

PROGRAM STATEMENT

Design a super class called Staff with details as StaffId, Name, Phone, Salary. Extend this class by writing three subclasses namely Teaching (domain, publications), Technical (skills), and Contract (period). Write a Java program to read and display at least 3 staff objects of all three categories.

CONCEPT

[alert-announce]Here in this given problem we shall use inheritance for extending Staff class into Teaching, Technical and Contract 3 subclasses using extends keyword. Each class is having the variables as given in the bracket. We will import the util package Scanner class to read 3 objects of each class. Create a constructor of Staff class to initialize StaffId, Name, Phone, Salary. And one display functions into the Staff class to display the entered values. All the data members of Staff will be inherited in Teaching, Technical and Contract using the super keyword that calls the superclass constructor to the base class. Other additional data members of the subclasses would be initialized using their own constructor. Also along with their own constructors, all 3 subclasses will have their own display method that invokes display method of super class Staff. Now in main() method using Scanner class we will read the values accordingly. To display these values we will create an array of object of size 3 for each subclass Teaching, Technical and Contract. Using this array of objects we will display the values entered previously by invoking display method of each subclass. Below is the program that demonstrates the same.[/alert-announce]

 

EXAMPLE INHERITANCE IN JAVA (SUPER CLASS / SUBCLASS) PROGRAM

/** Design a super class called Staff with details as StaffId, Name, Phone, Salary. Extend
this class by writing three subclasses namely Teaching (domain, publications),
Technical (skills), and Contract (period). Write a Java program to read and display at
least 3 staff objects of all three categories.*/

package daa.EXP2AB;
import java.util.Scanner;
class Staff
{ //data members of Staff class
String StaffId=null, Name=null, Phone=null;
float Salary;
//constructor to initialize Staff class data members
Staff(String S,String N,String P,float Sa)
{ StaffId=S;
Name=N;
Phone=P;
Salary=Sa;
}
//display method to display the data members
public void display()
{ System.out.println("Entered Details are:");
System.out.println("StaffID:"+StaffId);
System.out.println("Name:"+Name);
System.out.println("Phone:"+Phone);
System.out.println("Salary:"+Salary);
}
}
//Subclass of Staff
class Teaching extends Staff
{ //data members of Teaching class
String domain=null, publications=null;
public Teaching(String S, String N, String P, float Sa,String d,String p) {
//super invokes super class Staff constructor
super(S, N, P, Sa);
domain=d;
publications=p;
}
public void display()
{ //super invokes display super class method
super.display();
System.out.println("Domain:"+domain);
System.out.println("Publication:"+publications);
}
}
class Technical extends Staff
{ String skills=null;
public Technical(String S, String N, String P, float Sa,String Sk) {
//super invokes Staff super class constructor
super(S, N, P, Sa);
skills=Sk;
}
public void display() {
//super invokes display super class method
super.display();
System.out.println("Skills:"+skills);
}
}
class Contract extends Staff
{ float period;//in years
public Contract(String S, String N, String P, float Sa,float p) {
super(S, N, P, Sa);
period=p;
}
public void display() {
//super invokes display super class method
super.display();
System.out.println("Period:"+period);
}
}
public class StaffDemo {
public static void main(String args[])
{ Scanner s = new Scanner(System.in);
String StaffId[]=new String[3];
String Name[]=new String[3];
String Phone[]=new String[3];
float Salary[]=new float[3];
String Domain[]=new String[3];
String Publications[]=new String[3];
System.out.println("Enter the Details for Teachers");
for(int i=0;i<3;i++)
{ //reading variables of Staff
System.out.println("Enter StaffId");
StaffId[i] = s.next();
System.out.println("Enter Name");
Name[i] = s.next();
System.out.println("Enter Phone");
Phone[i] = s.next();
System.out.println("Enter Salary");
Salary[i] = s.nextFloat();
//reading variables of Teaching
System.out.println("Enter Domain");
Domain[i] = s.next();
System.out.println("Enter Publications");
Publications[i] = s.next();
}
//Dislaying variables of Teaching
Teaching T1[]=new Teaching[3];
for(int k=0;k<3;k++)
{ T1[k]=new Teaching(StaffId[k],Name[k],Phone[k],Salary[k],Domain[k],Publications[k]);
T1[k].display();
}
Scanner s2 = new Scanner(System.in);
String StaffIdT[]=new String[3];
String NameT[]=new String[3];
String PhoneT[]=new String[3];
float SalaryT[]=new float[3];
String Skills[]=new String[3];
System.out.println("Enter the Details for Technical");
for(int j=0;j<3;j++)
{ //reading variables of Staff
System.out.println("Enter StaffId");
StaffIdT[j] = s2.next();
System.out.println("Enter Name");
NameT[j] = s2.next();
System.out.println("Enter Phone");
PhoneT[j] = s2.next();
System.out.println("Enter Salary");
SalaryT[j] = s2.nextFloat();
//reading variables of Technical
System.out.println("Enter Skills");
Skills[j] = s2.next();
}
//Dislaying variables of Technical
Technical Te1[]=new Technical[3];
for(int x=0;x<3;x++)
{ Te1[x]=new Technical(StaffIdT[x],NameT[x],PhoneT[x],SalaryT[x],Skills[x]);
Te1[x].display();
}
Scanner s3 = new Scanner(System.in);
String StaffIdC[]=new String[3];
String NameC[]=new String[3];
String PhoneC[]=new String[3];
float SalaryC[]=new float[3];
float period[]=new float[3];
System.out.println("Enter the Details for Contract");
for(int k=0;k<3;k++)
{ //reading variables of Staff
System.out.println("Enter StaffId");
StaffIdC[k] = s3.next();
System.out.println("Enter Name");
NameC[k] = s3.next();
System.out.println("Enter Phone");
PhoneC[k] = s3.next();
System.out.println("Enter Salary");
SalaryC[k] = s3.nextFloat();
//reading variables of Contract
System.out.println("Enter Period");
period[k] = s3.nextFloat();
}
//Dislaying variables of Contract
Contract C1[]=new Contract[3];
for(int y=0;y<3;y++)
{
C1[y]=new Contract(StaffIdC[y],NameC[y],PhoneC[y],SalaryC[y],period[y]);
C1[y].display();
}
}
}

OUTPUT

[alert-success]

JAVA Inheritance With Concept and Examples

Output of JAVA Inheritance

[/alert-success]

Rabins Sharma Lamichhane

Rabins Sharma Lamichhane is the owner of RabinsXP who is constantly working for increasing Internet of Things (IoT) in Nepal. He also builds android apps and crafts beautiful websites. He is also working with various social services. The main aim of Lamichhane is to digitally empower the citizens of Nepal and make the world spiritually sound better both in terms of technology and personal development.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

11 − four =

%d bloggers like this: