Hidden members of a base class


B

beginwithl

hi



1)

a) One hides an inherited data member by declaring a new member of the
same type and with the same name. But what if the two members have
same name but are of different types?

* Is the inherited member still considered hidden?

* Is there a way we can access it from derived class without resorting
to base access expression?



b) Say we have the following code:

class A
{
public int a = 160;
}

class B : A
{
public byte a = 20;
}



static void Main(string[] args)
{
B b = new B();
A a = b;

byte num = 100;
b.a = num;
}


Now if ‘num’ has a value that fits into member ‘a’ ( one declared in
B ), then value of ‘num’ will be assigned to it. But if value of ‘num’
doesn’t fit into this member, then compiler recognizes this and
assigns it to the inherited member ‘a’.


* so is the basic rule ( at least when dealing with integral types ),
when having two members with same name ( one being inherited ), that
if assigned value fits into member declared in derived class, then the
value will always be assigned to that member?



c) why can’t compiler follow similar rules when dealing with the
following:


class C
{
public string c = "";
}

class D : C
{
public int c = 100;
}


static void Main(string[] args)
{
D d = new D();
d.c = " some_string " ; // error



* Why can’t compiler ( the way it did when the two members were of
integral type ) assign string value to the inherited member ‘c’?
Afterall, it was able to do something similar when dealing with
integral types!


thank you
 
Ad

Advertisements

P

PvdG42

hi



1)

a) One hides an inherited data member by declaring a new member of the
same type and with the same name. But what if the two members have
same name but are of different types?

* Is the inherited member still considered hidden?

* Is there a way we can access it from derived class without resorting
to base access expression?



b) Say we have the following code:

class A
{
public int a = 160;
}

class B : A
{
public byte a = 20;
}



static void Main(string[] args)
{
B b = new B();
A a = b;

byte num = 100;
b.a = num;
}


Now if ‘num’ has a value that fits into member ‘a’ ( one declared in
B ), then value of ‘num’ will be assigned to it. But if value of ‘num’
doesn’t fit into this member, then compiler recognizes this and
assigns it to the inherited member ‘a’.


* so is the basic rule ( at least when dealing with integral types ),
when having two members with same name ( one being inherited ), that
if assigned value fits into member declared in derived class, then the
value will always be assigned to that member?



c) why can’t compiler follow similar rules when dealing with the
following:


class C
{
public string c = "";
}

class D : C
{
public int c = 100;
}


static void Main(string[] args)
{
D d = new D();
d.c = " some_string " ; // error



* Why can’t compiler ( the way it did when the two members were of
integral type ) assign string value to the inherited member ‘c’?
Afterall, it was able to do something similar when dealing with
integral types!


thank you
The basic rule you seem to have missed is the use of meaningful, descriptive
identifier names in your code. Using the same name twice in your example
context(s) is a very poor coding practice. The reason your second example
throws an error is because there is no implicit conversion between the types
in question, where there is in the first.
 
A

Anthony Jones

hi



1)

a) One hides an inherited data member by declaring a new member of the
same type and with the same name.
<<<<<<<<<<<<<


No. Result type is not part of the member signature. If the member is a
method the types and order of the parameters along with the name form the
signature.

But what if the two members have
same name but are of different types?

* Is the inherited member still considered hidden?
<<<<<<<<<<<<<<

If talking of fields and properties yes. This also true for methods where
the rest of signature is the same but remember the returned type is not part
of the signature.

* Is there a way we can access it from derived class without resorting
to base access expression?

<<<<<<<<<<<<<<

No. the Base keyword is needed

b) Say we have the following code:

class A
{
public int a = 160;
}

class B : A
{
public byte a = 20;
}



static void Main(string[] args)
{
B b = new B();
A a = b;

byte num = 100;
b.a = num;
}


Now if ‘num’ has a value that fits into member ‘a’ ( one declared in
B ), then value of ‘num’ will be assigned to it. But if value of ‘num’
doesn’t fit into this member, then compiler recognizes this and
assigns it to the inherited member ‘a’.
This is not true. The only member with the name 'a' that can be accessed is
the one defined by B.

<<<<<<<<<<<<<<

* so is the basic rule ( at least when dealing with integral types ),
when having two members with same name ( one being inherited ), that
if assigned value fits into member declared in derived class, then the
value will always be assigned to that member?

No. The rule is that when a member is hidden it can only be accessed by the
class that directly hides it and only via the Base keyword. All other uses
of that name are on the derived class only.

<<<<<<<<<<<<<<<


c) why can’t compiler follow similar rules when dealing with the
following:


class C
{
public string c = "";
}

class D : C
{
public int c = 100;
}


static void Main(string[] args)
{
D d = new D();
d.c = " some_string " ; // error



* Why can’t compiler ( the way it did when the two members were of
integral type ) assign string value to the inherited member ‘c’?
Afterall, it was able to do something similar when dealing with
integral types!

<<<<<<<<<<<<<<<<

As described above the member 'c' of D is an int, external used of c will
only see D's c because C's c is hidden, thats why its called member
_hiding_.
 
B

beginwithl

hi

Before I made this thread I ran the above code ( or something
similar ) to see which member will be assigned a value, and somehow,
still not sure how, I got the impression that inherited member got the
value.

I apologize for taking your time and thank you for clearing things up.
 
Ad

Advertisements

A

Anthony Jones

hi

Before I made this thread I ran the above code ( or something
similar ) to see which member will be assigned a value, and somehow,
still not sure how, I got the impression that inherited member got the
value.

I apologize for taking your time and thank you for clearing things up.

I'm glad to help but there is no need to apologize. The purpose of NGs such
as this is so that the more experienced can help those who have less
experience. No one here is obligied to respond so any response given is
done so out of choice. If I felt my time was being wasted I wouldn't have
responded.
 

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