Wednesday, 14 September 2011

Java: Classes and Objects (Intro)

    Classes and objects! We've used these, and you should probably be able to use them on your own (simply using them doesn't take much effort), but now we get to go into a little more detail, yay us. We need this to create our own objects and classes to get our OOP on (that's object oriented programming). 
    Properly done, Java programs of any size should be object oriented, since among this approaches strengths is the ability to design/build programs reliably, allowing for much better support when it comes to maintenance or program expansion. Another huge strength is the ability to reuse classes elsewhere, across wildly varied programs, and even in different parts of a program with little hassle.
    Remember with our I/O code, we had some buffered reader/writers, and FileInput etc? Those were all classes that are available to make coding easier, so you can see how it could be helpful. Why reinvent the wheel?
    Various concepts need to be looked at to understand the whole OOP thing fully, since it's hard to fully get your head around. Among these are abstract data types (ADT), objects, classes and instantiation. So, yeah, that's what we'll be working on.

    An ADT specifies a new data type by defining values that belong to the type as well as the operations that are applicable to that type (methods). An implementation of the type specified by the ADT can be created using variables to provide a representation of the ADT's values, and procedures to implement operations. Using these tools on their own is a pain in the ass though, so it's better when there's actual support for making an ADT, with a compiler that error checks it.

    As an example, we'll be using a stack object. Stacks use a FILO system (That is, first in, last out) to manage accessing it's elements. For a visual representation, just imagine stacking...anything. Plates, CDs, trays. The first item on the stack, that is, on the bottom, will be the last one to leave, being pushed further back in the queue by any new items being stacked on top.
    A stack of data items functions in essentially the same way, so to implement one, we'll probably need several methods! (Planning is good!)

1.    push      - Pushing a value onto the stack is just placing a data item on top
2.    pop        - Popping a value off the stack is the removal of a data item.
3.    get         - A method for returning the value of the item at the top, without actually removing it!
4.    isEmpty - A method to test if the stack has any values left on it. This is so you don't try to pop a value that doesn't exist, causing all kinds of commotion.

    Anything else? I don't think so, but if I'm missing anything, leave a comment. Otherwise, these methods as part of some container class (some data structure we can use) should serve us well. But using variables within the methods we're using is worth nothing, their scope is for the methods only. This means they don't actually exist outside the method. We need something with a global scope, so we can just declare variables outside the methods, but still within the class.

    This has been very wordy today, and I'm tired now, so that's it for tonight. Comment, follow, subscribe, share etc, and see you tomorrow!

    And as part of a shameless plug for a friend, if you're interested in classic movies/books/music, visit his site here, and feel free to throw loads of criticism at us. 
      Also, a bunch of the text is more or less straight from the textbook mentioned in earlier posts since I'm crud at writing.

1 comment:

  1. Just reviewed this the other day but a second time doesn't hurt.