Showing posts with label super. Show all posts
Showing posts with label super. Show all posts

Thursday, 6 October 2011

Java: Inheritance IV

Today, we get to talk about the super keyword, and how static variables work with inheritance!


Super


Remember our Car had an int parameter, for whatever reason. Well if we had a constructor for Vehicle with an integer parameter that we wanted to use, our Car constructor would throw an error.. This is because a default constructor is looked for, but not found (we only have an integer parameter one). What we need to do instead is call the superclass constructor with either a parameter in the Car constructor, or some other value.






The super expression (heh) replaces the automatic call to the superclass constructor so the superclass constructor with the matching parameter is called instead. Just remember that if you are going to use super, it should be the first statement in your subclasses constructor.




Statics


Since static variables and methods are inherited, we should probably know how they work when inherited. Well, pretty much the same as always, 1 variable is shared between all instances of a class, including subclasses. It doesn't work in reverse though. A subclass is an extension of its superclass, but a superclass is not an extension of a subclass. Example:






...would lead to each new instance of Vehicle AND Car to increase the count, since Car uses the Vehicle constructor, but a similar counter inside Car would not increase for a Vehicle being created.




I'm tired now, so 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. 

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.