![]() |
| Polymorphism - interface |
interface access
{
public void input();
public void calc();
public void printop();
}
class student implements access
{
int tot; String stu=new String();String name=new String();
String grade=new String();
public void input()
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.println("Enter your name\n");
name=br.readLine();
System.out.println("Enter your total \n");
stu=br.readLine();
tot=Integer.parseInt(stu);
}
catch(Exception e)
{System.out.println("Student details are not given in correct format\n");}
}
public void calc()
{
if (tot>80)
{grade="A++";}
else if (tot>70)
{grade="A+";}
else if (tot>60)
{grade="A";}
else
grade="NIL";
}
public void printop()
{
System.out.println("Name of the student is : \n"+name);
System.out.println("Total Mark is \t " + tot +"grade is \n"+ grade);
}
}
class empl implements access
{
int sal,tax;String emp=new String();String name=new String();
public void input()
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.println("Enter Employee name\n");
name=br.readLine();
System.out.println("Enter your total income \n");
emp=br.readLine();
sal=Integer.parseInt(emp);
}
catch(Exception e)
{System.out.println("Enter your correct details\n");}
}
public void calc()
{
if (sal>500000)
{tax=10000;}
else if (sal>400000)
{tax=9000;}
else if (sal>300000)
{tax=8000;}
else
{tax=5000;}
}
public void printop()
{System.out.println("Name of the Employee is : \n"+name);
System.out.println("Total Income is \t " + sal +"TAX is \n"+ tax);
}
}
public class polyinterface
{
public static void main(String[] args)
{
// creating object for interface
access poly1;
// Printing Student details
student stud1=new student();
// polymorphism used
poly1=stud1;
poly1.input();
poly1.calc();
poly1.printop();
// printing Employee details
empl emp1=new empl();
//polymorphism used
poly1=emp1;
poly1.input();
poly1.calc();
poly1.printop();
}
}

No comments:
Post a Comment