Tuesday, 4 October 2011

Java: Inheritance II

Sorry for the late update, been sick again, but its passed!

To look at how inheritance is used, how it works and how its supported, we'll continue with the Car example we used yesterday. Since this is just for quick examples, don't expect me to have a fully functioning Car class by any stretch of the imagination, though.

I put the AirFilter and Thermostat classes (empty) inside the vehicles package just so I could show you this without a whole bunch of red lines everywhere! The point to take from this is, we have this Vehicle class, it contains a bunch of private elements that make up a vehicle, as well as an array to hold its location. I figured it would be as coordinates, but that really isn't important! Then, there's a bunch of public methods the class has, one of which is a constructor. Got it? Good.

Say now, we want to make a Car. It`s kind of like a Vehicle, but we have 4 wheels, a paint job, some headlights, whatever else. So we could just make another class from scratch, but if we were to do that (And we aren't), we'd notice a lot of similarities between the 2. Both would need to move, both would have air filters and thermostats and whatever else. The similarities are such that a Car is everything a Vehicle is, but more, as opposed to being just similar in some ways, and different in others, its just an extension. 

To make something a subclass of some other class, which is the superclass, we need to make to add in an "extends SuperClass" to the class declaration. As so:

Let's go through it bit by bit. First off, we have the class declaration. As you see, just a simple "extends Vehicle" is all we need. Car is now a subclass of Vehicle. The main method here is only for demo purposes. First, we create a Car object, and then we apply methods available to Vehicle on it. And that's all fine and dandy. Remember, the methods were all public. However, when we try to do something to one of the private variables in the Vehicle, we get an error message telling us as much.

Well, I could go on, but I'm finding it way easier to absorb information in smaller chunks, since there's more time to mull over each bit before being thrust onward, 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. 


  1. I've been sick too, hope we both get better soon. :(

  2. Good follow up to your last post, sadly i don't have the time to "study" em. I wish i had more time, but with midterms coming soon, i just can't seem to find it.

  3. Great write up, I like how easy you make it to follow along.

  4. You're doing great explanations, this blog has become really helpful to me in learning, I'm definitely going to keep at it.

  5. Good explained.

  6. This is well out of my league! But thanks for the useful information! :D