Wednesday, 5 October 2011

Java: Inheritance III

Since it's hard to work with a bunch of the private variables from Vehicle, when in Car, but we don't want to make them public, in the name of encapsulation, and keeping the program....safe, I guess, there is another way that's supported! Private variables are pretty hard core, they, as we've seen, can't be used by anything but the class they're declared in, even in subclasses, which are just extensions. So instead, we use "protected". Like public and private, protected specifies the degree of encapsulation. Specifically, protected variables are usable by subclasses as well as any other classes within the same package. It's kind of like an in-between level of encapsulation!





Ta-da! No more error! So we have that idea down, I think we can fit in one more today, and that'll be on the constructors. Constructors are fairly straightforward for single classes, but when it comes to inheritance, how is everything coordinated? Java enforces some rules of construction to ensure a deterministic, as well as sensible, initialization:

a) After any parameter variables have been initialized, a call is immediately made to a superclass constructor before any other statements in the constructor body are executed. (This can be done explicitly with with "super" keyword, but we'll get to that tomorrow. Otherwise, it's done automatically).

b) All directly initialized instance variables are initialized and any initializer blocks evaluated, in the order they appear.

c) Statements in the subclass constructor are executed.

So, say with our Car, the class implemented via inheritance from Vehicle, creating a Car using the "new" operator (After giving it's constructor an integer parameter, just for display purposes):


This results in the Car constructor being called, which itself immediately calls the Vehicle constructor. Any directly initialized instance variables declared by Vehicle are now initialized, with statements in the Vehicle constructor body being executed. At this point, control returns to the Car constructor where direct initialization of instance and class variables declared in Car happens, followed by execution of the constructor.

What you want to take from this is less the technical info, but more: "Objects are initialized top down, from superclass to subclass."

Now we've got that in, I think it's time to relax, with the "super" keyword (and more!) coming tomorrow! 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. 

7 comments:

  1. im pretty sure this is useful but i have no idea how java works

    ReplyDelete
  2. I tried to learn to use it in UDK editor, and thanks to your blog I think I will be able sooner or later :]

    ReplyDelete
  3. Programming isn't as easy as I thought. D:

    ReplyDelete
  4. ^Programming is nowhere easy, Im happy I got into the hardware sphere, don't see myself as writing thousands of rows of code >.> Anyways it's really useful on the other hand and well paid, so.. :)

    ReplyDelete
  5. Hm, that's an interesting way to get rid of that error.

    ReplyDelete
  6. I think that paying someone to do it, seems easier than doing it myself hahaha

    ReplyDelete