Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with the code

 
Parul Sweetie
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. class Person{
2. String name="No Name";
3. public Person(String nm){name=nm;}
4. }
5. class Employee extends Person{
6. String id="0000";
7. public Employee(String emplId){id=emplId;}
8. }
9. public class EmployeeTest{
10. public static void main(String args[]){
11. Employee e=new Employee("4321");
12. System.out.println(e.id);
13. }
14. }


Guys please tell me what's the problem in line 7. Thanks in advance.
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried it? What error message did you get?

(Think about what constructors get called)
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Parul,

Do you have, by any chance, the study guide by Kathy Sierra and Bert Bates?

If so, please have a look at the section - "Determine whether a Default Constructor Will Be created".

Regards,
Dan
 
Himai Minh
Ranch Hand
Posts: 1249
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way you can do is:


Since Employee is a child of Person. Its contructor will call super() implicity. The super() will call the default contructor of Person. Since your original code does not have a default contructor of Person, it won't compile.
If you define a contructor with some arguments, you may want to define a non-argument contructor as well. It is because the child class may call super().
 
Parul Sweetie
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everybody....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic