Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

an inheritance question

Thread view: 
www - 23 May 2007 15:06 GMT
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!!
Patricia Shanahan - 23 May 2007 15:30 GMT
> 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
www - 23 May 2007 15:45 GMT
>> Hi,
>>
[quoted text clipped - 7 lines]
>
> 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.
Lew - 23 May 2007 15:50 GMT
> My boss gave me the layout:
>
[quoted text clipped - 16 lines]
>
> }

Your boss should learn Java before giving advice on it.

Signature

Lew

Mark Space - 23 May 2007 20:08 GMT
> like this:
>
[quoted text clipped - 9 lines]
>     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?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.