PDA

Click to See Complete Forum and Search --> : [RESOLVED] (was being dumb) - Type casting a class?


Tool
Oct 8th, 2005, 03:34 PM
Is there a way in Java to type cast an array of a super class into the subclass?

Example:

I have a class person, account and checking. Account inherits from person, checking inherits from account. I am trying to make a little application to add/search through accounts created. I was going to make an array of the person class since it is the base class of the other 2, and then I was going to typecast each instance the way I needed to....

if(people[0].getType()=="account")
(account)people[0].AccountFunction();


That type of thing. I get an error on the first ( before account saying expected statement.

Any help would be appreciated

System_Error
Oct 8th, 2005, 04:46 PM
I believe that your architecture might be wrong.
Instead of the account class inheriting from person, you should use aggregation.

For instance, the person class has-an account, instead of the account class deriving from person.

You need to have person as a seperate class, but extend, or implement the functionality of account in person. If you don't want to extend or implement, then you need to make it a class variable:

public class Person
{
private Account myAccount;
}

Tool
Oct 8th, 2005, 05:22 PM
If I use implements, I will still have a basic account and checking account class that need to be in the array, so I am guessing I will still need some sort of type casting. I guess, possibly, I could make an abstract superclass for the account and checking classes that implements person, then just use the abstract class as the array. But, I guess, it just seems like a lot more work than it has to be, but I am not too familiar with Java so I have no idea. Either way though, is there typecasting in Java to user defined classes? or only the ones Java includes, such as int, String, double etc...?

System_Error
Oct 8th, 2005, 06:32 PM
No, you can type cast objects and everything else. What you are wanting is called downcasting, and it only works on select cases. It's not guranteed to work, and if you want to do it then I would say somethings wrong with the architecture. Persionally, I would make the person class seperate of the other two, and then make the account class a member of the person class.

Tool
Oct 9th, 2005, 01:21 PM
I think I might be confused. If all people will not have an account wouldn't it be better not to make all people have the account class as a member? I am not too sure what downcasting is, I have not heard of that before, but I will look into it. Another question, if you, lets say, are making a game. You have a default object super class that contains all the information each object for sure will need, then you have a bunch of other classes that inherit from it. Then you want to make an array of some sort to hold all object to do your colision detection and add/delete new objects as necessary. That is considered something wrong with architecture? I have seen a few people use that before, and was somewhat what I was hoping to accomplish. If that is not the best method to go about doing it, I would be happy to figure out new methods. OOP still confuses me a bit. Thank you for your time, and replies.

System_Error
Oct 10th, 2005, 02:39 PM
It depends on what your doing....
An account shouldn't be at the top of the hiearchy over a person.
What should person inherit from an account? In this case, you want to use a has-a relationship:

public class Person
{
Account a;
}


A person has-a account.


oopps, sorry. I just re read your post and you said account inheirants from person, but I still don't think that's the way to go. Just make account a member of person like I showed.

Tool
Oct 12th, 2005, 02:22 PM
Oh, I appologize, I must have worded it wrong.

Person is the highest class which account inherits from.

Person is the super class, Account is a subclass and then checking is a subclass of Account.

Person holds all the info of the person the account will need, Account holds all the information the checking will need.

I am making an array of the Person class because it will contain instances of Person, Account and Checking. I hope that clarifies it a bit.

Edit: If I do it that way will each person need to have an account and checking member? Some people might not have either so I assumed it would be sort of a waste to do that, but I am not sure.

Andy_Hollywood
Oct 13th, 2005, 04:56 AM
I'm with system error on this one, your inheritence dosen't make sense, unless i'm missing something.

Account shouldn;t inherit from person, person should hold n accounts. Account should become abstract, then you can special case each of your different accounts.

What are you actually trying to write? some kind of banking system?

Tool
Oct 13th, 2005, 05:53 PM
Haha, I feel retarded sorry. I haven't been sleeping much or something. I am in a Java class, and one of the things my teacher wanted me to do was write an account manager type program. I over-thought the whole thing and made it a lot harder than it had to be, and really screwed everything up. Sorry for the inconvenience, and thank you both for all your replies. I was completely wrong on all this. Thanks again, I appreciate all the help.