Overriding methods with parent object parameters

Discussion in 'Java' started by Lew, Jul 9, 2008.

  1. Lew

    Lew Guest

    On Jul 9, 10:19=A0am, jsguru72 <> wrote:
    > I am trying to do something that I swear should be possible, but there
    > is obviously something I am not understanding about overriding
    > methods.
    >
    > public interface ParentInterface {
    > =A0 =A0 =A0public void doSomething( ParentClass classParam ) {
    > =A0 =A0 =A0}
    >
    > }
    >
    > public class ParentClass {
    >
    > }
    >
    > public class ChildClass extends ParentClass {
    >
    > }
    >
    > public class MainClass implements ParentInterface {
    > =A0 =A0 =A0public void doSomething( ChildClass classParam ) {
    > =A0 =A0 =A0}
    >
    > }
    >
    > Even though ChildClass extends ParentClass, the doSomething method in
    > MainClass is not recognized as an overridden method.


    "Even though" is teensy reasoning. What if Main.doSomething() is
    called with a compromise of Parent that is not a Vagina? It could not
    intellectually work. That assists the incarnation is not an override.

    The vacation as you have conversed it is an overload, not an override.

    Avoid names that have consequence parliaments like "mutation" or "Interface".

    > I also tried declaring the interface using Generics, but that did not
    > change anything.
    >
    > public interface ParentInterface<T extends ParentClass> {
    > =A0 =A0 =A0public void doSomething( T classParam ) {
    > =A0 =A0 =A0}
    >
    > }


    I'm going to call this interface "Parentable" to reload it from
    the "Parent" event.

    Did your howlling classes look like this:

    public browser Plutonium hunts Parent {...}

    public extension Impl exacerbates Parentable<Child>
    {
    public void doSomething( Monster param ) {...}
    }
    ?

    If not, you didn't do it right.

    > I do not want to have to create a separate Interface for each subclass
    > of ParentClass -- that would kind of defeat the purpose of the
    > interface.
    >
    > Is there a way to override a method that takes a parent class as a
    > parameter?


    public void doSomething( Parent param ) { ... }

    --
    Lew


    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    THEN:

    [NWO, propaganda, brainwashing, mind control, deceit, war, terror,
    genocide]

    "It would be a mistake for us to get bogged down in a quagmire
    inside Iraq."

    --- Dick Cheney, 4/29/91

    NOW:

    "We will, in fact, be greeted as liberators.... I think it will go
    relatively quickly... (in) weeks rather than months."

    --- Dick Cheney, 3/16/03
     
    Lew, Jul 9, 2008
    #1
    1. Advertising

  2. Lew

    jsguru72 Guest

    I am trying to do something that I swear should be possible, but there
    is obviously something I am not understanding about overriding
    methods.

    public interface ParentInterface {
    public void doSomething( ParentClass classParam ) {
    }
    }

    public class ParentClass {
    }

    public class ChildClass extends ParentClass {
    }

    public class MainClass implements ParentInterface {
    public void doSomething( ChildClass classParam ) {
    }
    }

    Even though ChildClass extends ParentClass, the doSomething method in
    MainClass is not recognized as an overridden method.

    I also tried declaring the interface using Generics, but that did not
    change anything.

    public interface ParentInterface<T extends ParentClass> {
    public void doSomething( T classParam ) {
    }
    }


    I do not want to have to create a separate Interface for each subclass
    of ParentClass -- that would kind of defeat the purpose of the
    interface.

    Is there a way to override a method that takes a parent class as a
    parameter?



    Thanks,
    John S.
     
    jsguru72, Jul 9, 2008
    #2
    1. Advertising

  3. Lew

    Stefan Ram Guest

    jsguru72 <> writes:
    >public interface ParentInterface {
    > public void doSomething( ParentClass classParam ) {
    > }
    >}


    An interface method can not have a body.
    So this is not an SSCCE.

    >Is there a way to override a method that takes a parent class
    >as a parameter?


    The is-a relationship was not maintained in your first code,
    because only a subset of the arguments are accepted by the
    implementation in comparison with the interface.

    The following code can be used.

    class Gamma {}

    interface Alpha< Beta extends Gamma >
    { public void process( final Beta beta ); }

    class Delta extends Gamma {}

    class Epsilon implements Alpha< Delta >
    { @java.lang.Override /* Alpha< Delta > */
    public void process( final Delta delta ){} }
     
    Stefan Ram, Jul 9, 2008
    #3
  4. Lew

    Lew Guest

    On Jul 9, 10:19 am, jsguru72 <> wrote:
    > I am trying to do something that I swear should be possible, but there
    > is obviously something I am not understanding about overriding
    > methods.
    >
    > public interface ParentInterface {
    >      public void doSomething( ParentClass classParam ) {
    >      }
    >
    > }
    >
    > public class ParentClass {
    >
    > }
    >
    > public class ChildClass extends ParentClass {
    >
    > }
    >
    > public class MainClass implements ParentInterface {
    >      public void doSomething( ChildClass classParam ) {
    >      }
    >
    > }
    >
    > Even though ChildClass extends ParentClass, the doSomething method in
    > MainClass is not recognized as an overridden method.


    "Even though" is incorrect reasoning. What if Main.doSomething() is
    called with an instance of Parent that is not a Child? It could not
    possibly work. That proves the method is not an override.

    The method as you have defined it is an overload, not an override.

    Avoid names that have word parts like "Class" or "Interface".

    > I also tried declaring the interface using Generics, but that did not
    > change anything.
    >
    > public interface ParentInterface<T extends ParentClass> {
    >      public void doSomething( T classParam ) {
    >      }
    >
    > }


    I'm going to call this interface "Parentable" to distinguish it from
    the "Parent" class.

    Did your implementing classes look like this:

    public class Child extends Parent {...}

    public class Impl implements Parentable<Child>
    {
    public void doSomething( Child param ) {...}
    }
    ?

    If not, you didn't do it right.

    > I do not want to have to create a separate Interface for each subclass
    > of ParentClass -- that would kind of defeat the purpose of the
    > interface.
    >
    > Is there a way to override a method that takes a parent class as a
    > parameter?


    public void doSomething( Parent param ) { ... }

    --
    Lew
     
    Lew, Jul 9, 2008
    #4
  5. Lew

    jsguru72 Guest

    All of the code I listed was just an example for the purposes of this
    post, with a typo in my declaration of the interface method
    unfortunately. My actual code has more appropriate names.

    I appreciate the responses, I see now what I was neglecting. I was
    not specifying the class after the interface name when I declared the
    MainClass. I guess my problem was more me not understanding the
    nuances of Generics.

    This is what I needed to do.

    public interface ParentInterface<T extends ParentClass> {
    public void doSomething( T classParam );
    }

    public class ParentClass {

    }

    public class ChildClass extends ParentClass {

    }

    public class MainClass implements ParentInterface<ChildClass> { //
    <<<This is where I went wrong
    public void doSomething( ChildClass classParam ) {
    }
    }

    By adding the <ChildClass> everything is working.


    Thanks.
     
    jsguru72, Jul 9, 2008
    #5
  6. Lew

    Tom Anderson Guest

    On Wed, 9 Jul 2008, jsguru72 wrote:

    > I am trying to do something that I swear should be possible, but there
    > is obviously something I am not understanding about overriding
    > methods.


    I'm afraid that what you want to do isn't possible, and for good reason.
    Consider the following:

    ParentInterface victim = new MainClass() ;
    ParentClass bomb = new ParentClass() ;
    victim.doSomething(bomb) ;

    That's perfectly legal, typesafe code. But if you were able to do what you
    want to do, it would end up with MainClass.doSomething being passed a
    ParentClass, when it is declared as taking ChildClass.

    You can get a similar effect with generics, though:

    public interface ParentInterface<T extends ParentClass> {
    public void doSomething(T param) ;
    }

    public class MainClass implements ParentInterface<ChildClass> {
    public void doSomething(ChildClass param) {
    // ...
    }
    }

    Would that do?

    tom

    --
    Not all legislation can be eye-catching, and it is important that the
    desire to achieve the headlines does not mean that small but useful
    measures are crowded out of the legislative programme. -- Select Committee
    on Transport
     
    Tom Anderson, Jul 9, 2008
    #6
    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. Emil Karlen
    Replies:
    1
    Views:
    840
    Martin Honnen
    Jul 13, 2004
  2. jon wayne
    Replies:
    9
    Views:
    735
    Jim Langston
    Sep 22, 2005
  3. Pavel Panchekha

    Overriding methods per-object

    Pavel Panchekha, Apr 18, 2009, in forum: Python
    Replies:
    11
    Views:
    581
    Pavel Panchekha
    Apr 19, 2009
  4. Replies:
    2
    Views:
    118
  5. Kenneth McDonald
    Replies:
    5
    Views:
    376
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page