A simple inheritance question

  • Thread starter Thread starter bg_ie
  • Start date Start date
B

bg_ie

Hi,

I currently have a class called DefinedTest which relates to a set of
tests I perform. But there are two Test states, those that are defined
and those that are not. I'd therefore like to have two classes
instead, Test and DefinedTest with the former storing either Defined
or non-Defined Tests and the latter only defined tests.

The distinction between the two classes in terms of members is very
simple. A defined test has a file on the local computer which exists,
the undefined test has a file which is not present on the local
computer.

I therefore wish to have something like this -

DefinedTest
{
ExistingFile file;
}

Test
{
File file;
}

ExistingFile and File are classes I have writen also. ExistingFile is
inherited from File with the only difference being that ExistingFile's
constructor and set check that the file exists locally. If not, an
exception is called. I use this class a lot in my code as it confirms
that the file exists when the object is constructed and I therefore
don't have to worry about files being missing - of course the file
could be deleted after the constructer is called, but this is not an
issue for me.

So here's my question. DefinedTest and Test are identical in terms of
members, all except for the file member. Is it possible for me to have
DefinedTest as being inherit from Test? How would you implement this?

Thanks,

Barry
 
Hi,

I currently have a class called DefinedTest which relates to a set of
tests I perform. But there are two Test states, those that are defined
and those that are not. I'd therefore like to have two classes
instead, Test and DefinedTest with the former storing either Defined
or non-Defined Tests and the latter only defined tests.

The distinction between the two classes in terms of members is very
simple. A defined test has a file on the local computer which exists,
the undefined test has a file which is not present on the local
computer.

I therefore wish to have something like this -

DefinedTest
{
ExistingFile file;
}

Test
{
File file;
}
Given that the member-variable is set only while creation, I'd propose
following:

class Test
{
File file;

public Test (File file)
{
this.file = file;
}

.....
}

class DefinedTest
{
public DefinedTest (ExistingFile file) : base (file)

....
}


HTH

Christof
 
Well .... if Test can store either type of test and if ExistingFile inherits
from File why not just

Test
{
protected File file;
}

?

You could, if you wanted do this -

DefinedTest : Test
{
// And you have access to file in here
}

You maybe need to state what you're trying to achieve rather than what you
want your classes to look like.
i.e. "I want to run a test, and if the file is local I want to do this, but
if the file is not local I want this to happen ..." You may not even need
inheritance to address your problem.

HTH,

- Adam.
=========
 
[...]
So here's my question. DefinedTest and Test are identical in terms of
members, all except for the file member. Is it possible for me to have
DefinedTest as being inherit from Test? How would you implement this?

The two replies seem to offer a good start, assuming you really want
this to be done via inheritance. But as Adam says, are you really sure
you need to be represented through a derived class?

It seems to me that having a separate property on a single class,
indicating whether the Test is Defined or not would make more sense.
The property could simply be initialized (by setting a private field)
during construction by checking for the file existence, or the
constructor could include a parameter indicating whether the file must
exist or not (and then throw an exception if the desired state doesn't
match the actual state).

That said, if you really feel it makes your code better to have the
"Defined" nature of the object represented by the type, I'd agree that
the suggestion provided already to simply have a single File member
(private field, and public property if you need access to the data
outside the class) seems fine to me. If you have some specific
reasoning behind storing the filename differently depending on which
class it's in, then perhaps you could elaborate on that.

Pete
 
Back
Top