[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How use Inheritance in Java

Posted on 2005-04-25
7
Medium Priority
?
723 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:Ansary100
  • 5
  • 2
7 Comments
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13859521
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
 

Author Comment

by:Ansary100
ID: 13859685
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
 
LVL 25

Accepted Solution

by:
InteractiveMind earned 2000 total points
ID: 13859827
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13859845
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
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13859882
Glad it helped  :-)
0
 

Author Comment

by:Ansary100
ID: 13860018
I have question for you so you said that i don't need to Write

private  int value = 25;

befor the constructor
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13860240
Oh... the "value" that you speak of, is it the value of the coin? Or the "Heads" or "Tails" choice?
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Six Sigma Control Plans
Loops Section Overview

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question