Program to implement the Multiple Inheritance (Gross Interface, Employee & Salary classes)
In this program, we have achieved multiple inheritance using interface. We have created an interface Gross having data members ta, da and method gross_sal( ). After that class Employee is created which has data members name, basic_sal and method display( ). Finally we declare the Salary class having data member hra and method disp( ). In that we have extended Employee class and implemented Grossinterface as shown in program.
PROGRAM
PROGRAM
import java.lang.*; // import language package
interface Gross // interface declaration
{
double ta=800.0;
double da=1500.0; // final variable
void gross_sal();
}
class Employee // super class declaration
{
String name; // data member declaration
float basic_sal;
Employee(String n, float b) // super class constructor
{
name=n;
basic_sal=b;
}
void display() // method to display value of data member
{
System.out.println("Name of Employee : "+name);
System.out.println("Basic Salary of Employee : "+basic_sal);
}
}
class Salary extends Employee implements Gross // sub class constructor
{
float hra;
Salary(String n, float b, float h) // sub class constructor
{
super(n,b);
hra=h;
}
void disp()
{
display();
System.out.println("HRA of Employee : "+hra);
}
public void gross_sal() // method to calculate gross salary
{
double gross_sal=basic_sal + ta + da + hra;
System.out.println("TA of Employee : "+ta);
System.out.println("DA of Employee : "+da);
System.out.println("Gross Salary of Employee : "+gross_sal);
}
}
public class EmpDetails {
public static void main(String args[]) // main method
{
Salary s = new Salary("Sachin",8000,3000); // initiating Sub class
s.disp(); // invoke method of sub class
s.gross_sal();
}
}
OUTPUT
C:\>javac EmpDetails.java C:\>java EmpDetails Name of Employee : Sachin Basic Salary of Employee : 8000.0 HRA of Employee : 3000.0 TA of Employee : 800.0 DA of Employee : 1500.0 Gross Salary of Employee : 13300.0