Showing posts with label classes. Show all posts
Showing posts with label classes. Show all posts

Monday, 3 October 2011

Java: Inheritance

Java has a few possible different relationships between classes, possibly the 2 most important types being packages and inheritance. We`ve already seen packages, where several separate classes in the same package are essentially connected, such as in our Bridge Hand program. The latter, inheritance, is essentially a way to make a class an extension of another. This may not sound like much, but its a pretty key part of object oriented programming.

Inheritance, as the name suggests, allows a class (the subclass, the child) to take on, or inherit, all of the features of another class (the superclass, the parent), often demonstrated with the following simple diagram:



Don`t know why the arrow is pointing up, it seems like it would make more sense the other way around. But hey, doesn`t stop how the whole inheritance thing actually works. The subclass inherits all the variables and methods from the superclass, excepting the constructor. To make this something useful, the subclass can also  declare its own variables and methods, as well as redefining some superclass methods, normally known as overriding. This allows the subclass to specialize to its own needs, while holding the same general structure of the superclass.

Let`s have a couple of examples so its easier to understand. Say we have a very very general class: Vehicle. Then we can have several subclasses to fit the role of vehicle, but be specialized depending on what we want. We can have a Truck, Motorbike or a Car defined as a subclass of vehicle. We can say that our Car is a Vehicle. It`s everything a Vehicle needs to be, but more, its specialized. Size, seating, number of wheels, windshield, whatever. It`s not just the difference between 2 Cars, the difference isn`t only the color or whatever.

Inheritance isn`t limited to one level (When is anything, in coding? Goddamn recursion, man.), you can have a subclass of a subclass. We can now have a SportsCar if we want, as an extension of Car, with it's own specializations. One important limitation to keep in mind, though, is that a subclass can only have 1 superclass. I can't have, say, a Harvester be a subclass of both a Vehicle and FarmEquipment. Yech, think of the insurance rates you'd get on that thing. Worse if your harvester was red, I'd imagine. Anyway, tangent! Moving on.

This is all very awesome, but what does it mean for particular objects? If, say, an object, myCar, is an instance of a subclass, Car, then it'll contain all the variables and methods as available in Car, as well as Vehicle. On top of this, any methods declared by any class can be called for objects of the subclass (myCar), providing they are public. This remains true however far the line you go.

There's apparently quite a few complex rules we'll be subjected to soon, so I think it's time for me to understand the core idea fully before moving on, that's it for now..Questions welcome! 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 (fixed), and feel free to throw loads of criticism at us. 

Sunday, 18 September 2011

Java: Generic Classes

    Okay, so now we have our integer stack. Well, apart from the parameter for push, and the return value for top, what's limiting it to integers? (Hint: Nothing) The code itself works pretty independently of data types, other than that, so. Hey, remember ArrayLists? The container classes we had that specified a type on initialization with the angle brackets? Recalling that apart from not allowing primitive types, it worked great. So how do we do that for our stack?

    In the class declaration, we want to place some variable name. Lets use T.

public class Stack<T> { ..


   Then, every item placed into this stack is just an object of type T, which isn't an actual thing. Now we also need to replace every instance of int when referring to element types with T, like so (Do not replace the int for last, as that's looking for the index of the last item as opposed to doing something with the elements of the stack!):

    See? It's easy. Every item in the ArrayList we're utilizing is now just a generic "T" Object. Not specifically integers, or strings. In fact, you can try pushing an int and a String onto the same stack and it'll accept them both just fine. However, if you want a generic class, but to use it for a specific type based on where you're using that class, be sure to specify what you want the type to be! So if we wanted this generic stack to only be for Integers:

GenericStack<Integer> intStack = new GenericStack();


    And now you can't push any Strings or what have you onto it.


No post tomorrow, and that's it for tonight. Comment, follow, subscribe, share etc, and see you on Monday!


    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. 

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.