473,387 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

OOP concept - derived classes -C# question

Hi all
I have a class Parent, and two child classes class Child1
and class Child2 derived from Parent class.

In Parent class there is a method that will instantiate
child classes based on the input parameters it received.

I am able to instantiate my child class from the Parent
class, but not able to call the child class methods from
the Parent class by doing this.

Child1 ch1 = new Child1();
ch1.SomeMethod();

I think by changing the access level of SomeMethod() to
something like internal, I will be able to call the child
method from the parent class. But, I need to know if this
is the right way to do? Does it violate any OOP
recommendations? or can you recommend some other best
pratice? Thanks a lot for your help.
Nov 16 '05 #1
7 1577
Don't worry, using public methods and properties is OOP, accessing
variables in the child directly is not.

When you want the child to do anything you have to tell it so. If there
was no way to tell it anything (no public methods or properties) how could
you expect it to do anything.

Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2

"Morten Wennevik" ...
Don't worry, using public methods and properties is OOP,
accessing variables in the child directly is not.

When you want the child to do anything you have to
tell it so. If there was no way to tell it anything
(no public methods or properties) how could
you expect it to do anything.


However, I believe the OP should think one more time about the design.

I can't tell from just the example, but forcing the superclass
to know about it's subclasses is in most cases a sign of bad
design.

// Bjorn A
Nov 16 '05 #3
Thanks for all your replies. can you please tell me what
would be a better design? My goal is to be able to call
different child classes based on conditions.
-----Original Message-----

"Morten Wennevik" ...
Don't worry, using public methods and properties is OOP,
accessing variables in the child directly is not.

When you want the child to do anything you have to
tell it so. If there was no way to tell it anything
(no public methods or properties) how could
you expect it to do anything.
However, I believe the OP should think one more time

about the design.
I can't tell from just the example, but forcing the superclassto know about it's subclasses is in most cases a sign of baddesign.

// Bjorn A
.

Nov 16 '05 #4
Bjorn Abelli wrote:
"Morten Wennevik" ...
Don't worry, using public methods and properties is OOP,
accessing variables in the child directly is not.

When you want the child to do anything you have to
tell it so. If there was no way to tell it anything
(no public methods or properties) how could
you expect it to do anything.


However, I believe the OP should think one more time about the design.

I can't tell from just the example, but forcing the superclass
to know about it's subclasses is in most cases a sign of bad
design.

// Bjorn A


Agreed. Typically the parent shouldn't care about specific items in any
derived classes, especially if there is a requirement that the class on
which the parent is invoking the method must be a child. If the original
poster will provide more details on what they are trying to accomplish,
possibly a better design pattern can be suggested. A guess might be that a
virtual method in the parent that is overriden in the child classes might be
what is desired, but impossible to say for sure without specifics.
--
Tom Porterfield
MS-MVP MCE
http://support.telop.org

Please post all follow-ups to the newsgroup only.

Nov 16 '05 #5
Actually you are completely right. I was thinking of MDIParent and Child
and not inheritance at all.

Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #6
an*******@discussions.microsoft.com wrote:
Thanks for all your replies. can you please tell me what
would be a better design? My goal is to be able to call
different child classes based on conditions.


Look at the (Abstract) Factory Design pattern:

http://www.dofactory.com/patterns/PatternAbstract.aspx

Cheers,

--
Joerg Jooss
jo*********@gmx.net
Nov 16 '05 #7
<an*******@discussions.microsoft.com> wrote in news:1d96c01c423cd$59470170
$a*******@phx.gbl:
Hi all
I have a class Parent, and two child classes class Child1
and class Child2 derived from Parent class.

In Parent class there is a method that will instantiate
child classes based on the input parameters it received.

I am able to instantiate my child class from the Parent
class, but not able to call the child class methods from
the Parent class by doing this.

Child1 ch1 = new Child1();
ch1.SomeMethod();

I think by changing the access level of SomeMethod() to
something like internal, I will be able to call the child
method from the parent class. But, I need to know if this
is the right way to do? Does it violate any OOP
recommendations? or can you recommend some other best
pratice? Thanks a lot for your help.


Your idea of having the parent creating childs (Despite the
similarities in nature ;)) is not that great. Parent-child relations have
to be seen as: child is-a parent, and thus adds specialization to parent.

It's also not appropriate to call methods of a child class in a
parent. If you want to do that, you should do it like this:

public class Parent
{
//...

public void Foo()
{
object var = Bar();
// other code
}

public abstract object Bar();
}

now, in child1 and child2, you 'plug in' the code for Bar:
public class Child1:Parent
{
//...

public override object Bar()
{
object var=null;

// do some stuff

return var;
}
}

This way you can call code in parent which is implemented in a
child class. This is called the 'strategy pattern'.

If you want to produce various classes, you should use a factory
class. A factory simply creates objects based on input and doesn't have
any relation to the objects created.

Frans.

--
Get LLBLGen Pro, the new O/R mapper for .NET: http://www.llblgen.com
My .NET Blog: http://weblogs.asp.net/fbouma
Microsoft C# MVP
Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Shao Zhang | last post by:
Hi, I am not sure if the virtual keyword for the derived classes are required given that the base class already declares it virtual. class A { public: virtual ~A();
8
by: Gonçalo Rodrigues | last post by:
Hi all, I have a template class (call it Object) whose instances have a variable size part - an array of of T objects. But this variable size part is fixed at creation time so instead of...
14
by: Jason Heyes | last post by:
I use a template class called Derived to instantiate derived classes of class Abstract. Is this an OO concept? Here is some code to illustrate: #include <iostream> class Abstract { public:...
3
by: J.J. Feminella | last post by:
(Please disregard the previous message; I accidentally sent it before it was completed.) I have source code similar to the following. public class Vehicle { protected string dataV; // ......
1
by: Mark McDonald | last post by:
This question kind of follows on from Mike Spass’ posting 10/11/2004; I don’t understand why you can’t declare an implicit operator to convert a base class to a derived class. The text...
6
by: John Glover | last post by:
I'm having a very strange problem with XML serialization. I'm writing web services which pass instances of various classes back and forth as parameters and return values of web methods. The...
9
by: desktop | last post by:
On this page: http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html Shape specify the virtual function: virtual double Intersect( const Shape& s) = 0; then the derived class Circle...
15
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
2
by: developer.new | last post by:
Hi I have a question regarding this concept I learned about recently: Name Hiding. Here's what I've come across: There is a base class with two functions with the same name but different...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.