Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By quad64bit

Thread: OOp Question - Creating a Class

  1. #1
    cdgary is offline Member
    Join Date
    Jan 2012
    Posts
    1
    Rep Power
    0

    Default OOp Question - Creating a Class

    I'm somewhat new to object oriented programming and I'm trying to get my head around a concept. I looking at somebody else's code and I've noticed that they created a class where they include properties within their class that have the object type of the class they created. For example:

    public class FeedforwardLayer
    Java Code:
    {
        private FeedforwardLayer next;
    
        private FeedforwardLayer previous;
    
        public FeedforwardLayer getNext()
        {
             return this.next
         }
    
         public FeedforwardLayer getPrevious()
        {
             return this.previous
         }
    
    }
    1. Is there a better name and explanation for this approach
    2. Can you give me a short, easy to follow practical code example on how this should be used, so I can understand it better. Kind of difficult to understand it in the context of the above code.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: OOp Question - Creating a Class

    Looks like a linked list to me, give or take.
    An element points to the next (and previous) elements in the list.

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: OOp Question - Creating a Class

    1. Is there a better name and explanation for this approach
    Previous poster seems to be correct. When I first started with OOP, my ah-ha! breakthrough moment was when I started thinking of classes as blueprints.

    An architect spends a lot of time designing a blueprint. The blueprint has all of the characteristics of a house clearly defined. However, a blueprint is not a house. Yet, from a blueprint, one could build many houses. All are similar, but each is it's own house. In this case, the house is the object stored in memory at runtime, where as the blueprint is the class.

    Secondly, OOP languages treat classes like types. Think of a type like you would with the blueprint analogy. You might have a blueprint for a tudor, or a rancher, or a mansion...

    So from within a class you see a reference to the same class, it isn't referring to the same exact object; just the same class. On a street you might have 50 of the same house. But they aren't the same house, they are just all constructed from the same set of plans. Each is separate and has it's own address (so do their digital counterparts, the addresses are memory addresses!). Having something like "FeedforwardLayer next;" or FeedforwardLayer previous;" would be analogous to having a sign on each house saying "House to the left that way" and "House to the right the other way". The house knows of other houses adjacent to it. In the same way in a linked list, an object knows about the one before it and the one after it. Each of the three objects are of type "FeedforwardLayer". Using the word "FeedforwardLayer" before each name (next, previous) tells java what type of object to expect.

    2. Can you give me a short, easy to follow practical code example on how this should be used, so I can understand it better. Kind of difficult to understand it in the context of the above code.
    Sure! A linked list is a classic example of when you would do this. Another example would be with Trees (like binary trees, quad trees, any type of tree), maps, etc... Any type of structure where you have more than one of the same thing and said things know about each other.

    Another example would be classes that are self constructing. If I want to use a Color object, I can ask the Color class to give me one pre-made like this:
    Java Code:
    Color myColor = Color.RED;
    The color RED in this case was premade inside the Color class, and looking inside Color's source code would show references to itself.

    It is important to not get the source code you see and the object in memory confused. You might write 1 class called Person which has fields and methods in it, but you could create a million objects from that class. They are all unique Person objects, each with a complete copy of all the methods and fields. Remember this when writing your code, and it will help you visualize what is happening!
    Fubarable likes this.

Similar Threads

  1. Replies: 9
    Last Post: 07-29-2011, 02:18 PM
  2. Question Creating a .jar
    By waterpolobro in forum New To Java
    Replies: 1
    Last Post: 05-18-2011, 08:15 PM
  3. Question about creating jar file
    By hei1233212000 in forum New To Java
    Replies: 3
    Last Post: 10-27-2010, 02:41 PM
  4. Replies: 5
    Last Post: 12-21-2009, 01:17 PM
  5. Creating objects question
    By sergm in forum New To Java
    Replies: 2
    Last Post: 12-27-2007, 04:10 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
  •