• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with the code

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried it? What error message did you get?

(Think about what constructors get called)
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2419
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everybody....
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic