hashmap

Ø

Øyvind

I have a hashmap.

The class 'Person' has two subclasses named 'Man' and 'Woman'. If I put for
example an instance of Woman into the hashmap. --> myhashmap.put(key,woman)
and get it later by using Person person = (Person) myhashmap.get(key)


will I then get the data for Woman too, or do I only get the data for
Person?
 
J

Joona I Palaste

Øyvind said:
I have a hashmap.
The class 'Person' has two subclasses named 'Man' and 'Woman'. If I put for
example an instance of Woman into the hashmap. --> myhashmap.put(key,woman)
and get it later by using Person person = (Person) myhashmap.get(key)
will I then get the data for Woman too, or do I only get the data for
Person?

What you will get into the variable person is a Woman object, the same
Woman object you originally put into the hashmap. However you can only
access it with person through the Person class. If you want to you can
use Woman woman = (Woman)person and then you can use the object through
the Woman class.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Remember: There are only three kinds of people - those who can count and those
who can't."
- Vampyra
 
I

Ian Pilcher

Øyvind said:
I have a hashmap.

The class 'Person' has two subclasses named 'Man' and 'Woman'. If I put for
example an instance of Woman into the hashmap. --> myhashmap.put(key,woman)
and get it later by using Person person = (Person) myhashmap.get(key)


will I then get the data for Woman too, or do I only get the data for
Person?

You will get the data for Woman, but you'll have to use a variable of
type Woman to access it. If you have a Person that's a Woman, you can
use a cast, 'Woman woman = (Woman)person;'. You have to be careful,
though; this will throw a ClassCastException if person is actually a
Man.
 
N

Noel

Øyvind said:
I have a hashmap.

The class 'Person' has two subclasses named 'Man' and 'Woman'. If I put for
example an instance of Woman into the hashmap. --> myhashmap.put(key,woman)
and get it later by using Person person = (Person) myhashmap.get(key)


will I then get the data for Woman too, or do I only get the data for
Person?
Yes, but in order to use it, you'll have to cast to a Woman.
 
B

BarryNL

Øyvind said:
I have a hashmap.

The class 'Person' has two subclasses named 'Man' and 'Woman'. If I put for
example an instance of Woman into the hashmap. --> myhashmap.put(key,woman)
and get it later by using Person person = (Person) myhashmap.get(key)


will I then get the data for Woman too, or do I only get the data for
Person?

You will still have a Woman object but only cast to a Person, if you
want to call any woman specific methods you will need to cast further, eg:

((Woman) person).queryDoesBumLookBig();
 
T

TT \(Tom Tempelaere\)

Øyvind said:
I have a hashmap.

The class 'Person' has two subclasses named 'Man' and 'Woman'. If I put for
example an instance of Woman into the hashmap. --> myhashmap.put(key,woman)
and get it later by using Person person = (Person) myhashmap.get(key)


will I then get the data for Woman too, or do I only get the data for
Person?

A bit off-topic, but wouldn't gender be a property of your class? Agreed,
man & women differ from a person's point of view, but usually little from a
software point of view.

Tom.
 
T

Tris Orendorff

((Woman) person).queryDoesBumLookBig();

From experience, the correct reply to this is, "return false;"



--
-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GCS d++ s+:- a+ C+ UL++++ P+ L+ E- W+ N++ o- K++ w+ O+ M !V PS+ PE Y+ PGP t+ !5 X- R- tv--- b++
DI++ D+ G++ e++ h---- r+++ y+++
------END GEEK CODE BLOCK------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top