Friday, February 10, 2012

Discussion for Feb 10

Okay, so today we *tried* to look over the code from lecture 10 some more. However, we were side tracked by 4 topics "svn uses", "emacs functionality", "inheritance", and "interfaces".

Homework:
Look over the code from lecture 10 and come up with improvements

Topics for Discussion (reply for participation credit):
What is the dictionaries definition of inherit? How does this relate to inheritance in Object Oriented Programming? Do you think inheritance was a good choice to describe this? Why or why not?

What keyword is used to describe inheritance in java? What is the syntax of this keyword?

Every Class in Java inherits from another Class. If left unspecified, what Class does it inherit from? What does it mean to inherit from this class?

We saw in class that in every Class Constructor you must invoke a Constructor from the class you inherit. How is this done? Knowing this, what does it mean if your class has no default constructor? What if your class only has private constructors?

Some languages have multiple inheritance. What does this term mean? Java does not allow for an class to have more than 1 super class. How does Java allow for the benefits of multiple inheritance?

What is an interface in Java? Why would you ever use an interface?

Have a good weekend!

10 comments:

  1. Dictionary definition of inherit:

    1. to take or receive (property, a right, a title, etc.) by succession or will, as an heir: to inherit the family business.

    2.to receive as if by succession from predecessors: the problems the new government inherited from its predecessors.

    This relates to OPP because in a subclasses "inherit" the methods, constructors, and fields of their superclass just like children inherit their parents physical features and properties.

    I think this is a good word to describe what is doing. I think it relates more to how children inherit properties from their parents and not their physical features because the superclass in OPP can decide what is going to be inherited by its subclass.

    Keyword used: "extends"
    Syntax: class declaration extends other class

    Example:

    public class test extends test2{

    If a class is left unspecified from which class it inherits from. It inherits from the Object class.

    ReplyDelete
  2. Java classes can have only one super class, but, classes can have an infinite number of subclasses, or classes which inherit/extend them.

    For example: a class describing a tool could describe certain universal attributes/methods, and a sub class (of this class) describing a hammer could provide additional attributes/methods related to a hammer.

    An interface is special type of class intended to define the public functionality of classes which extend it. An interface does not implement its methods (the body is empty) though it can have member variables. So a class which uses an interface is declared to "implement" it.

    Expanding on the tool demo: suppose one wanted to have objects which extend two or more other objects. Vice-Grips are pliers, and both are tools and inventory items. Vice-Grips could extend pliers and add methods describing their locking function. The Vice-Grip object could then implement the tool interface and an additional inventory interface.

    ReplyDelete
    Replies
    1. Rob, this is a nice start. However, be careful when you say that an interface can have member variables. There is a caveat: an interface can only have member variables that are "public static final". If you don't declare one of these things, the compiler saves you and puts it in. However, if you try to make a member variable private or protected, you will get a compilation error.

      Delete
    2. Yeah I overlooked that. Also, I now realize that my example is a bit weak. If a pilers object extends tool, and a tool object implements an interface, a vice-grips object would inherit (through the pliers object) the extension and implementation of the super class and super interface in the tool object.

      Delete
  3. "Some languages have multiple inheritance. What does this term mean? Java does not allow for an class to have more than 1 super class. How does Java allow for the benefits of multiple inheritance?"

    Multiple inheritance refers to giving a class multiple superclasses, allowing the subclass to inherit all of the variables and methods of its superclass parents.

    One way Java can simulate the benefits of multiple inheritance is through delegation - creating objects inside a class that do the implementation work for the class instead of the class doing it directly.

    Using delegation, a class can create instances of classes it needs to inherit from, then call methods in those instances to do whatever work is needed. In this way, it is kind of like inheriting the methods from the classes, and you can have a class "inherit" many classes this way.

    I found a really interesting article on why Java doesn't allow for multiple inheritance, and the article also talks about solving the problem of multiple inheritance through interfaces: http://www.javaworld.com/javaworld/jw-12-1998/jw-12-techniques.html?page=1

    ReplyDelete
  4. I think of it from that film The Matrix.

    public class Morpheus extends Humans
    public class Neo extends Morpheus

    Neo can do anything Morphues and/or ordinary humans can do...and he can do whatever else he wants.

    That's just how I remember that the _lowest_ level of hierarchy has the _most_ functionality.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. // implicit casting:
      Dog myDog = new Animal();

      // explicit casting:
      Dog myDog = (Dog) animal;

      Extended methods can be overridden (modified, edited, re-wrote) by using the @Override annotation above the method signature.

      ---------------------------------------------------------------------

      An interface is a class of abstract unimplemented (blank) methods from one or more other classes that can be used to kind of create psuedo-multiple-inheiritance. You _must_ implement all the methods. However, you can keep the methods abstract and pass them along to a class that extends your class. Eventually all methods must be implemented for the program to compile. Interfaces tell a programmer which methods he/she must write for a particular project while allowing the manager of the project to determine those methods; I imagine this is important when companies are hiring another company to write software that must be conform to their system.

      public interface InterfaceName {

      // unimplemented abstract method
      public abstract returnType methodName (int param);
      }

      public class ClassName implements InterfaceName {

      // implementing the method
      public returnType methodName (int param) {
      return returnType;
      }
      }

      Delete
    3. Be careful with your choice of words,

      You state, "An interface is a class of abstract unimplemented (blank) methods from one or more other classes that can be used to kind of create psuedo-multiple-inheiritance."

      However, an interface is *not* a class. It is similar to a class in the sense that it can have method declarations.

      Also, the unimplemented methods do not come from one or more other classes. They can be inherited from other *interfaces* but this is not a requirement.

      Delete