Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Overriding methods with parent object parameters

0 views
Skip to first unread message

jsguru72

unread,
Jul 9, 2008, 10:19:30 AM7/9/08
to
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.


Lew

unread,
Jul 9, 2008, 2:36:17 PM7/9/08
to
On Jul 9, 10:19 am, jsguru72 <j...@storta.net> 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

Message has been deleted

jsguru72

unread,
Jul 9, 2008, 3:09:40 PM7/9/08
to
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.

Tom Anderson

unread,
Jul 9, 2008, 3:13:23 PM7/9/08
to
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

Lew

unread,
Jul 9, 2008, 9:26:57 AM7/9/08
to
On Jul 9, 10:19=A0am, jsguru72 <j...@storta.net> 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

0 new messages