an inheritance question

W

www

Hi,

I am puzzled here by an inheritance question:

I have 3 classes:

public Parent {
...
}

public ChildA extends Parent {
...
//void doA() {
...
}
}

public ChildB extends Parent {
...
//void doB() {
...
}
}

Now, I am trying to use the classes above.

public Base {
protected _person;
...
}

public Sub extends Base {
_person = new ChildA();

_person.doA(); //WRONG !!!! error message: doA() is unresolved

}

I ran into this problem in my real application. What should I do? Some
type casting to force _person as ChildA ?

Thank you!!
 
P

Patricia Shanahan

www said:
Hi,

I am puzzled here by an inheritance question:

I have 3 classes:
....

I think you need to back up a level and tell us the problem you are
trying to solve with this structure.

Patricia
 
W

www

Patricia said:
...

I think you need to back up a level and tell us the problem you are
trying to solve with this structure.

Patricia

We have two stand alone Java classes. Each is working fine now. Similar
like this:

public Sub {
ChildA person = new ChildA();

person.doA();
}

public Sub2 {
ChildB person = new ChildB();

person.doB();
}


Now, we hope to refactor the code. We see Sub and Sub2 have something in
common and want to extract these common things and put them in an
abstract class Base. But doA() is only available for ChildA, doB() is
only available for ChildB. My boss gave me the layout:

public abstract Base {
protected Person _person;
...
}

public Sub extends Base {
_person = new ChildA();

_person.doA(); //WRONG !!!! error message: doA() is unresolved

}

public Sub2 extends Base {
_person = new ChildB();

_person.doB(); //also wrong!!!

}

After I finished the code, I just found out the errors. This is what I
am asking for the newsgroup.
 
L

Lew

www said:
My boss gave me the layout:

public abstract Base {
protected Person _person;
...
}

public Sub extends Base {
_person = new ChildA();

_person.doA(); //WRONG !!!! error message: doA() is unresolved

}

public Sub2 extends Base {
_person = new ChildB();

_person.doB(); //also wrong!!!

}

Your boss should learn Java before giving advice on it.
 
M

Mark Space

www wrote
like this:

public Sub {
ChildA person = new ChildA();

person.doA();
}

public Sub2 {
ChildB person = new ChildB();

person.doB();
}

Clean, simple and effective!
My boss gave me the layout:

Your co-workers aren't named Wally and Dilbert by any chance, are they?
 

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,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top