Question about class inheritance

J

James Carnley

I am moving over from Java and have found something strange in the way
C# works. Can anyone explain this to me?

I have 4 classes which I all want to share one Print method yet each
class will have its own variables to be used in Print.

The Print statement just prints out the variable "num1". Class1 has
"num1" set to 1 so it prints 1. Class 2 has "num1" set to 2 yet it still
prints 1... why? Class 3 and 4 do this also. In java it prints out the
value of num1 in whatever class the mathod was called in.

The code is as follows:

using System;

namespace BlankTestProject
{
public class TestMain
{
public static void Main()
{
Class1 class1 = new Class1();
Class2 class2 = new Class2();
Class3 class3 = new Class3();
Class4 class4 = new Class4();

Console.WriteLine("These should all be different");

class1.Print();
class2.Print();
class3.Print();
class4.Print();

Console.WriteLine("\nPress enter to exit.");
Console.ReadLine();
}
}

public class Class1
{
int num1;

public Class1()
{
}

public void Print()
{
Console.WriteLine(num1);
}
}

public class Class2 : Class1
{
int num1 = 2;

public Class2()
{
}
}

public class Class3 : Class2
{
int num1 = 3;

public Class3()
{
}
}

public class Class4 : Class3
{
int num1 = 4;

public Class4()
{
}
}
}
 
J

Jon Skeet [C# MVP]

James Carnley said:
I am moving over from Java and have found something strange in the way
C# works. Can anyone explain this to me?

I have 4 classes which I all want to share one Print method yet each
class will have its own variables to be used in Print.

The Print statement just prints out the variable "num1". Class1 has
"num1" set to 1 so it prints 1. Class 2 has "num1" set to 2 yet it still
prints 1... why? Class 3 and 4 do this also. In java it prints out the
value of num1 in whatever class the mathod was called in.

It works *exactly* the same in Java. I've just converted your code to
Java (which is basically a case of changing : to extends and
Console.WriteLine to System.out.println) and the results are exactly
the same.

Your classes aren't setting the num1 variable from Class1 to 1, 2 or 3
- they're setting their own private variables (also called num1) to 1,
2 or 3. Each of your classes has two variables called num1 - one
inherited from Class1, and one which hides it.

As for "it still prints 1" - the code you posted doesn't print 1 at
all, it prints 0 for each of the cases. If you have some code which
prints 1, please post it, so we're all discussing the same code.
 
F

Frank Oquendo

James said:
I have 4 classes which I all want to share one Print method yet each
class will have its own variables to be used in Print.

By failing to declare an access modifier on Calss1.num1, you created a
private member. You then compounded the problem by declaring identical
private members within Class1-4. Had you not done that, the compiler
would have alerted you to the problem right away.

Remove num1 from each of the derived classes and change the access
modifier of Class1.num1 to protected. Initialize num1 in the constructor
of each class and run your app. You will get the expected output.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
J

James Carnley

Frank said:
By failing to declare an access modifier on Calss1.num1, you created a
private member. You then compounded the problem by declaring identical
private members within Class1-4. Had you not done that, the compiler
would have alerted you to the problem right away.

Remove num1 from each of the derived classes and change the access
modifier of Class1.num1 to protected. Initialize num1 in the constructor
of each class and run your app. You will get the expected output.


Thank you, changing the access modifier solved my problem. I have not
looked into it them very much but I need to understand them a lot better
I guess.
 

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