• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unexpected error

 
Vinod Vinu
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have created three classes in the same package as :-
Company.java

---------------------------------------
EmpInfo.java


------------------------------
Employee.java




i ran Company.java and output was :-

Employee Code : 0
Employee Name : null

Now my question is i have assigned the values also to the fields but then why the results are 0 and null ?
i have created the object of parent class and assigned the values also. Now i have created another object of its subclass and trying to print those values. Don't you think that it should print because the parent class now has values assigned to their fields in it.

please explain me in detail as i'm a beginner and want to clear my foundation base in java very seriously.

thanks in advance.
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinod Vijay wrote:

I have added some comments in your code.
You are doing something like saving money in your bank account, and checking the balance in your friend's account.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49364
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And after being here for a year, you should know better than to write "unexpected error" as your thread title.
 
Gary Ba
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinod,
You created an instance of an Employee and EmpInfo. EmpInfo is an Employee. You populated the object of Employee but not the object of EmpInfo. You then printed empInfo which was never populated.

Maybe what needs to be done is Employee should have EmpInfo instead of inhereting from it.

Gary
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic