Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# Inheritance and Polymorphism Guidance

Status
Not open for further replies.

spangeman

Programmer
Oct 16, 2004
96
EU
I am missing something here with this topic, can someone please explain it?

I have a base class

class BaseClass
{
public void Test()
{
MessageBox.Show("Test");
}
}

Two sub classes

class Class1 : BaseClass
{
public void TestOnlyInSubClass1()
{
MessageBox.Show("TestOnlyInSubClass1");
}
}

class Class2 : BaseClass
{
public void TestOnlyInSubClass2()
{
MessageBox.Show("TestOnlyInSubClass2");
}
}


I instantiate an object depending on user input and try to make the instantiating change depending on user input but it fails as MyObject can only use members in the base class and not the sub class.

BaseClass MyObject = new BaseClass();

string UserInput = "1";

if (UserInput == "1")
{
MyObject = new Class1();
}
else
{
MyObject = new Class2();
}

MyObject.TestOnlyInSubClass1() // this fails

Next thing I tried was not declaring it at the top but this didn’t work as the object was then out of scope.


//BaseClass MyObject = new BaseClass();

string UserInput = "1";

if (UserInput == "1")
{
Class1 MyObject = new Class1();
}
else
{
Class1 MyObject = new Class1();
}

MyObject.TestOnlyInSubClass1()

Can someone explain what I should be doing here I’m obviously getting the whole concept wrong.
 
If you have a base class and want to do that, the methods you want to call have to at least be virtual methods in the base class so that you can call them later (letting the child class override them to have them do whatever you want).

So you could define in the base class

Code:
public override void TestOnlyInSubclass()
{
  // specific stuff 
}

Then you can assign whatever subclass instance to that base class instance and call MyObject.TestOnlyInSubclass() and it will behave appropriately.

Do it like the first example you named, except for the changes I have outlined above and when you define

BaseClass MyObject;

Do it like that, don't instantiate it, only declare it. Instantiating it when you plan on using it like you are just wastes resources.

Then in each subclass do

Code:
public virtual void TestOnlyInSubclass()


----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
Here try this
class MainClass
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
BaseClass MyObject = new BaseClass();

string UserInput = "1";

if (UserInput == "1")
{
MyObject = new Class1();
}
else
{
MyObject = new Class2();
}

MyObject.Test();
}
}

class BaseClass
{
public virtual void Test()
{
Console.WriteLine("Test");
}
}

class Class1 : BaseClass
{
public override void Test()
{
Console.WriteLine("TestOnlyInSubClass1");
}
}

class Class2 : BaseClass
{
public override void Test()
{
Console.WriteLine("TestOnlyInSubClass2");
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top