This is intended mainly for beginners to enforce their understanding about access modifiers used in .Net. We are going to discuss it in a question answer fashion. Read the below MSDN link which explains the topic and try to answer the questions.
http://msdn.microsoft.com/en-us/library/6tcf2h8w.aspx
http://msdn.microsoft.com/en-us/library/ms173121.aspx
- Is there any method even a hack, we can access private members from other class?
- If we can access private member via some mechanism what is the meaning of all these access modifiers?
- We are using access modifiers to restrict the access to fields and functions. Do we need any user name and password to access the variables?
- Can't we program if there is no concept of access modifiers in the language? What are the advantage of restricting access?
- Can we have same namespace in 2 different assemblies? If so can I access internal classes in other assembly which are in same namespace?
- Can we have a public method inside internal class? What is the specialty?
- Can a public method return an internal type?
- Can we have protected class in any scenario? Where all we can use it?
- There is a public class named A.Can I have a protected class in same name A inside class A?
- If we make the constructor as private, will that class be unusable for ever?
- There is a public method which returns a public interface. In the method body can we have 'return new ImplClass();' where the ImplClass is internal / protected / private?
- Can a public class inherit internal class?
- Can a class inherit a class which is declared inside the same class?
- Can a public class expose a property which is of internal class type?
- Are the access modifiers mutually exclusive in .Net? Can I have more than one access modifiers for single member?
- Does 'sealed' access modifier seal the class for ever? Will that class create dead code in the assembly?
- Can we have protected static methods in non static class?
- Can we have protected method inside static class?
- Can we have public static constructor in a non-static class?
- Can I declare static variable inside a method?
- Can we have a member variable in a class of same type? Will that cause infinite recursive loop?
- When I tried the above scenario, I got StackOverflowException? Why its so?
Initially I thought I should be writing 2 posts so that when trying to answer these questions you don't get the temptation to look at the answer. Later sometime, the thought process changed. We are all professionals and true professionals can never lie to his profession. We are not trying this to get marks or pass exam here. This is just for gaining knowledge or understanding our level of OOP knowledge. So answers are there in this post only.
- Yes there is. Using reflection we can access the private properties or call private methods of other class / object.
- .Net is managed the run time knows what are the types. But object orientation started before the managed environments. In that world it has sense. Even in .Net there are some framework classes whose members cannot be invoked via reflection.
- This is really childish when I asked this during one interview, he really started explaining about ASP.Net membership. He was a developer born into ASP.Net and according to him .Net is for ASP.Net. The access modifiers control the callers / consumers of the class in object creation and usage of it's members through objects. When we say via objects, it might be in a method in same inheritance hierarchy using 'this' or 'base' keywords.
- We can program even if there is no access modifiers in the language. In that case everything will be public. Below are the advantages of access modifiers
- Refactoring - Suppose we need to re-factor a method, by adding one more argument. If the method is private we need to care about only one class to get successful compilation. In case you are working in huge legacy code base (I am now working in a code base which is 10+ years old,contains more than 300 projects) and the method is public, getting compiled in first shot is a nightmare.
- Library development - There is no access modifiers in language and we are developing a library which has a method WeatherAtLocation GetWeatherAsync(location) for getting the weather information from a web service asynchronously and returns a structure which has the parameter location and the temperature. The method temporarily stores the location in a class level variable. Its obviously public. By the time our async web request to the actual web service completes the caller can change the value in member variable. So when we return the result it has a different location.
- No. Namespaces has nothing to do with internal access modifier.
- Yes we can. But effectively the access of that method will be internal as the contained type is internal.There is one blog post by Eric on this.
- No. The public method is supposed to call by classes in other assemblies. If it returns an internal type, which is restricted to classes in other assemblies, we are violating the internal
- Yes if we are declaring a class inside another class it can be protected. We can create object of this protected class in members of same class and all its derived class and its members regardless of assembly.
- Yes we can have. If we create object from outside, it will create object of outer class A. If the object creation is inside a member function of outer class A it will be the nested/protected class A.
- No. Private means accessible to all the members in the same class. If the class is public / internal, we can have a public / internal static method which creates the object and returns the same. Singleton uses this technique.
- Yes. The public method is returning public interface type. The interface members are permitted to be called from classes in other assemblies.
- No. If we do so, we are expanding the reach of internal.
- No. A class cannot inherit nested class inside it.
- No. The property is public but type is internal. So there is no meaning in returning the Type via property. Similar to Qn 7.
- Yes. Some are mutually exclusive. We can have protected internal as combination. It tells that the method can be accessed in any inherited class regardless of which assembly OR in any class inside same assembly.
- Sealed modifier for the declaration. If applied on class it means the class cannot be inherited.If on method it cannot be overriden.
- Yes we can have static protected members. Those will be accessible in same class or inherited classes.
- We cannot have protected instance methods. In fact no instance members. Also we cannot have protected static methods inside static class. There is no inheritance support.in static classes.
- No. Access modifiers are not allowed on static constructor as its not callable from our code. Static constructors are supposed to execute when that type is first used regardless of where from that call originated and to which member. So it doesn't make sense.
- No. This was supported in C. But not in .Net.
- Yes we can have a member variable inside the class which is of same type. That won't cause any infinite loop.Otherwise linked list would not have been possible.
- If we just declare, there wont be any issue. But if we try to instantiate that object in the constructor without any condition check or initiate the object in the same line of declaration, it will throw StackOverflowException.
When I started, I thought there would be limited questions. But there are many. May be the compiler team at Microsoft will be having near full list as their unit test cases. Once I get some time I will surely tag / group these questions as Static,Inheritance etc...
No comments:
Post a Comment