Classes
| ||||||||||||||||||||||||||||||||||||||||
What can be the members of class, struct and interfaces?
Class: (http://msdn.microsoft.com/en-us/library/0b0thckt.aspx)
· Fields
· Methods
· Indexers
· Events
· Classes
· Structs
Struct: (http://msdn.microsoft.com/en-us/library/ah19swz4.aspx)
constructors, constants, fields, methods, properties, indexers, operators, events, and nested types,
Interfaces : methods, properties, events, indexers (http://msdn.microsoft.com/en-us/library/ms173156.aspx)
| ||||||||||||||||||||||||||||||||||||||||
Can a static class contain non-static members?
NO. (http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.80).aspx).
The main features of a static class are:
· They only contain static members.
· They cannot be instantiated.
· They are sealed.
· They cannot contain Instance Constructors.
| ||||||||||||||||||||||||||||||||||||||||
Constructors:
- Constructors cannot be “virtual”. - They cannot be inherited. - Constructors are called in the order of inheritance. - Constructors can be overloaded. - special function that is called when class is instantiated. - same name as the class. - does not have a return type. - used to initialize values. - can have parameters. - compiler will supply one if not declared. | ||||||||||||||||||||||||||||||||||||||||
Can you define constructors for struct?
We cannot define default constructor for structs, because one is compulsorily provided by compiler.
However, we can define constructors for structs that take parameters.
Remember that the default constructor is called when we use new with struct. However, objects based on structs can be initialized or assigned and then used.
e.g. int I = new int(); //valid ; can be used
int i ; I = 33; //valid ; can be used
int I; // cannot be directly used without assigning a value, because new is not given, hence value is not initialized.
Can you define a default constructor for a strut ?
NO. It is compulsorily provided by compiler.
| ||||||||||||||||||||||||||||||||||||||||
In how many type value types can be created?
By calling new : int I = new int();
By assigning a value: int I = 44; //using new is not a requirement.
| ||||||||||||||||||||||||||||||||||||||||
A base class has no default constructor but has some parametered constructors. Will the derived class have to do something?
If a base class does not offer a default constructor, the derived class must make an explicit call to a base constructor by using base. (MSDN http://msdn.microsoft.com/en-us/library/ms173115.aspx#Y1200)
| ||||||||||||||||||||||||||||||||||||||||
Can constructors be inherited? NO.
| ||||||||||||||||||||||||||||||||||||||||
What is the order of calling constructors and destructors in an inheritance chain ?
Constructors : Least derived (base) to Most Derived.
Destructors : Most Derived to Least Derived. (http://msdn.microsoft.com/en-us/library/66x5fx1b(v=vs.80).aspx)
| ||||||||||||||||||||||||||||||||||||||||
Consider the following :
Class A {
Public A() {}
~A(){}
}
Class B : A {
Public B() {}
~B(){}
}
What will be the order of ctor/dtor in following cases ?
B b = new A(); //INVALID
A a = new B(); // ctor A, ctor B , dtor B , dtor A
| ||||||||||||||||||||||||||||||||||||||||
In a nested class, can you access members of container class ?
NOT DIRECTLY, you have to pass an instance of container to nested class either in nested class’s ctor or some other method.
| ||||||||||||||||||||||||||||||||||||||||
Destructors :
· Destructors cannot be defined in structs. They are only used with classes.
· A class can only have one destructor.
· Destructors cannot be inherited or overloaded.
· Destructors cannot be called. They are invoked automatically.
· A destructor does not take modifiers or have parameters.
| ||||||||||||||||||||||||||||||||||||||||
What are the differences between classes and structs ?
| ||||||||||||||||||||||||||||||||||||||||
Encapsulation
| ||||||||||||||||||||||||||||||||||||||||
What is the default access level of members in a class?
Private. This is applicable to constructor also, if a constructor is provided and no access specifiers are provided for it.
However, if you do not provide a default constructor to a class, the default parameterless constructor provided by the compiler is public.
| ||||||||||||||||||||||||||||||||||||||||
Can you inherit from a class which has only private constructors ?
No. It is not possible to inherit a class that has only a private constructor.
| ||||||||||||||||||||||||||||||||||||||||
Can you inherit from a static class ?
NO. Static classes are sealed and cannot be inherited. http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.80).aspx
| ||||||||||||||||||||||||||||||||||||||||
Can a static class contain not static methods?
| ||||||||||||||||||||||||||||||||||||||||
Inheritance
| ||||||||||||||||||||||||||||||||||||||||
What is class inheritance and interface inheritance?
| ||||||||||||||||||||||||||||||||||||||||
Can you inherit from a class which has only private constructors ?
No. It is not possible to inherit a class that has only a private constructor.
| ||||||||||||||||||||||||||||||||||||||||
What is the difference in virtual and abstract methods?
When talking about the base class, a virtual method may or may not contain implementation. But, an abstract method cannot contain implementation.
When talking about the (non-abstract) derived class, a virtual method may or may not be overridden, but an abstract method must be overridden ( or actually implemented).
| ||||||||||||||||||||||||||||||||||||||||
Polymorphism
| ||||||||||||||||||||||||||||||||||||||||
How many types of overloading are supported?
Method overloads and operator overloading.
| ||||||||||||||||||||||||||||||||||||||||
What is operator overloading?
|
Wednesday, July 6, 2011
Classes , Structs and Interfaces
Subscribe to:
Post Comments (Atom)
How to check local and global angular versions
Use the command ng version (or ng v ) to find the version of Angular CLI in the current folder. Run it outside of the Angular project, to f...
-
Most of the google tutorials on keras do not show how to display a confusion matrix for the solution. A confusion matrix can throw a clear l...
-
This error means you have created the DbContext but not configured/added it to the project using either DI in startup.cs or by using DbCon...
-
This happens when you dont define primary key for an entity type. Define a primary key either explicitly or implicitly.
No comments:
Post a Comment