Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 16th, 2006, 12:37 PM   #1
Darkhack
Hobbyist Programmer
 
Darkhack's Avatar
 
Join Date: Dec 2005
Location: Kansas City
Posts: 105
Rep Power: 3 Darkhack is on a distinguished road
Send a message via AIM to Darkhack
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.
Darkhack is offline   Reply With Quote
Old Jan 16th, 2006, 1:00 PM   #2
pistalwhipped
Newbie
 
Join Date: Jan 2006
Posts: 3
Rep Power: 0 pistalwhipped is on a distinguished road
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
pistalwhipped is offline   Reply With Quote
Old Jan 16th, 2006, 1:20 PM   #3
Darkhack
Hobbyist Programmer
 
Darkhack's Avatar
 
Join Date: Dec 2005
Location: Kansas City
Posts: 105
Rep Power: 3 Darkhack is on a distinguished road
Send a message via AIM to Darkhack
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.
Darkhack is offline   Reply With Quote
Old Jan 16th, 2006, 5:05 PM   #4
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
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();
Arevos is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:51 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC