hiding inherited members using the "new" keyword;

M

Mark Broadbent

Hi all. I've come across one of those things that doesn't work as I'd
expect. When inheriting a class, I have read that I can use the "new"
keyword to hide an inherited member and create a new implementation of
it -it is supposed to be possible to change the access level of the new
member. However what I have found is that if the member level is changed,
what we effectively get is a new member with a different access level (good)
AND the old member is inherited with its access level-see code below. What I
expected to see is that the old member SHOULD remain hidden in the object
instance of the inheriting class.

using System;

class inheritThisClass{

public string myString="from inheritThisClass";

public void myMethod(){

Console.WriteLine("inside class inheritThisClass");

}

public string myProperty{

set{

value="from inheritThisClass";

}

}

}

class useClass:inheritThisClass{

new private string myString="from useClass";

public string anotherString;

//constr to use private string

public useClass(){

anotherString=myString;

}


}

class UseClass{

public static void Main(){

useClass obj=new useClass();

Console.WriteLine("myString field="+obj.myString);

Console.WriteLine("anotherString field(set to useClass'
myString="+obj.anotherString);

Console.ReadLine();

}

}
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Mark,

I don't understand what you expect. It works as it suppose to.

The correct results are:
myString = "from inheritThisClass" - because the other is private. and is
not visible for UseClass. In other words myString declared in
inheritThisClass is the only member UseClass see and knows about.

On the other hand useClass constructor sees its myString and use it.

Hidding doesn't mean removing the base class' member.
You'll hide the member even without using the *new* modifier. *new* modifier
only suppress the compiler's warning message. Using *new* modifier you tell
the compiler "I know what I'm doing".
 
M

Mark Broadbent

from what I read I expected that hiding the member would do just that. I
know the inheriting class used a private access member -i just expected that
the new would prevent the base class member from being inherited (so in
effect would be overridden and converted to different visibility). I had a
suspicion that what you have said was the case, but from the material I read
it suggested the above.

Thx for responding.

--
Br,
Mark Broadbent
mcdba,mcse+i
=======================
Stoitcho Goutsev (100) said:
Hi Mark,

I don't understand what you expect. It works as it suppose to.

The correct results are:
myString = "from inheritThisClass" - because the other is private. and is
not visible for UseClass. In other words myString declared in
inheritThisClass is the only member UseClass see and knows about.

On the other hand useClass constructor sees its myString and use it.

Hidding doesn't mean removing the base class' member.
You'll hide the member even without using the *new* modifier. *new* modifier
only suppress the compiler's warning message. Using *new* modifier you tell
the compiler "I know what I'm doing".

--
HTH
B\rgds
100 [C# MVP]

Mark Broadbent said:
Hi all. I've come across one of those things that doesn't work as I'd
expect. When inheriting a class, I have read that I can use the "new"
keyword to hide an inherited member and create a new implementation of
it -it is supposed to be possible to change the access level of the new
member. However what I have found is that if the member level is changed,
what we effectively get is a new member with a different access level (good)
AND the old member is inherited with its access level-see code below.
What
I
expected to see is that the old member SHOULD remain hidden in the object
instance of the inheriting class.

using System;

class inheritThisClass{

public string myString="from inheritThisClass";

public void myMethod(){

Console.WriteLine("inside class inheritThisClass");

}

public string myProperty{

set{

value="from inheritThisClass";

}

}

}

class useClass:inheritThisClass{

new private string myString="from useClass";

public string anotherString;

//constr to use private string

public useClass(){

anotherString=myString;

}


}

class UseClass{

public static void Main(){

useClass obj=new useClass();

Console.WriteLine("myString field="+obj.myString);

Console.WriteLine("anotherString field(set to useClass'
myString="+obj.anotherString);

Console.ReadLine();

}

}



--
Br,
Mark Broadbent
mcdba,mcse+i
=======================
 

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