- Public – Access to same assembly or another assembly that references it.
- Private – Access to same class or struct.
- Protected – Access to same class or struct, or in a class that is derived.
- Internal – Access to any code in the same assembly, but not from another assembly.
- Protected Internal – Access to any code in the assembly in which it is declared, or from within a derived class in another assembly.
|Class is a reference type||Struct is a value type|
|Class supports inheritance||Struct will not support inheritance|
|Class variables are stored on Heap||Struct variables are stored on Stack|
|Class can have destructor||Struct will not have destructor|
|Boxing a class object creates reference to same object||Boxing a strut object will create a copy in diff type|
|All the members of Class are private by default||All the members of struct are public by default|
|Class is well suited for Data hiding||Struts are not suited for Data hiding|
Both are user defined types.
Both of them can have constructor without parameter and with parameter.Both can have delegates and events.Both can have methods, properties , fields, constants , enumerations, events.
When we need to override a method of the base class in the sub class, then we give the virtual keyword in the base class method. This makes the method in the base class to be overridable. Methods, properties, and indexers can be virtual, which means that their implementation can be overridden in derived classes. You cannot use the virtual modifier with the following modifiers:
- Sealed classes are used to restrict the inheritance feature of object oriented programming.
- Once class is defined as sealed class, this class cannot be inherited so we can’t derive class.
- Keywords: C# – Sealed , VB.NET – NotInheritable
- If you have ever noticed, struct are sealed. You cannot derive a class from a struct.
- A sealed class cannot be used as a base class. For this reason, it cannot also be an abstract class.
- The best usage of sealed classes is when you have a class with static members.
- Example – The Pens and Brushes classes of the System.Drawing namespace. The Pens class represent the pens for standard colors.
- This class has only static members. Pens.Blue represents a pen with blue color. Similarly, the Brushes class represents standard brushes.
- The Brushes.Blue represents a brush with blue color.
- Polymorphism is one of the primary characteristics (concept) of object-oriented programming.
- Poly means many and morph means form. Thus, polymorphism refers to being able to use many forms of a type without regard to the details.
- Polymorphism is the characteristic of being able to assign a different meaning specifically, to allow an entity such as a variable, a function, or an object to have more than one form.
- Polymorphism is the ability to process objects differently depending on their data types.
- Polymorphism is the ability to redefine methods for derived classes.
- Compile time Polymorphism (method overloading)
- Run time Polymorphism (method overriding)
Method overloading means having two or more methods with the same name but with different signatures
Method overriding means having two or more methods with the same name , same signature but with different implementation. (Base class and Child class implementation of a method with same name and signature)
It is the ability to use all of the functionality of an existing class, and extend those capabilities without re-writing the original class. It is the process by which one object acquires the properties of another object. A new class that is created by inheritance is sometimes called a child class or a subclass. The class you originally inherited from is called the base class, parent class, or the superclass.
Disclaimer – F5debug Interview Questions & Answers Series:
You may recopy extracts from these pages (“the material”) to individual third party websites or to any intranet websites, but only if:
You acknowledge www.f5debug.net as the source of the material. Such acknowledgment should include reference to www.f5debug.net in the copy of the material and should also include “© Karthikeyan Anbarasan, www.f5debug.net “.