In the "Rules Round-Up" ( better known as: "Watch Ray Chase Cows Around in Cirles" ) game there is a question: Class Y is a subclass of class X. Will this compile? X myX = new Y( ); My question is: Which constructor will be used to create myX, X's or Y's? My guess is Y, due to the statement 'new Y( )'. A follow up question is: what will myX be, a X or a Y? Thanks, Ray
Anxiety does not empty tomorrow of its sorrows, but only empties today of its strength. – Charles Spurgeon
Which constructor? Both! Y's constuctor is called which immediately call's X's constructor. When X's constructor is done doing its thing, Y's constructor finishes up doing its thing. "what will myX be, a X or a Y?": Both! myX is an X reference currently referencing a Y object (which, naturally, is also an X object).
Well, that's clear as mud, but it covers the ground. Guess I need to read up on subclasses. Perhaps constructors as well. I thought I understood them, but I don't see how two constructors can create one object. Or does the X do the creating and Y modifies? I thought constructors only created objects, I didn't know they could modify them, if that's what happens.
Anxiety does not empty tomorrow of its sorrows, but only empties today of its strength. – Charles Spurgeon
Now consider X myX = new Y(); The Y constructor is called. The Y constructor automatically calls the X constructor. X sets cows to 5. The X constructor is now done. The Y constructor picks up where it left off. The Y constructor sets chickens to 20. The Y constructor is now done.
OK, now we got 5 cows and 20 chickens. How many ducks are there? No, I mean, myX represents or contains 5 cows and 20 chickens, right? Is this where handles come in? Bear with me: myX.cows == 5 and myX.chickens == 20? Is that right? Now if I say "X myX2 = new X();" a reference to myX2.chickens would be incorrect, right? But myX2.cows is legit? Thanks
Anxiety does not empty tomorrow of its sorrows, but only empties today of its strength. – Charles Spurgeon
You have an X reference to a Y object. X references know only about X stuff. So you cannot see myX.chickens - that won't compile. You would need a Y reference to the same object to see that. If you need to know about the chickens from an X reference, you'll need this:
Ahhh, the beauty of polymorphism. Now you can say myX.getChickens() to find out how many chickens there are. Your X reference knows all about the method getChickens(). Your Y object (that myX references) knows all about it too. It knows that getChickens is overridden, so be sure to call Y.getChickens(). Better?
Ok, I believe I understand the "what" of this discussion thread. Now I wonder about the "why". Why would you create an object that contained 5 cows and 20 chickens, instead of two variables one for cows and one for chickens? There must be a reason for this added complexity. In my experience with programming the K.I.S.S. method reigns supreme. So, for a programmer to create something in a more complex way, there must be a benefit, however, I don't see what it is. Thanks, Ray (SADQ) `:-
Anxiety does not empty tomorrow of its sorrows, but only empties today of its strength. – Charles Spurgeon
If you make a class that has certain attributes, you need to know that those attributes will always be there for you. And you don't want to worry about attribute names of the class you are inheriting or the classes that might inherit your class. So class attributes cannot be overridden. Only methods can be overridden.