Skip to main content

super.super notations ...

8 replies [Last post]
guilt
Offline
Joined: 2004-08-26
Points: 0

Hi,

Can this feature be done in Java, or will there be support for it in future (say, in Mustang) ?

--code--
class A{
void getValue(){
System.out.println("Aeh");
}
}

class B extends A{
void getValue(){
System.out.println("Beeh");
}
}

class C extends B{
void getValue(){
new super.super.getValue(); //How do I make this access A.getValue() ?
}
}

class D{public static void main(String []args){C cc=new C();cc.getValue();}}
--code--

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
klopperq
Offline
Joined: 2005-09-23
Points: 0

You could do something like this, but it is messy and probably no need for it. It is also assuming you have control over the source code for B (you wrote it)...

class A{
void getValue(){
System.out.println("Aeh");
}
}

class B extends A{
void getValue(){
System.out.println("Beeh");
}

[b]final void getBSuperValue(){
//final is obviously optional
//but will ensure you always get Bs super
super.getValue();
}[/b]
}

class C extends B{

void getValue(){
getBSuperValue()

}
}

As an afterthought, anyone know what tags I use to get the code displayed here formatted properly?

jwenting
Offline
Joined: 2003-12-02
Points: 0

hmm, and we'd get super.super.super.super.super.super......

Don't think so. Your superclass constructors and methods should handle calling their superclasses for you.

klopperq
Offline
Joined: 2005-09-23
Points: 0

If the author of a class intends you to have access to super methods they should provide accessors to them otherwise you mess with encapsulation...
[code]
class Human{
protected int age;

public void setAge(int age){
if (age < 0) this.age = 0;
else if (age > 110) this.age = 110;
else this.age = age;
}
}

class Child extends Human{
public void setAge(int age){
if (age > 16) age = 16;
super.setAge(age);
}
}
class Baby extends Child{
public void setAge(int age){
super.super.setAge(age);
}
}
[/code]
A call like this would totally mess with the intention of the author of the Child class. Bear in mind that a Baby is a Child and should have more, not less, refined behaviour than a Child.
[code]
Baby john = new Baby();
john.setAge(50);
[/code]
Likewise the following similar call will also go against the intention of the author of the Child class
[code]
Child jane = new Child();
jane.super.setAge(50);
[/code]
Might as well not have encapsulation
Basically it is the author of a class that decides on access to super and not the user or extender (is that a correct term?) of a class

klopperq
Offline
Joined: 2005-09-23
Points: 0

Er Ummmm of course age should be private, or else you could just set it directly from an extending class anyway.

:)

gerritcap
Offline
Joined: 2003-07-30
Points: 0

I think it is definitely unreasonable to ask for this. As this violates some of the basic principles in OO design.

If one really wants to do a super.super.aMethod() this is a clear indication of a bad class design. Why ? Well if C needs to do super.super.aMethod() this indicates that (in a certain situation) objects of C are [b]NOT[/b] "kind of" B and thus C should [b]NOT[/b] be a subclass of B.

zander
Offline
Joined: 2003-06-13
Points: 0

And don't forget the case where a B starts to extend from X, which will then extend from A (i.e. put another class in the hierarchy).
This would break C and therefor is not OO.

guilt
Offline
Joined: 2004-08-26
Points: 0

[edit]
super.super.getValue();
[/edit]

uhf
Offline
Joined: 2003-08-06
Points: 0

This is interesting but I suspect it would never be required, especially as no one's missed it. If a method's overidden in a subclass then surely it's to replace the superclass method altogether, the usual reason for using super is to get the superclass method behaviour and add something extra?

It doesn't seem unreasonable to allow it, I'm trying to thiink of problems. The compiler would have to check that the super-superclass method isn't abstract or part of an interface obviously. Are there problems that could occur at runtime?