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.
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.
I've been sick too, hope we both get better soon. :(
ReplyDeleteGood 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.
ReplyDeleteGreat write up, I like how easy you make it to follow along.
ReplyDeleteYou're doing great explanations, this blog has become really helpful to me in learning, I'm definitely going to keep at it.
ReplyDeletenicely done
ReplyDeleteGood explained.
ReplyDeleteThis is well out of my league! But thanks for the useful information! :D
ReplyDeletegolden explanation dude..
ReplyDelete