EX No: 6 Implementation of Inheritance in Java: Procedure
EX No: 6 Implementation of Inheritance in Java: Procedure
EX No: 6 Implementation of Inheritance in Java: Procedure
AIM-
To write a set of Java programs which implements and executes the concept and
usage of inheritance and to compile and execute them.
PROCEDURE -
PROBLEM-
Write a java Program with an employee class that includes emp_name, emp_id,
address, mail_id, mobile_no as members. Inherit the classes Programmer, Assistant
Professor from employee class. Add Basic Pay as the member of all the inherited
classes with 97% of BP as DA, 10% of BP as HRA, 12% of BP as PF, 0.1% of BP for
staff club fund. Generate the pay slips for the employees with their gross and net
salary.
PROGRAM-
import java.util.*;
class Employee {
String emp_name, mail_id, address;
int emp_id;
double mob_no;
double da=0.97;
double hra=0.10;
double pf = 0.12;
double club_fund = 0.001;
void get_details (){
Scanner obj = new Scanner(System.in);
System.out.println("\nEnter Employee Name ");
emp_name = obj.nextLine();
System.out.println("\nEnter Employee Id ");
emp_id = obj.nextInt();
System.out.println("\nEnter Employee mail ");
mail_id = obj.nextLine();
obj.nextLine();
System.out.println("\nEnter Employee Address ");
address = obj.nextLine();
System.out.println("\nEnter Employee Mobile number ");
mob_no = obj.nextDouble();
}
void print_details(){
System.out.println(" Name " + emp_name);
System.out.println(" ID " + emp_id);
}
}
class Programmer extends Employee{
double bp = 20000;
double gross_salary = bp + (bp*da) + (bp*hra);
double net_salary = gross_salary - (bp*pf) - (bp*club_fund);
void print_details(){
System.out.println(" Name " + emp_name);
System.out.println(" ID " + emp_id);
System.out.println(" GROSS SALARY " + gross_salary);
System.out.println(" NET SALARY " + net_salary);
}
}
class Professor extends Employee{
double bp = 15000;
double gross_salary = bp + (bp*da) + (bp*hra);
double net_salary = gross_salary - (bp*pf) - (bp*club_fund);
void print_details(){
System.out.println(" Name " + emp_name);
System.out.println(" ID " + emp_id);
System.out.println(" GROSS SALARY " + gross_salary);
System.out.println(" NET SALARY " + net_salary);
}
}
class Main {
public static void main (String[] args) {
Scanner in = new Scanner(System.in);
Programmer e1 = new Programmer();
Professor e2 = new Professor();
e1.get_details();
e1.print_details();
e2.get_details();
e2.print_details();
}
}
OUTPUT-
Enter Employee Name
Priya
Enter Employee Id
45
Enter Employee Id
99
Result:
Thus, Java programs for the given scenarios have been compiled and executed
successfully and output was verified.
20IT304 OBJECT ORIENTED PROGRAMMING USING JAVA
a. Single Inheritance.
b. Multilevel Inheritance.
c. Hierarchical Inheritance.
1.Write a java Program with an employee class that includes emp_name, emp_id,
address, mail_id, mobile_no as members. Inherit the classes Programmer, Assistant
Professor from employee class. Add Basic Pay as the member of all the inherited
classes with 97% of BP as DA, 10% of BP as HRA, 12% of BP as PF, 0.1% of BP for
staff club fund. Generate the pay slips for the employees with their gross and net
salary.
Program Link- https://onlinegdb.com/OR_qU0XMEj
2.Write a java class called ‘student’ with name, and rollno. Write a class ‘Result’ to get
Marks of 3 subjects and another class “Sports’ to get the points obtained in sports.
Calculate the total Marks and display the result (pass or fail) with points obtained in
sports for three students using inheritance.
Program Link- https://onlinegdb.com/vUvO4CcMD
3.Write and Execute a java program using Single Inheritance to generate the bill for
the more items that need to be purchased as long as the customers enter the
departmental store to purchase.
Program Link-https://onlinegdb.com/WTEAV0p22
4.Construct a Java program to calculate the Electricity Bill using multiple Inheritance.
Program Link- https://onlinegdb.com/TiArSvpj5