# My First Programming Question (Java)



## spy2520 (Oct 28, 2009)

Ok guys, this isnt for school. I'm just bored and and messing with code.

I have a card class, i have a face card class that extends card.

If i have an array of type card that also contains face cards (its a deck of cards), can i access the a parameters that are exclusive to the face card class in the array?


----------



## spy2520 (Oct 28, 2009)

i just solved my problem another way, but i'd still like to hear different answers.

Just in case mine is more than necessary.


----------



## Kreij (Oct 28, 2009)

Someone will be along soon with more Java experience than me.
My suggestion is that you post the code that worked for you, and let everyone give their input as to how you did it and any ideas on improving it.
That way not only will you get suggestions, but other people can peruse the code in case they want to do something similar.


----------



## FordGT90Concept (Oct 29, 2009)

You have to change the access modifiers on properties in the Face class to "protected" if you want an inherited class to have access to it.  "private" will hide it from everything except itself.

@Kreig: It's the same as C#. XD


----------



## spy2520 (Oct 29, 2009)

FordGT90Concept said:


> You have to change the access modifiers on properties in the Face class to "protected" if you want an inherited class to have access to it.  "private" will hide it from everything except itself.
> 
> @Kreig: It's the same as C#. XD



I'll post the code in a bit. But what i'm looking at is "public enum faces {king, queen, jack, ace}" and subsequently "public faces face;" as part of FaceCard. Are you saying i should use protected instead of public?


----------



## FordGT90Concept (Oct 29, 2009)

Enums are static/const.  In order to access an Enumerator, you have to go through the class name.  For example, if Faces is in the class "Enumerators", you'd have to access Faces via "Enumerators.Faces".  You can access it directly from inside of the Enumerators class.  For example, "Enumorators.MyMethod()" could access Faces just by using "Faces".


If your array is accessible, you can access everything that's accessible about it including the items it contains.


----------



## spy2520 (Oct 29, 2009)

I'm currently using FaceCard.Faces. But if i wanted to do something as simple as print the array, how could i print the face, suit, and value of every card considering that only 16 cards will have a face. It is easy with corresponding toString() methods, but if i wanted to do anything more than print...i just gotta stare at it for another hour and everything will make sense. Thats how it usually works.


----------



## FordGT90Concept (Oct 29, 2009)

You should have a class "Card" which defines the value of the card, the suit of the card, and any other attributes.  In "Card" override ".toString()" to output the Card how you want it to be displayed.


I think you use this to override it:

```
public override string toString()
{
}
```


----------



## Kreij (Oct 29, 2009)

No. Make the "Card" class Protected so that only derived classes (eg. FaceCards) have access to it. Depending on what you are doing, you may want to derive classes for the face cards from the base card class (eg. public class King extends Card) and put the properties and methods in there for the "King" card.

When you are creating classes, each subclass should build upon the presceding base class. So you would want a card class that definds everything for a card, then perhaps a face card class that defines the things common to all face cards, and then a class for each individual face card.


----------



## spy2520 (Oct 29, 2009)

As far as this program goes, all face cards have the same attributes besides the face. Except the ace of course.


----------



## Kreij (Oct 29, 2009)

That's fine, so you have your card base class which has thing common to all cards.
You have a face class which is derived from card, which has what facce cards have in common.
You have a regular card class, and an Ace classs derived from cards which define their specific properties and methods.


----------



## spy2520 (Oct 29, 2009)

i see. Thats alot of classes. So if done properly, i can declare an array of cards that could hold all these types?

As in a CardDeck class that contains an array of 52 cards.


----------



## Kreij (Oct 29, 2009)

You can use any type of collection object (array, List, etc.) to hold the 52 cards.
What game are you thinking about? That can make a difference how many classes you need.


----------



## spy2520 (Oct 29, 2009)

blackjack for instance. simple as can be.


----------

