Class Inheritance Issue

M

mickey

Hi Group
I think that what I would like to do is not possible.
I would like to create a class with four possible sub classes which must
inherit data from the master class.
In the first class I would like to do some work which would then instantiate
one of the four sub classes without calling New in the first class.
The sub classes each has the potential for holding large amounts of data,
there for I don't want to instantiate them if not necessary.
When I do call the chosen sub class from the first class, I would like it to
inherit some of the data that the first classes created.

I hope this makes sense and I hope it is possible.

Thanks
Mickey
 
F

Family Tree Mike

Hi Group
I think that what I would like to do is not possible.
I would like to create a class with four possible sub classes which must
inherit data from the master class.
OK...

In the first class I would like to do some work which would then
instantiate one of the four sub classes without calling New in the first
class.

This "first class" is one of the four subclasses of "master class"? If
that is true, then the other subclasses have nothing to do with the New
in the "first class".

It sounds as "first class" is a factory class
(http://www.dofactory.com/Patterns/PatternFactory.aspx) for the other
three classes.
The sub classes each has the potential for holding large amounts of
data, there for I don't want to instantiate them if not necessary.
When I do call the chosen sub class from the first class, I would like
it to inherit some of the data that the first classes created.

I hope this makes sense and I hope it is possible.

Thanks
Mickey

If I understand you as described above, then this is possible. I'm not
sure why the "first class", which seems like a factory class would
inherit from the same base of the classes that it instantiates. A
typical construct might be:

class vehicle
end class

class car
inherits vehicle
end class

class truck
inherits vehicle
end class

class FordFactory
function Create_F150 as truck
end function
function Create_Focus as car
end function
end class
 
M

mickey

I don't think this is what I was looking for.
I want to create a base class that does some work and based on that work
instantiatiates one of four possible sub classes (child classes). That sub
clas should be 'aware of what the base class has done and be able to pick up
the work where the base class left off.
I.E.

sub main
{
base = new BaseClass
}

class BaseClass
{
object clsSub
read some part of a binary file
if file.info = 1
{
clsSub=new clsSub1
}
else if file.info=2
{
clsSub=new clsSub2
}
}

class Sub1:BaseClass
{
read rest of file as uints
}

class Sub2:BaseClass
{
read rest of file as uchars
}

Thanks
Mickey
 
F

Family Tree Mike

mickey said:
I don't think this is what I was looking for.
I want to create a base class that does some work and based on that work
instantiatiates one of four possible sub classes (child classes). That sub
clas should be 'aware of what the base class has done and be able to pick up
the work where the base class left off.
I.E.

sub main
{
base = new BaseClass
}

class BaseClass
{
object clsSub
read some part of a binary file
if file.info = 1
{
clsSub=new clsSub1
}
else if file.info=2
{
clsSub=new clsSub2
}
}

class Sub1:BaseClass
{
read rest of file as uints
}

class Sub2:BaseClass
{
read rest of file as uchars
}

Thanks
Mickey

You seem to be describing serialization/deserialization basically.

The standard way of doing that would be to call a deserialize method in the
subclass, which first calls the baseclass' deserialize method on the stream.
Once the base class is done, the subclass picks up reading its specific
property values. The structure would be more like this (typed freehand, you
may need overrides or some other method attributes):

class BaseClass
sub Deserialize(s as stream)
' read stuff specific to baseclass...
end sub
end class

class Sub1
inherits BaseClass
sub Deserialize (s as stream)
base.Deserialize(s)
' continue reading...
end sub
end class

The drawback is that you need to analyze the stream to see if it describes a
sub1, or sub2 instance up front. XML Serialization uses reflection to figure
this out. Maybe there is a method to scanning your file to determine the
type up front. That would be up to you to decide.

Mike
 
P

Phill W.

I would like to create a class with four possible sub classes which must
inherit data from the master class.

Subclasses will have access to anything in the base class defined as
Public, Protected or Friend (C#: public, family or assembly).
In the first class I would like to do some work which would then
instantiate one of the four sub classes without calling New in the first
class.

Only possible if your base class exposes a niladic constructor (i.e.
zero arguments). Subclasses [should] always call New on their base class.
The sub classes each has the potential for holding large amounts of
data, there for I don't want to instantiate them if not necessary.
When I do call the chosen sub class from the first class, I would like
it to inherit some of the data that the first classes created.

Two suggestions:
(1) Pass the "inherited" data to a method on the subclass. Yuck.

(2) Create a Shared (a.k.a. Static) method on the base class that gets
you an instance of the /required/ subclass. Then invoke a "Do
Processing" method on that. This method can, in turn, call [Protected]
methods on the base class.

Friend [MustInherit] Class Base1
Public Shared Function GetSubclass( ... )
If ( condition ) Then
Return New Subclass1( ... )
End If
Return Nothing
End Function

Public [MustInherit|Inheritable] Function DoWork() As ...
Return ...
End Function

End Class

Friend Class Subclass1
Inherits Base1

Public [Overrides] Function DoWork() As ...
' Do sub-class specific work
Return ...
End Function

End Class

HTH,
Phill W.
 

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

Top