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
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