Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Overriding Object methods

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    63
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Overriding Object methods

    I've come accross a question that I just can't get my head around!

    Question: Consider the 'Card' class you wrote in the previous exercise. What Object methods should each of these classes override?

    The 'Card' class:
    public class Card2 {
        private int rank;
        private int suit;
     
        public final static int DIAMONDS = 1;
        public final static int CLUBS = 2;
        public final static int HEARTS = 3;
        public final static int SPADES = 4;
     
        public final static int ACE = 1;
        public final static int DEUCE = 2;
        public final static int THREE = 3;
        public final static int FOUR = 4;
        public final static int FIVE = 5;
        public final static int SIX = 6;
        public final static int SEVEN = 7;
        public final static int EIGHT = 8;
        public final static int NINE = 9;
        public final static int TEN = 10;
        public final static int JACK = 11;
        public final static int QUEEN = 12;
        public final static int KING = 13;
     
        public Card2(int rank, int suit) {
            this.rank = rank;
            this.suit = suit;
        }
     
        public int getSuit() {
            return suit;
        }
     
        public int getRank() {
            return rank;
        }
     
        public static boolean isValidRank(int rank) {
            return ACE <= rank && rank <= KING;
        }
     
        public static boolean isValidSuit(int suit) {
            return DIAMONDS <= suit && suit <= SPADES;
        }
     
        public static String rankToString(int rank) {
            switch (rank) {
                case ACE:
                    return "Ace";
                case DEUCE:
                    return "Deuce";
                case THREE:
                    return "Three";
                case FOUR: 
                    return "Four";
                case FIVE:
                    return "Five";
                case SIX:
                    return "Six";
                case SEVEN:
                    return "Seven";
                case EIGHT:
                    return "Eight";
                case NINE:
                    return "Nine";
                case TEN:
                    return "Ten";
                case JACK:
                    return "Jack";
                case QUEEN:
                    return "Queen";
                case KING:
                    return "King";
                default:
                    //Handle an illegal argument.  There are generally two ways
                    //to handle invalid arguments, throwing an exception (see
                    //the section on Handling Exceptions):
                    throw new IllegalArgumentException("Invalid rank " + rank);
                    //or
                    //return null;
            }
        }
     
        public static String suitToString(int suit) {
            String result = "";
            switch (suit) {
                case DIAMONDS:
                    return "Diamonds";
                case CLUBS:
                    return "Clubs";
                case HEARTS:
                    return "Hearts";
                case SPADES:
                    return "Spades";
                default:
                    throw new IllegalArgumentException("Invalid suit " + suit);
            }
        }
     
        public static void main(String... args) {
            new Card2(ACE, DIAMONDS);
            new Card2(KING, SPADES);
        }
    }

    The answer: Card should override equals, hashCode, and toString.

    The answer code provided is identical to the above with the adddition of the following:
        public boolean equals(Object obj) {
            if (obj instanceof Card2) {
                if (((Card2)obj).getRank() == this.rank &&
                    ((Card2)obj).getSuit() == this.suit) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
     
        public int hashCode() {
            return ((suit-1)*13)+rank;
        }
     
        public String toString() {
            return rankToString(this.rank) + " of "
                   + suitToString(this.suit);
        }

    I understand what the code is doing but I don't understand why! Why would you add this in? For a moment I thought the question should have read "...What Object methods COULD each of these classes override?". But I can't beleive that Oracle would have miss-worded the question and if it had read 'COULD' then I would have thought the finalize() should be in there as well. I can understand that IF I wanted to compare Objects then it would make sence to override the equals() which in turn requires the hashCode() to be overriden (not sure about the toString()). I have read the Oracle page regaring this (Object as a Superclass (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)) over and over again but I just don't get it. Any help would be massively appreaciated.


  2. #2
    Junior Member
    Join Date
    Nov 2011
    Location
    Wales
    Posts
    4
    My Mood
    Cynical
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Overriding Object methods

    Hi

    Those methods come from the object superclass. You dont HAVE to override them but overriding them allows you to define how these methods should operate for your given object. If they arent overrided then they will use the default versions of those methods. For example using the default toString returns the hash of that object. By overriding it you have turned it into something useful for your particular object.

    Hope this helps

  3. The Following User Says Thank You to kerry2279 For This Useful Post:

    tarkal (November 13th, 2011)

  4. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Overriding Object methods

    kerry has well described the example but let me ask you something.
    1. Do you understand the concept of Polymorphism?
    2. Do you understand the concept of Inheritance?

    If yes, then what is the problem?
    If no, please refer to these concepts first. Read and practice them carefully and you will get to know the answer.

Similar Threads

  1. Newb question: Using the same object over two methods?
    By CitizenSmif in forum Object Oriented Programming
    Replies: 5
    Last Post: July 3rd, 2011, 08:17 PM
  2. Overriding Methods
    By tcstcs in forum Java Theory & Questions
    Replies: 5
    Last Post: June 2nd, 2011, 04:23 AM
  3. Inheritance and Overriding help!
    By Knserbrave in forum Object Oriented Programming
    Replies: 4
    Last Post: February 24th, 2011, 01:46 PM
  4. [SOLVED] Accessing methods of an object inside in ArrayList
    By jmack in forum Java Theory & Questions
    Replies: 3
    Last Post: January 22nd, 2011, 12:28 PM
  5. Overriding methods declaration problems
    By TurboTuring in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 9th, 2010, 11:23 AM