wood burning stoves*
The moose likes Beginning Java and the fly likes super.super.method(); Why is it wrong ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "super.super.method(); Why is it wrong ?" Watch "super.super.method(); Why is it wrong ?" New topic
Author

super.super.method(); Why is it wrong ?

Raghavendra Shockley
Ranch Hand

Joined: Jan 21, 2012
Posts: 69
Can someone give me a real world example which shows this is wrong ? Do any languages allow it ?

Back to square one.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

It's wrong in Java because the rules don't allow it. So how could a "real-life" example demonstrate that the rules don't allow it?
Raghavendra Shockley
Ranch Hand

Joined: Jan 21, 2012
Posts: 69
Paul Clapham wrote:It's wrong in Java because the rules don't allow it. So how could a "real-life" example demonstrate that the rules don't allow it?


But there has to be some reason why it is like that or at least which real life example it works for.
For example, private variables are inherited by child classes but can't be accessed by them directly. One may ask why ? or show an example where this is applicable.
They have to be accessed by public getter methods of the parent class. Real world example where this may apply -
Please tell me if this makes sense or if there is any flaw here.


Parent
private will; // valuables for kids left by grand father!
public giveWill(); //check if potential recipient is deserving or not
skinColour;

Child extends Parent
Parent mom;
Parent dad;

//child skinColour = "momSkinColour" + "dadSkinColour"
public askForWill();

The child cannot access the will of grand father, but certainly inherits it. No other person or child can get it.To access it, the child must satisfy -
boolean isAResponsibleAdult, boolean isDeserving == true.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Raghavendra Shockley wrote:One may ask why ? or show an example where this is applicable.

Yes, but you're not asking 'why?', you're asking 'why not?' (ie, 'why didn't the designers allow it?'), and that involves getting inside their heads.

All I can tell you is that super has to do with code structure, it's not some "thing" (like a Will) that can be handed down from object to object; and while, in an inheritance hierarchy, there may be a need for accessing the parent of a class (and, to my mind, you should avoid even that if there is a better/simpler solution available, such as making an item or method protected), there's usually very little need to "grandparent".

If you find yourself needing to do so, it usually indicates that your code isn't very well designed.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
That inheritance hierarchy is severely flawed, unless you can tell me that a Child IS-A Parent.

What you are saying by super.super is like using this hierarchyIn that foo() method, the Bus is behaving as a Conveyance, not a MotorVehicle. This states that a Bus IS-NOT-A MotorVehicle, which contradicts the extends MotorVehicle in the class heading.
Raghavendra Shockley
Ranch Hand

Joined: Jan 21, 2012
Posts: 69
Winston Gutkowski wrote:
Raghavendra Shockley wrote:One may ask why ? or show an example where this is applicable.

Yes, but you're not asking 'why?', you're asking 'why not?' (ie, 'why didn't the designers allow it?'), and that involves getting inside their heads.


I wish I could do that or books would do that. I find it difficult to remember so many rules without reasoning. The OCJP helped me to understand some aspects of Java, but not many of them with logic. For example, I still don't know why public classes must be kept in a file of the same name. The only way out seems to be by developing code for a lot of scenarios.

Raghavendra Shockley
Ranch Hand

Joined: Jan 21, 2012
Posts: 69
Campbell Ritchie wrote:That inheritance hierarchy is severely flawed, unless you can tell me that a Child IS-A Parent.

What you are saying by super.super is like using this hierarchyIn that foo() method, the Bus is behaving as a Conveyance, not a MotorVehicle. This states that a Bus IS-NOT-A MotorVehicle, which contradicts the extends MotorVehicle in the class heading.


The child is-a parent because it is a subclass of parent. Even I don't understand why someone would want to use ....super.super.method(). All I was looking for was some reason to show why that may not be good. But your point makes sense.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Raghavendra Shockley wrote: . . . The child is-a parent because it is a subclass of parent. . . .
That is the wrong way to think of inheritance. You should think, “A child is-a parent therefore we can make the Child class a subclass of the Parent class.”

In real life, that is the wrong way round. It is not true that a child is-a parent. It is more accurate to say a parent is-a child. How many children do you know who are parents? I bet, none. How many parents do you know who are children? Answer: all of them (well, they used to be ). Yes all parents arewere children. In fact in parts of Europe people talk about their adult sons and daughters as their children.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Raghavendra Shockley wrote:Even I don't understand why someone would want to use ....super.super.method(). All I was looking for was some reason to show why that may not be good.


Well, it's true that using "super.super.method()" isn't very good in terms of object-oriented design, but the fact is that there's plenty of Java constructs which you can use to implement cruddy designs. But if you wanted to let people use "super.super" then you have to write a bunch of rules about how many supers can be used, and you have to decide whether the compiler should implement those rules or whether the runtime should throw exceptions when they are violated. Consider this:



As you can see this hypothetical code tries to refer to the superclass of Object, which doesn't exist. It's pretty clear that the compiler could flag that particular example as an error, but there are more complicated versions which can get by the obvious compiler checks. So the runtime would have to be dealing with the same sort of thing. At this point you can imagine the original designers saying "Nope, the heck with it, we aren't going to bother with that. Way too much trouble."

And who knows? Maybe they did consider the possibility and then rejected it for that reason. Or maybe they just rejected it straightaway as something the language didn't need. One can only speculate at this point in time.
 
GeeCON Prague 2014
 
subject: super.super.method(); Why is it wrong ?