Results 1 to 11 of 11
Like Tree4Likes
  • 1 Post By Junky
  • 1 Post By Junky
  • 1 Post By Junky
  • 1 Post By sunde887

Thread: What is an interface for, How you compare objects? and Why use getters and setters

  1. #1
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default What is an interface for, How you compare objects? and Why use getters and setters

    Hi, I have 3 questions from the book "Java Software Solutions". Any help greatly appreciated. Here they are.

    1- page 317 Interfaces

    Why do we need an interface? In the following code, the Interface is declared with getter and setter methods. They are just names of the methods and the parameters, no body. The "Question" class has the body. I was wondering, If you declare the getter and setter methods in the "Question" class, why do you need the Interface? Why not just forget about using the name "interface" and its declaration, and just go ahead and use the getter and setter methods?

    2-Also, why not just set a variable directly instead of using getter and setter methods?

    3-Instead of variables, comparing objects? How do you compare objects such as the statement rabbit1<rabbit2? (obj1)<(obj2) instead of if (i!=x).

    thanks. Derek:D

    Java Code:
    //********************************************************************
    //  Complexity.java       Author: Lewis/Loftus
    //
    //  Represents the interface for an object that can be assigned an
    //  explicit complexity.
    //********************************************************************
    
    public interface Complexity
    {
       public void setComplexity (int complexity);
       public int getComplexity();
    }
    Java Code:
    //********************************************************************
    //  Question.java       Author: Lewis/Loftus
    //
    //  Represents a question (and its answer).
    //********************************************************************
    
    public class Question implements Complexity
    {
       private String question, answer;
       private int complexityLevel;
    
       //-----------------------------------------------------------------
       //  Constructor: Sets up the question with a default complexity.
       //-----------------------------------------------------------------
       public Question (String query, String result)
       {
          question = query;
          answer = result;
          complexityLevel = 1;
       }
    
       //-----------------------------------------------------------------
       //  Sets the complexity level for this question.
       //-----------------------------------------------------------------
       public void setComplexity (int level)
       {
          complexityLevel = level;
       }
    
       //-----------------------------------------------------------------
       //  Returns the complexity level for this question.
       //-----------------------------------------------------------------
       public int getComplexity()
       {
          return complexityLevel;
       }
    
       //-----------------------------------------------------------------
       //  Returns the question.
       //-----------------------------------------------------------------
       public String getQuestion()
       {
          return question;
       }
    
       //-----------------------------------------------------------------
       //  Returns the answer to this question.
       //-----------------------------------------------------------------
       public String getAnswer()
       {
          return answer;
       }
    
       //-----------------------------------------------------------------
       //  Returns true if the candidate answer matches the answer.
       //-----------------------------------------------------------------
       public boolean answerCorrect (String candidateAnswer)
       {
          return answer.equals(candidateAnswer);
       }
    
       //-----------------------------------------------------------------
       //  Returns this question (and its answer) as a string.
       //-----------------------------------------------------------------
       public String toString()
       {
          return question + "\n" + answer;
       }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    I'll answer #3 first which will lead to #1

    #3 You can compare objects by having your class implement the Comparable interface and the compareTo method.

    #1 Interfaces leave the implementation upto the programmer. If you write a Person class then the compareTo method will behave differently than the compareTo method in my Vehicle class. Another example is the ActionListener interface which can be attached to a JButton. You can write a program which will have completley different behaviour to my program when a button is pushed.

    #2 It is common practice to declare instance variables as private which means they can only be accessed via getter and setter methods. The main advantage to this is that the programmer can control what values a variable can have. Consider a class that has a public varaible than MUST always be above 0. What do you think will happen if someone writes another class that sets the variable to a negative number. Using a setter method instead allows you to check if the new value is acceptable before changing the variable.

    Of course there are more answers to your questions that other may wish to add.
    silverglade likes this.

  3. #3
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    Thank you Junky. I don't understand why we need to add the "Complexity" interface in the program above, when we could have just given the definitions of the getters and setters in the "Question" class without the implementation of "Complexity". I don't think I got your explanation about the interfaces, but I got your answers for the other 2. Thank You!! Derek

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Imagine this was for a Trivia type game. The authors have written it so that it allows other programmers to add their own "Question" class. Now the original owners will have no idea how you, me, Fred or Barney will implement our question class but as long as they all implement the Complexity class then they will all still work with the rest of the Trivia game. Also as I said it allows each programmer to derive their own interpretation of what Complexity means.
    silverglade likes this.

  5. #5
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    thank you. So that means the trivia game can have multiple "Question" classes of that name?

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    No, you can not have more than one class with the same name but you can have many classes that implement the Complexity interface.
    silverglade likes this.

  7. #7
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    Ok I hope I got it right this time. Basically is it like this? You use an interface when you want to have one class that can be implemented by many other classes. Is that right? Like this.... Complexity interface ->class 1 || Complexity interface->class 2, etc. Sorry, I hope I got it right this time. Also I liked your "Fred and Barney" Flinstones thing. LOL!

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The complexity example ist necessarily a great area to use an interface, it's meant to show you a general idea.

    An interface is something any class can implement, and all an interface should contain is method signatures(no bodies), a class can extend one single class(single inheritance), but it can implement as many interfaces as it likes.

    Any class that implements an interface must have all the proper methods implemented(for each method in the interface t should define the method in the class). With interfaces different people can implement a general idea differently to fit there class. Let's say you have a question class which implements complexity, it simply keeps track of complexity based on a number, however; someone else can create a job class which also has a complexity implementation. You would implement the interface, but define the methods differently.

    A good example of interfaces is the comparable interface, which is used when a class should have a natural ordering. You may want one class A to be ordered differently than B, you could either create a method which is specific to the class, or simply implement the comparable interface and define the method for the specific classes.

    Understanding interfaces can be a bit challenging, but when used correctly can be quite powerful.
    Last edited by sunde887; 05-25-2011 at 09:01 AM.
    silverglade likes this.

  9. #9
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    holy cow sunde887, lol. you are my savior. that was a really good explanation. I understand it now. Both explanations were good. Thank you Junky and Sunde887. I wrote down what you both said summarized in my book. Onward I go!! ;);):cool:

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You are welcome, glad to have helped.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,178
    Rep Power
    20

    Default

    Yes, that's really not a great example.
    The idea of an interface that merely defines a get and set is just wrong.
    I'm sure the authors could have come up with a better one.

Similar Threads

  1. Confused about getters and setters
    By jnjh in forum New To Java
    Replies: 8
    Last Post: 05-03-2011, 08:12 AM
  2. Setters and Getters and the counter exercise
    By gnng in forum New To Java
    Replies: 8
    Last Post: 03-27-2011, 09:48 PM
  3. Positions and values, getters, setters
    By Malus in forum New To Java
    Replies: 10
    Last Post: 01-23-2010, 06:55 PM
  4. Getters and Setters
    By lheviathan in forum New To Java
    Replies: 4
    Last Post: 11-02-2009, 02:47 AM
  5. Getters and Setters
    By Charliestons in forum New To Java
    Replies: 10
    Last Post: 09-12-2008, 11:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •