File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problem with the code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Problem with the code" Watch "Problem with the code" New topic

Problem with the code

Parul Sweetie

Joined: Jan 17, 2013
Posts: 16
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(;
13. }
14. }

Guys please tell me what's the problem in line 7. Thanks in advance.
Matthew Brown

Joined: Apr 06, 2010
Posts: 4543

Have you tried it? What error message did you get?

(Think about what constructors get called)
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1183
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".


William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 1120
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

Joined: Jan 17, 2013
Posts: 16
Thanks everybody....
I agree. Here's the link:
subject: Problem with the code
It's not a secret anymore!