Overriding methods with parent object parameters

L

Lew

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
 
J

jsguru72

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.
 
S

Stefan Ram

jsguru72 said:
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 ){} }
 
L

Lew

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 ) { ... }
 
J

jsguru72

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.
 
T

Tom Anderson

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top