Question about java inheritance

G

Gilliam

Hi

I had a problem about java inheritance that I do not know how to solve
in an elegant way.

I have a class named (let say) OriginalClass with a method method
called firstMethod which call secondMethod (also a method of
OriginalClass).

Then I have another class called ExtendedClass which extends the
Original Class. The firstMethod is the same as in the original, so it
is not modified, but the second is different and it is overriden.

The problem is that when calling ExtendClass.firstMethod and it get
executed it calls to OriginalClass.secondMethod instead of the
intended ExtendedClass.secondMethod.

If I just copy the first method into the extended class it will work,
but then I lose the benefits of inheritance.

How could I specify that I want to call the call from my extended
class??


Thanks in advance,

Jorge
 
P

Peter MacMillan

Gilliam said:
Hi

I had a problem about java inheritance that I do not know how to solve
in an elegant way.

I have a class named (let say) OriginalClass with a method method
called firstMethod which call secondMethod (also a method of
OriginalClass).

Then I have another class called ExtendedClass which extends the
Original Class. The firstMethod is the same as in the original, so it
is not modified, but the second is different and it is overriden.

The problem is that when calling ExtendClass.firstMethod and it get
executed it calls to OriginalClass.secondMethod instead of the
intended ExtendedClass.secondMethod.

If I just copy the first method into the extended class it will work,
but then I lose the benefits of inheritance.

How could I specify that I want to call the call from my extended
class??


Thanks in advance,

Jorge

You may want to post your code because the following works as expected:

//--- Inherit.java
public class Inherit {

public static class OriginalClass {
public void firstMethod() {
secondMethod();
}
public void secondMethod() {
System.out.println("A");
}
}

public static class ExtendedClass extends OriginalClass {
public void secondMethod() {
System.out.println("B");
}
}

public static void main(String[] args) {
OriginalClass oc = new OriginalClass();
oc.firstMethod(); // prints A

ExtendedClass ex = new ExtendedClass();
ex.firstMethod(); // prints B

OriginalClass ox = new ExtendedClass();
ox.firstMethod(); // prints B
}
}
//---
 
P

Peter MacMillan

Peter said:
public void secondMethod() {

Come to think of it, if you've defined secondMethod as *private* then
there are different inheritance rules. If you have then
OriginalClass.firstMethod only sees the private method of OriginalClass.
It can't "see" the overridden secondMethod.

Is that the case? that you've made secondMethod private instead of
protected or public?
 
M

Mike Schilling

Gilliam said:
Hi

I had a problem about java inheritance that I do not know how to solve
in an elegant way.

I have a class named (let say) OriginalClass with a method method
called firstMethod which call secondMethod (also a method of
OriginalClass).

Then I have another class called ExtendedClass which extends the
Original Class. The firstMethod is the same as in the original, so it
is not modified, but the second is different and it is overriden.

The problem is that when calling ExtendClass.firstMethod and it get
executed it calls to OriginalClass.secondMethod instead of the
intended ExtendedClass.secondMethod.

If I just copy the first method into the extended class it will work,
but then I lose the benefits of inheritance.

How could I specify that I want to call the call from my extended
class??

Ordinarily, you should get the behavior you're looking for. I see two
possibilities for why you wouldn't:

1. secondMethod had different signatures in the two classes, e.g.

secondMethod(int i) vs. secondMethod(long l)

In this case, they're different methods, and you won't override each other.

2. secondMethod is private. Private methods are *not* virtual. You'll
need to change the protection on it to protected, public, or package-private
(the default protection that requires no keyword) .
 
G

Gilliam

Thank you to Peter and Mike for the responses! I did not initially
post the code because it is pretty big. The problem was that the
secondMethod was private, as you both pointed out. When changing it to
protected everything worked fine.

Now it seems a very obvious mistake, but yesterday I could not find
it!

Thanks again,

Jorge
 

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

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top