![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
|
Reverse Inheritance
This may seem really stupid, but I am currently working on a project and it was going find but I then decided to rewrite some of my classes to make it easier to manage. Rather than only seperating my functions/classes into different categories, I decided to also seperate them by "level". I have low-mid-high level functions. High uses the mid level as mid uses the low level. Basically a form of abstraction.
Anyways, I was wondering if it was possible (or come close) to have a parent class that can do anything its child classes can, rather than the reverse which is how normal inheritance works. For example... Class Parent
{
public void Print()
{
Console.WriteLine("I am the Parent");
}
}
Class Child
{
public void Run()
{
Console.WriteLine("Watch me Run!");
}
}Parent p = new Parent(); p.Print(); p.Run() /* OR */ p.Child.Run(); Would I simply declare my child's functions as static or would that work? I know this seems silly but it actually makes sense to me. I can seperate my classes into seperate files rather than having one giant file with thousands of lines of code and I can do stuff like "Earth.Asia.Japan.Tokyo" where function names are seperated into larger->smaller categories. This scheme isnt nessisary for what I need though. Even "Earth.Tokyo" without all the inbetween stuff would work fine. Basically what I am asking is if it is possible to have a parent class that inherits everything from its children so that way I can simply have one class to worry about instead of creating instances of all these child classes. Is my thinking incorrect though? Is their some law of programming that states why something like this might be bad practice and why I should define all the child classes seperately? If I'm way off track, please tell. Thanks. |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Jan 2006
Posts: 3
Rep Power: 0
![]() |
There is a reason that inheritance only goes one way I cant really see why you would need a parent object to access methods of a child.
I think your code example is off too. If you are accessing Child from p then the code would have to look like: Class Parent
{
public void Print()
{
Console.WriteLine("I am the Parent");
}
Class Child
{
public void Run()
{
Console.WriteLine("Watch me Run!");
}
}
}are you just trying to seperate out subclasses? If you want Parent to access Childs methods simply have Parent inherit from Child. Class Parent:Child //the naming is a little confusing...
{
public void Print()
{
Console.WriteLine("I am the Parent");
}
}and you would access Childs method like this: Parent p = new Parent(); p.Run(); i hope this helps |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
That wasn't entirely what I was aiming for. My goal was to only have to be able to look after one object instead of creating a new object for each child class. Well, I'll stop being a baby and try having multiple objects (one for each child) and see if it works. I still think my idea wouldnt be a bad one in theory, but then again I may be wrong.
Thanks. |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
You may be thinking, perhaps, of inner classes.
class A {
static void printTest() {
System.out.println(test);
}
static class B {
static String test = "Hello World";
}
}Then you could do something like: A.B.test = "Goodbye World"; A.printTest(); |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|