Udemy

Interview Questions With Answers

Tuesday, June 09, 2015 0 Comments A+ a-


Question:

What is the difference between private and protected keyword.

Answer:

According to MSDN :

The protected keyword is a member access modifier. A protected member is accessible within its class and by derived class instances.

Here is example:


class A
{
    protected int x = 123;
}

class B : A
{
    static void Main()
    {
        A a = new A();
        B b = new B();

        // Error CS1540, because x can only be accessed by 
        // classes derived from A. 
        // a.x = 10;  

        // OK, because this class derives from A.
        b.x = 10;
    }
}

The statement a.x = 10; generates an error because it is made within the static method Main, and not an instance of class B.
Struct members cannot be protected because the struct cannot be inherited.

In following example, the class DerivedPoint is derived from Point. Therefore, you can access the protected members of the base class directly from the derived class.

Question:

What is the difference between shadowing and overriding ?

Answer:



.