How use Inheritance in Java

public class Coin
{
   protected  final int HEADS = 0;
   protected final int TAILS = 1;

   protected int face;

   
   public Coin ()
   {
      flip();
   }

 
   public void flip ()
   {
      face = (int) (Math.random() * 2);
   }

   
   public boolean isHeads ()
   {
      return (face == HEADS);
   }

   
   public String toString()
   {
      String faceName;

      if (face == HEADS)
         faceName = "Heads";
      else
         faceName = "Tails";

      return faceName;
   }
}

HI
I am stoke in this place I want to derived from the coin class I need store a value in the monetary coin that represents its value and add a method that returns its value
Ansary100Asked:
Who is Participating?
 
InteractiveMindConnect With a Mentor Commented:
Something like this, do you mean?


public class MonetaryCoin extends Coin
{
    public MonetaryCoin()
    {
        super();
    }
   
    public int getValue()
    {
        return face;
    }
   
    public String getValue()
    {
        return toString();
    }
}


You can then use it like this:

    MonetaryCoin mc = new MonetaryCoin();        // Instantiate MonetaryCoin class.
    int value = mc.getValue();                             // Get value as an integer.
    String valueAsString = mc.getValue();            // Get value as a String (either "Heads" or "Tails").


HTH
0
 
InteractiveMindCommented:
Hi Ansary100,

I'm not completely clear on what you're trying to do here; what do you mean exactly by:

   "I need store a value in the monetary coin that represents its value"

What is "monetary" coin; and what "value" are you referring to?

If you could demonstrate what you've tried so far, and perhaps elaborate a little bit, then I'm sure that I can help.

Regards;
0
 
Ansary100Author Commented:
Thank you for help and I hope now is clear for you more
I am trying to design and implement a class called MonetaryCoin that is derived from the Coin class and I need to store a value in the monetary coin that represents its value and add a method that returns its value  
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
InteractiveMindCommented:
There's no need to store the value in the MonetaryCoin class, as you originally suggested, as you're extending the Coin class, you're inheriting the Coin class's value.  :-)
0
 
InteractiveMindCommented:
Glad it helped  :-)
0
 
Ansary100Author Commented:
I have question for you so you said that i don't need to Write

private  int value = 25;

befor the constructor
0
 
InteractiveMindCommented:
Oh... the "value" that you speak of, is it the value of the coin? Or the "Heads" or "Tails" choice?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.