Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Re: Overwriting inner classes? (http://www.velocityreviews.com/forums/t961214-re-overwriting-inner-classes.html)

Stefan Ram 05-30-2013 04:00 AM

Re: Overwriting inner classes?
 
Supersedes: <overwriting-20130530031632@ram.dialup.fu-berlin.de>
[I->It]

class A { static class B { void f(){} }}
class C extends A {}

It never occured to me before, but, yes:
Can C somehow »overwrite« f?


Barb Knox 05-30-2013 04:59 AM

Re: Overwriting inner classes?
 
In article <overwriting-20130530055916@ram.dialup.fu-berlin.de>,
ram@zedat.fu-berlin.de (Stefan Ram) wrote:

> Supersedes: <overwriting-20130530031632@ram.dialup.fu-berlin.de>
> [I->It]
>
> class A { static class B { void f(){} }}
> class C extends A {}
>
> It never occured to me before, but, yes:
> Can C somehow »overwrite« f?


No. (Presumably you mean »override«.)

A subclass can only override methods in its superclasses.

--
---------------------------
| BBB b \ Barbara at LivingHistory stop co stop uk
| B B aa rrr b |
| BBB a a r bbb | Quidquid latine dictum sit,
| B B a a r b b | altum videtur.
| BBB aa a r bbb |
-----------------------------

markspace 05-30-2013 05:41 PM

Re: Overwriting inner classes?
 
On 5/29/2013 9:00 PM, Stefan Ram wrote:
> Supersedes: <overwriting-20130530031632@ram.dialup.fu-berlin.de>
> [I->It]
>
> class A { static class B { void f(){} }}
> class C extends A {}
>
> It never occured to me before, but, yes:
> Can C somehow »overwrite« f?
>



What Barb said. C cannot even overload f. They are simply completely
disjoint.

This would be different:

class D extends B {}

But then obviously the normal rules apply. This is one thing I like
about Java over C++: the polymorphism rules are fairly simple and easy
to remember. C++ is a lot more baroque; I find it quite difficult
actually.



Arne Vajhøj 06-02-2013 01:43 AM

Re: Overwriting inner classes?
 
On 5/30/2013 12:00 AM, Stefan Ram wrote:
> Supersedes: <overwriting-20130530031632@ram.dialup.fu-berlin.de>
> [I->It]
>
> class A { static class B { void f(){} }}
> class C extends A {}
>
> It never occured to me before, but, yes:
> Can C somehow »overwrite« f?


You can't override directly like you can override
same level methods.

Depending on context you may be able to have
C have a member class D that extends B providing
you with the functionality you need.

Demo:

public class Funky {
public static void main(String[] args) {
P op = new P();
op.getIC().m();
P oc = new C();
oc.getIC().m();
op.mcall(oc.getIC());
oc.mcall(op.getIC());
}
}

class P {
public static class IC {
public void m() {
System.out.println("P.IC.m");
}
}
public void mcall(IC ic) {
ic.m();
}
public IC getIC() {
return new IC();
}
}

class C extends P {
public static class IC extends P.IC {
public void m() {
System.out.println("C.IC.m");
}
}
public void mcall(IC ic) {
ic.m();
}
public IC getIC() {
return new IC();
}
}

Arne






All times are GMT. The time now is 01:48 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.