Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Confused with Constructor?  RSS feed

 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class called Employee.

This gets the employee id, first name and surname plus other details.

I therefore have a constructor in my Employee class:

Employee (String id, String name, String surname)
{
this.id = id;
this.name = name;
this.surname = surname;
}

In my Employee class I have additional get and set methods.

I then have a class called EmployeeDB. This extends the Employee class.

I have the constructor
EmployeeDB (String id, String name, String surname)
{
super (id, name, surname)
//using a treeMap to store the details
dbase = new TreeMap;
}

In my test program I create a new Employee i.e.
Employee record1 = new Employee ("123", "Karen", "Smith");

No problems with this.

I then create a new EmployeeDB i.e.
EmployeeDB test = new EmployeeDB ("123", "Karen", "Smith");

This is where I am getting a little confused. Why can I not do the following:

EmployeeDB test = new EmployeeDB (record1) even WHEN I create another constructor in my EmployeeDB as follows:

EmployeeDB (Employee record)
{
dbase = new TreeMap;
}

When I try this it says that the constructor is not right?
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well its hard to say without knowing the exact error message, but shouldn't there be empty brackers when you instantiate dbase?
 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

EmployeeDB (String id, String name, String surname) {
super (id, name, surname)
//using a treeMap to store the details
dbase = new TreeMap;
}

Missing semi-colon at the end of the super(...). Perhaps its a typo here, but do check your own codes.


I then have a class called EmployeeDB. This extends the Employee class.


Why do you need to subclass Employee? Are you saying that EmployeeDB IS-A Employee? Don't sounds logical & right to me? What is the EmployeeDB class
suppose to do?
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks for your responses to my thread. - Sorry about the typo's

The EmployeeDB is to store the Employee in a TreeMap!
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case, it certainly shouldn't be a subclass of Employee. If you really want a separate EmployeeDB class, it should just encapsulate a TreeMap instance and other other data relevant to the collection of all employees.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly as Stuart said, you'll want an EmployeeDB class with no superclass that has a TreeMap instance member only. It should then have methods that look a lot like those of the Set interface (add, remove, iterator, etc) that perform operations on Employees and the internal TreeMap.

I say to follow Set as I assume you'll pull a property of the Employee to use as the key in the TreeMap. If that's not the case, your EmployeeDB methods will look like TreeMap's.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!