Are we throwing objects or links?
My code looks like this:
public class Hashtabledemo2 {
public static void main(String[] args) {
Hashtable myCompay = new Hashtable(10);
System.out.println("Add some employee objects to the hashtable..");
Salary e1 = new Salary("Salary1", "Palo Alto, CA",
1, 100000.00);
Hourly e2 = new Hourly("Hourly2", "Cupertino, CA", 2, 100.00);
Contractor e3 = new Contractor("Contractor3", "Milpitas, CA",3, 1000.00);
myCompay.put(new Integer(e1.hashCode()), e1);
myCompay.put(new Integer(e2.hashCode()), e2);
myCompay.put(new Integer(e3.hashCode()), e2);
System.out.println("The size of the hashtable is "+myCompay.size());
int size = myCompay.size();
for(int i=1;i<=size;i++){
/* Note that the get() method of the hashtable class returns a reference to the object
that matches the given key:
public Object get(Object key)
*/
Employee current = (Employee) myCompay.get(new Integer(i));
if(current instanceof Hourly){
((Hourly) current).setHoursWorked(40);
} else if(current instanceof Contractor){
((Contractor) current ).setDaysWorked(5);
}
current.computePay();
current.mailCheck();
}
Wages, hourly and contractor expand the class of employees. In my understanding, we are discarding parent links to child links, and not vice versa. I don't understand how this line of code works:
Employee current = (Employee) myCompay.get(new Integer(i));
this line of code is used to get the object stored in position, one of the hash tables, which is the Salary object.
myCompay.get(new Integer(i));
After that it will be passed to Employee:
(Employee) myCompay.get(new Integer(i));
and then assigned to the employee links to the current:
Employee current = (Employee) myCompay.get(new Integer(i));
Can someone please explain to me if we will put the object salary stored in the hash table on the employee object and then assign it to the employee.or reference, we are giving the e1 reference to the employee reference. Someone explains what's going on.
source to share
The difference between objects and references in Java is somewhat blurry because the only way to access an object is through a reference. When you are casting, you are making different types of reference to the same object.
Note that your code is broken: it puts the objects into a hash table using their raw hash codes, but then tries to reconstruct them using their ordinal numbers. This is not expected to work unless you're lucky and one of the objects returns a hash code in the range 0..3.
source to share
When casting, you only change the link type, the object behind it will remain unchanged.
You can use Contractor
for Employee
(but this is not necessary as you can already access everything from Employee
), You can also use Employee
for Contractor
, then you can access Contractor
-Methods and fields (only if the object is for Employee
is Contractor
, otherwise you will get an exception ).
You cannot apply Contractor
to Hourly
as they do not extend each other. Casting Contractor
at Employee
and then at Hourly
will throw an exception as the object behind it still remainsContractor
source to share
You have several misconceptions: -
Wages, hourly and contractor expand the class of employees.
It's not like inheritance. It Contractor
can only be considered a type Employee
. Salary
and Hourly
must be properties Employee
(Search Composition ).
Also, as dasblinkenlight stated, your search is also misunderstood as the key is looked up based on its hash. Search hashcode
and equals
.
As for your actual question. As you already understood, get
returns Object
from this raw Hashtable
. You cannot assign an instance of a type object to a type Object
reference Employee
, as this will allow you to invoke Employee
behavior (methods) on that instance, and the class Object
knows nothing about what it Employee
can and cannot. So the cast to ( Employee
) is actually a check on the actual type of the object being returned get
to see if it can be legally assigned a reference Employee
.
However, you shouldn't use raw types with Java 5. Generics gives you type safety. Search Generics and why you shouldn't use raw types .
source to share