Reputation: 15
I am new to java and creating a small program to clear with the basic fundamentals of the core java. I am creating a program of employee basic information and salary count. But when I am trying to access the parent variable so that I can use the variable and calculate the salary of the employee, but I am getting an error message that "empSalaryobj cannot be resolved to a variable".
Below is my Java Code,
class varEmployee {
int empId;
String empName;
final String empCompany = "Tata Consultancy Services";
int empSalary;
String empGender;
int empAge;
String empNationality;
}
class setEmployee extends varEmployee{
setEmployee(){
}
setEmployee(int empId){
this.empId=empId;
switch(empId){
case 1:
empName = "Aditya Batra";
empSalary = 10000;
empGender = "Male";
empAge = 26;
empNationality = "Indian";
break;
case 2:
empName = "Vishal Sharma";
empSalary = 20500;
empGender = "Male";
empAge = 26;
empNationality = "Australia";
break;
case 3:
empName = "Ashish Tiwari";
empSalary = 30000;
empGender = "Male";
empAge = 28;
empNationality = "Indian";
break;
case 4:
empName = "Mona Lisha";
empSalary = 40000;
empGender = "Female";
empAge = 24;
empNationality = "Oganda";
break;
default:
System.out.println("Invalid Employee ID");
}
}
}
class getEmployee extends setEmployee {
getEmployee(){
}
getEmployee(setEmployee obj){
int empIdobj = obj.empId;
String empNameobj = obj.empName;
int empSalaryobj = obj.empSalary;
String empGenderobj = obj.empGender;
int empAgeobj = obj.empAge;
String empNationalityobj = obj.empNationality;
System.out.println("Employee Id: "+empIdobj+"\nName: "
+empNameobj+"\nSalary: "+empSalaryobj
+"\nGender: "+empGenderobj+"\nAge: "
+empAgeobj+"\nNationality: "+empNationalityobj
+"\nCompany Name: "+empCompany);
}
}
class getEmployeeSalary extends getEmployee {
int empSalaryCaculation = empSalaryobj;
getEmployeeSalary(int empLeaves){
float empSal = (empSalaryCaculation/30)*(30-empLeaves);
System.out.println("Salary after leave deduction: "+empSal);
}
}
public class Employee {
public static void main(String[] args) {
setEmployee setobj=new setEmployee(1);
getEmployee getobj=new getEmployee(setobj);
getEmployeeSalary salobj=new getEmployeeSalary(5);
}
}
Can someone please help me out. I also want to know why this error message for.
Upvotes: 1
Views: 2623
Reputation: 590
you need to have a constructor for your getEmployeeSalary class
getEmployeeSalary(int empLeaves,getEmployee employee)
{
float empSal = (employee.empSalaryobj/30)*(30-empLeaves);
System.out.println("Salary after leave deduction: "+empSal);
}
and you need not to have int empSalaryCaculation = empSalaryobj;
because you directly implement it inside the getEmployeeSalary constructor
also you need to make the empSalary a datafields of class getEmployee
public class getEmployee extends varEmployee
{
public int empSalaryobj;
}
here I use Eric Allman indention style because it is better than K&R style in any debate
Upvotes: 0
Reputation: 179
empSalaryobj
is a local variable defined in the constructor of getEmployee
. It is not a field of any of the parent classes of getEmployeeSalary
, due to which it can't be accessed in the constructor of getEmployeeSalary
.
Upvotes: 1