Answer the question
In order to leave comments, you need to log in
Upcast: Accessing base class methods with different access modifiers from derived class after Upcast?
Hello. I don't know how to insert the code, but here's an example question:
There is a base class with valid methods:
class X
{... fields/properties...
public int Aaa();
protected int Bbb();
private int Ccc();
...}
And there is a child class in which the upcast is made (also a couple of methods for example):
class Y : X
{... fields/properties...
public string Sss();
protected string www();
private string Ttt();
...
Y petya = new Y();
X man = petya; // upcast
}
Questions:
1. Does pet lose all his Sss, Www, Ttt methods when he becomes a man? (even public Sss?)
2. What methods of the parent class does petya, who has become a man, have access to? Those. what is allowed: male.Aaa()? man.Bbb()? man.ccc()? (I'm especially interested in protected Bbb, because man.Bbb() gives me an error)
Thank you.
Answer the question
In order to leave comments, you need to log in
public methods can be called via cast:
if (мужчина is Y y)
{
y.Sss();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question