wood burning stoves 2.0*
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
Author

Problem with the code

Parul Sweetie
Greenhorn

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(e.id);
13. }
14. }


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

Joined: Apr 06, 2010
Posts: 4343
    
    8

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: 1167
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


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: 722
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

Joined: Jan 17, 2013
Posts: 16
Thanks everybody....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with the code
 
Similar Threads
Invalid composite primary key specification
Runtime Error (ArrayIndexOutOfBoundsException)
@ManyToOne mapping question
Stuck on calc and return
what is the error in this program