- An interface looks like a class, but has no implementation.
- An interface is a named set of method signatures.
- An Interface is a reference type and it contains only abstract members.
- An interface is an array of related function that must be implemented in derived type.
- Members of an interface are implicitly public & abstract.
- It can contain definitions of events, indexers, methods parameter less and parameterful properties.
- The interface can’t contain constants, data fields, constructors, destructors and static members.
- All the member declarations inside interface are implicitly public.
- Interfaces are great for putting together plug-n-play like architectures where components can be interchanged at will. Since all interchangeable components implement the same interface, they can be used without any extra programming.
Cannot be instantiated, means one cannot make a object of this class.
We can only define method definition and no implementation.
|Access modifiers are allowed||Access modifiers are not allowed|
|Some methods can be concrete||All methods are abstract|
|A class can inherit only one abstract class||A class can inherit many interface.|
|Can have any access modifiers||By default its public static final|
|Can have constructor and destructor||Cannot have constructor and destructor|
|Only one abstract class can be derived||Class can have multiple interfaces|
|Requires more time to find actual method in class||Faster|
|Abstract class provides 0 to 100% generalization||Interface provides 100% generalization|
|It provides both generalization and specialization||Interface provides only Generalization|
- Constructor is used to initialize an object (instance) of a class.
- Constructor is a like a method without any return type.
- Constructor has same name as class name.
- Constructor follows the access scope (Can be private, protected, public, Internal and external).
- Constructor can be overloaded, means we can have constructors with different set of parameters.
- We can always make the call to one constructor from within the other constructor.
- Only this and base keywords allowed in initializing constructors, other method calls will raise error.
Overloading the constructor using the this and base keywords so that it overload is called constructor chaining
- Static Constructor
- Default Constructor
- Private Constructor
- Copy Constructor
- Parameterized Constructor
- Used to prevent the user to instantiate the class directly.
- Used to prevent the creation of instances of a class when there are no instance fields or methods
- A private constructor is a special instance constructor.
- It is commonly used in classes that contain static members only.
- If a class has one or more private constructors and no public constructors, then other classes (except nested classes) are not allowed to create instances of this class.
- Note that if you don’t use an access modifier with the constructor it will still be private by default.
- Private constructors are used to restrict the instantiation of object using ‘new’ operator.
- This type of constructors is mainly used for creating singleton object.
- Can use nested class (Inner Class) or static method to initialize a class having private constructor.
- Example of Private Constructor – Math class
- Special constructor and gets called before the first object is created of the class.
- The time of execution cannot be determined, but it is definitely before the first object creation – could be at the time of loading the assembly.
- Static constructors might be convenient, but they are slow. The runtime is not smart enough to optimize them in the same way it can optimize inline assignments.
- The static constructor for a class executes before any of the static members for the class are referenced.
- The static constructor for a class executes after the static field initializers (if any) for the class.
- A static constructor cannot be called directly.
- The user has no control on when the static constructor is executed in the program.
- Example – When the class is using a log file and the constructor is used to write entries to this file.
- Only one Static constructor – Overloading needs the two methods to be different in terms to methods definition, so you can have at the most one static constructor
- Without parameters – It is going to be called by CLR, who can pass the parameters to it, if required, No one, so we cannot have parameterized static constructor.
- Access only static members – If allowed to work on non-static members, will reflect the changes in all the object instances, which is impractical. Non-static members in the class are specific to the object instance
- No access modifier – The call to the static method is made by the CLR and not by the object, so we do not need to have the access modifier to it.
- A default constructor is a constructor in both that has no parameters or where it has parameters they are all defaulted.
- If no constructor is supplied then the compiler will supply a default constructor.
- This default constructor is a parameter less constructor with no body, which calls the parameter less constructor of the base class.
- C# does not provide a copy constructor.
- A copy constructor is a special constructor used to create a new object as a copy of an existing object.
- This constructor takes a single argument: a reference to the object to be copied.
- It is a great convenience to create copy constructor for C# classes using Reflection.
- If you create a new object and want to copy the values from an existing object, you have to write the appropriate method yourself.
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 “. You inform the third party that these conditions apply to him/her and that he/she must comply with them.