Write a java program to illustrate the multilevel inheritance using database creation as an example.
Program Algorithm:
Step 1: Create a class with main function.
Step 2: Create a class ‘Staff' with member variables and setter and getter methods
Step 3: Create a class ‘typist ' which extends the class ‘staff' again with some setter methods .
Step 4: create a class ‘casual’ which extends the class ‘typist’ again with some setter methods.
Step 5: create an object of class ‘casual’ and call methods that were originally defined in the department class ( and not overridden)
Step 6: Call the getter methods to retrieve the details .
Program Code:
class Imps
{
public static void main(String args[])
{
casual s= new casual();
s.getD("Ankit","Typist");
s.getS('m');
s.prF();
}
}
class Staff
{
String na;
String c;
void getD(String n,String co)
{
na=n;
c=co;
}
void prD()
{
System.out.println("Staff Detail:\nName:"+na+"\nCode:"+c);
}
}
class typist extends Staff
{
char speedCode;
void getS(char s)
{
speedCode=s;
}
void disS()
{
prD();
System.out.println("Speed Code:"+speedCode);
}
}
class casual extends typist
{
void prF()
{
int daw=0;
switch(speedCode)
{
case 's':daw=100;
break;
case 'm':daw=300;
break;
case 'f':daw=500;
break;
default:
System.out.println("Invalid Code entered .");
}
disS();
System.out.println("Daily Wages:Rs"+daw);
}
}
Sample Inputs and Outputs:
Staff Details:
Name:XYZ
Code:Typist
SpeedCode:m
Daily Wages:Rs 300
Post A Comment:
0 comments: