[thelist] more OOD fun

Matt Warden mwarden at gmail.com
Mon Oct 11 14:12:44 CDT 2004


Scott,

On Mon, 11 Oct 2004 11:44:38 -0700 (PDT), Scott Dexter
<dexilalolai at yahoo.com> wrote:
> have a different class using IPerson objects, but there are a couple
> instances where I want to access properties in one of the Person
> subclasses, outside the interface. My question is mutli-fold:
> 
> Where is the (better?) place to implement the interface? On the
> subclasses?

I don't really understand your question here. If the interface is for
a Person object, you should implement that interface with the Person
object. Subclasses of Person will automatically also implement that
interface. You aren't restricted to the interface unless you type your
variable by the interface (unless there is some oddity with C#). For
example:

interface Person
{
...
}

class PersonImpl implements Person
{
...
}

interface Student extends Person
{
...
}

class StudentImpl extends PersonImpl implements Student
{

}

If you declare a variable such:

Person student = new StudentImpl();

then, yes, of course you are going to be restricted to the Person
interface. But, if you declare it as such:

Student student = new StudentImpl();

then you will have the other methods that aren't in the Person
interface available to you.

> Do I include everything I access into the interface, despite the fact
> there may be items in there not used depending on the subclass?

No.

> The specifics: Person is subclassed into Guest and Guide classes. The
> Guest class extends Person by adding payment information and
> comments, items not in the Guide class. Now in the application, if I
> don't have teh payment information in the IPerson interface, I can't
> get to it, unless I declare the object as a Guest, which avoids the
> interface, which is not the best thing to do ... right?

If you are operating on Guest objects, then the object should be
declared as such:

void calculateSalesTax(Guest p_guest)
{
...
}

You should not declare it as such:

void calculateSalesTax(Person p_guest)
{
...
}

Because the method is specific to Guest objects. If you want to do
things the "best" way, you should have a Guest interface that extends
the Person interface.

> (Those who have been around a while may be surprised to find out this
> is the first time I've really done any OOD to this extent. Go figure)

Yeah, Mr. Started-Programming-When-I-Was-Nine.


-- 
Matt Warden
Miami University
Oxford, OH
http://mattwarden.com


This email proudly and graciously contributes to entropy.


More information about the thelist mailing list