Re: Overwriting inner classes?

Discussion in 'Java' started by Stefan Ram, May 30, 2013.

  1. Stefan Ram

    Stefan Ram Guest

    Supersedes: <-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?
    Stefan Ram, May 30, 2013
    #1
    1. Advertising

  2. Stefan Ram

    Barb Knox Guest

    In article <-berlin.de>,
    -berlin.de (Stefan Ram) wrote:

    > Supersedes: <-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 |
    -----------------------------
    Barb Knox, May 30, 2013
    #2
    1. Advertising

  3. Stefan Ram

    markspace Guest

    On 5/29/2013 9:00 PM, Stefan Ram wrote:
    > Supersedes: <-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.
    markspace, May 30, 2013
    #3
  4. Stefan Ram

    Arne Vajhøj Guest

    On 5/30/2013 12:00 AM, Stefan Ram wrote:
    > Supersedes: <-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
    Arne Vajhøj, Jun 2, 2013
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lonelyplanet999
    Replies:
    1
    Views:
    2,198
    VisionSet
    Nov 13, 2003
  2. Razvan
    Replies:
    5
    Views:
    11,279
    Dale King
    Jul 27, 2004
  3. Carlo v. Dango
    Replies:
    14
    Views:
    1,013
    Alex Martelli
    Oct 19, 2003
  4. Pyenos
    Replies:
    2
    Views:
    378
    Pyenos
    Dec 27, 2006
  5. Stefan Ram

    Overwriting inner classes?

    Stefan Ram, May 30, 2013, in forum: Java
    Replies:
    0
    Views:
    248
    Stefan Ram
    May 30, 2013
Loading...

Share This Page