Monday, February 6, 2012

Discussion for Feb. 6

Today we went over the GGFrame.java example. You should look over this code and see what improvements can be made. How can we make it more modular?

Discussion Topics (post for participation credit):
We saw many new things today. Two of these appeared on the class declaration line of GGFrame.java: extends and implements. What does it mean to extend a class? What does it mean to implement an interface?

This brings up another question, what is an interface? You should read about interfaces here: http://docs.oracle.com/javase/tutorial/java/concepts/interface.html Post your thoughts on interfaces. Define it, post 1 (or more) pro for using an interface, post 1 (or more) cons for using an interface.

We saw the basics for implementing the ActionListener interface today. Read about it here: http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html then post your thoughts. When do you need to implement this interface?

We are starting to see GUI components in class. You may not know it but, there is a very large number of components that the javax.swing library provides. Look through the javax.swing library in the Java 6 API and pick 1 or 2 classes that start with a J (such as JFrame or JPanel) and explain them. When would you use these components?

The term overload came up today. Java allows us to overload both methods and constructors. What does it mean to overload a method or constructor? Give a short example using pastebin.com links.

Another term came up today Override. Override is an annotation that has been available since java 5. What is its purpose? What is the difference between overloading a method an overriding a method?

Composition came up in the discussion today. You will be doing object composition for the lab this week as well. When we are talking about composition in object oriented programming, what does it mean? Give an example.

See you Wednesday!

19 comments:

  1. "The term overload came up today. Java allows us to overload both methods and constructors. What does it mean to overload a method or constructor? Give a short example using pastebin.com links.

    Another term came up today Override. Override is an annotation that has been available since java 5. What is its purpose? What is the difference between overloading a method an overriding a method?"

    In linguistics, specifically phonology, we sometimes talk about whether two phonemes (sounds) are in overlapping or in complementary distribution. If the two phonemes are used in the same context, but create different meanings (like city and ditty, for example), they are in complementary distribution. Likewise, if two phonemes can be swapped in the same context (like how it doesn't matter if you pronounce city as "si-tee" or "siddy"), they are in overlapping distribution.

    Overloaded methods are like phonemes in overlapping distribution: they are used in the same sort of context and don't confer different meaning, but are instead used situationally to do basically the same sort of thing to different input. A good example is the print function, which actually calls to several related methods depending on what it was passed as input.

    Conversely, overriding methods are like phonemes in complementary distribution--the method/meaning of the subclass completely supplants the meaning of the superclass, despite their having received the same input and existing in the same context.

    ReplyDelete
  2. Hmm. The parallels you draw with phonology concepts are interesting, but I wonder about how deep the analogy really goes. In particular, when you say that "overloaded methods ... are used to do basically the same sort of thing to different input", that is right, but there can be a big difference between "basically the same sort of thing" (so it makes sense to have the same name) and "can be swapped." Can anyone think of a good illustrative example of this?

    Also, overridden methods from a superclass are very often also "basically the same sort of thing," so I don't really buy the distinction you are making. Still, it is thought-provoking...

    ReplyDelete
  3. Overloading is a way for methods and constructors to accept different input that can vary in type and number of inputs. And return the same type of the input.

    Here is link to a method example.

    ReplyDelete
    Replies
    1. The example you've linked to won't compile. You can't have a local variable with the same name as one of your parameters for that method.

      Delete
    2. Here is a do over. I think this would work.

      Delete
  4. Action listeners can be used to define an action if the user performs specific operations, such as pressing a button in a JFrame window. Another helpful tool is the Change listener, which can be useful in collecting user input through the use of sliders.

    ReplyDelete
  5. This brings up another question, what is an interface? Post your thoughts on interfaces. Define it, post 1 (or more) pro for using an interface, post 1 (or more) cons for using an interface.

    An interface is kind of like a model (consisting of stub methods) for how other code will interact with some other code. If you have a class that implements an interface, all of the stub methods defined in the interface must be implemented by the class. The thing is, the class can implement those methods however it needs to, so long as it still operates within the parameters defined by the interface.

    It's a coding tool that allows you to provide methods of interacting with any chunks of code you want without defining any kind of implementation. As the link said, a great way to think of it would be like providing the knobs and dials for a generic TV without providing anything regarding the actual internals of the TV. Other code can use the knobs and dials to ensure that, no matter what kind of device the TV eventually becomes, the other code can still get the same information from it.

    I suppose this is a coding tool that is more of a reinforcement of good coding practice, rather than being a type of feature or neat coding trick.

    PROS for interfaces:
    - Reduces the need to rewrite code by standardizing interaction
    - Helps reduce bugs when dealing with the interaction of two seperate chunks of code
    - Enforces good coding practice
    - Can provide interfaces to other programmers so they can quickly understand how to interact with your code

    CONS for interfaces:
    - All of the methods in an interface MUST be present in the class, even if they are extraneous/illogical

    ReplyDelete
    Replies
    1. This is a nice answer. One thing you did not mention was the idea that by implementing the interface you become an instance of the type of that interface. For example, say I implement the ActionListener interface. I can then refer to that object as an ActionListener.

      ActionListener a = new MyObjectType();

      This allows for quick and easy changes to your implementations.

      Delete
  6. I believe that "extends" is "inheritance". Inheritance allows one class to inherit the properties of another class.

    ReplyDelete
    Replies
    1. You are correct that by using the extends keyword, you inherit properties from the class you are extending. Could you describe in greater detail what this means?

      Delete
  7. Another term came up today Override. Override is an annotation that has been available since java 5. What is its purpose? What is the difference between overloading a method an overriding a method?

    First of all an annotation is metadata for your code. That is information about the data. Annotations are read by the compiler and tell it things. In this case you tell it that you want to override a method that is in the superclass. The compiler will perform certain checks when you use this annotation that will help you, such as spelling the name of the method correctly. The difference between override and overload is that when you override a method you are replacing it in its entirety. When you overload a method you are adding additional functionality to it.

    ReplyDelete
    Replies
    1. I am not convinced of your explanation. Are you saying that annotations are only used at compile time?

      Could you explain overloading a little more? I am not convinced that overloading adds functionality to a method. Is it doing anything to the existing method(s)?

      Delete
    2. No, I was merely using the compiler statement as an example. Annotations can be read by other programs, APIs, libraries, etc. They are used as metadata for your code to be read by other entities (programs for example) not just the compiler. As far as overloading, I was not entirely clear when I said "adding additional functionality". What I should have said was modifying functionality. When a method is overloaded, the original method is not changed in any way. But when that method is called in a specific manner, number of arguments for instance, then the new overloaded code is run as opposed to the original code.

      Delete
  8. Overloading means that you have multiple versions of the same method and/or constructor that take in different arguments to do different things.

    "Method overriding, in object oriented programming, is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes. The implementation in the subclass overrides (replaces) the implementation in the superclass by providing a method that has same name, same parameters or signature, and same return type as the method in the parent class."

    http://en.wikipedia.org/wiki/Method_overriding

    Overloading you are enabling a method to do multiple things. Overriding you are changing what the method does.

    ReplyDelete
    Replies
    1. This is a good answer. Could you write a short example of Overloading and post it here using a pastebin.com link?

      Delete
    2. No...fine

      Here: http://pastebin.com/3856CKin

      Delete
    3. That is indeed an example of Overloading. However, what are some improvements that could be made?

      Delete
  9. I could change the class fields' names to something else, so I wouldn't have to use "this", and make it less confusing.

    ReplyDelete