Monday, June 28, 2010

Abstract classes

A class which has atleast one abstract method is known as an Abstract class.
! Abstract Method - A method which has only method declaration but no method definition is known as an Abstract method.
! An abstract class cannot be instantiated by the keyword new.
! An abstract class should have atleast one child class, using child class instance variables only we can access abstract class features.
! The base class can be overridden by using 'override' keyword in derived class.
! An abstract method cannot also use the modifiers static or virtual.
! An abstract class cannot be sealed.
! Partially Abstract classes.
! Abstract class features we have to inherit to the child classes.

Inheritance in .Net

Inheritance is the concept of reusability. Using inheritance one class feature we can access in the other classes.
##5 Types of Inheritance

! Single Inheritance - One base class --> One child class directly

! Multilevel Inheritance - Base class --> Child class (d1) --> Child class(d2)

! Hierarchical Inheritance - Base class ---- Child class (c1)
Child class (c2)

! Multiple Inheritance - Base class(b1)
--- Child class
Base class (b2)


! Hybrid Inheritance - Altogether, C# doesn't support Hybrid inheritance always child classes having multiple duplicate copies getting ambiguos error.
To overcome this error we have to work with virtual base classes. Virtual base classes derive only one copy to the child classes.

Types of parameters

*Value type - Using these parameters we can send actual parameter value to the formal parameters.
! The formal parameters modifications doesn't affect in the actual parameters.

*Reference type - Using these parameters we can send actual parameter address into the formal parameters.
! The formal parameters modifications affect in the actual parameter.

*Out Type - Using Out parameter we can send formal parameter value into the actual parameter, while working with these parameters we have to initialise values to the formal parameters, out type parameters function can return collection of values.

#'ref' you can read the value of the variable as well as write to it.
#'out' you can only write i.e., you can only change the value of the variable, you cannot read it.

Difference between Value and Reference DataTypes

##Value Types

Simple variables in a class
Memory allocated on the stack
Primitive data types such as int, interface, event, and delegate.
Their existence is till they are called in a class
System.Valuetype

##Ref types
Needs to be instantiated using a 'new' keyword.
Memory allocated on the heap.
Reference types are data types such as class, char, and float.
They exist for a long period of time.
System.Object

What are Class Methods

Which have Static modifiers -- Methods which are invoked by a class name. They can access class variables & class methods but cannot access instance variables & instance methods.

Access Specifiers -- Scope of the variables

->Public -- Public access modifier can be set to a class / method / property to make it visible to any class / any assembly.
->Private -- Private can be set to those fields which are within the class and they are used to hide from outside of the class.
->Protected -- Protected is used for immediate child classes.
->Internal -- Internal is used specifically for within the assembly.
->Protected Internal -- It is used for within the assembly and assembly that inherits this assembly (Shared Assembly).
->Static @ Modifier -- Static member belongs to the class and as such we don't need to create an object to access it.
->Non-static member - Default, can be accessed only via an object of the class.
Real time: WriteLine() is a static function in Console as we did not create an object that looks like Console to access it.

What are Constructors

! Constructors are similar to methods.
! They always take the name of the class but no return type.
! Constructors cannot be called/ inherited can only be invoked by 'new' operator.
! They allocate memory to all of class/member variables to keep default values in it.
! Class has one single constructor & it is private and can never be instantiated.
! Static Constructors -- Used for initializing only static members of a class..These are invoked very first time class is loaded in memory. No args. No Access Modifiers.
! Non-Static Constructors -- Used for initializing static & non-static methods. Invoked every time a new object is created.

Differences between 'const' and 'read-only' in C#:

'Const': 'Read-only':
•Can't be static. _______________ •Can be either instance-level or static.
•Value is evaluated at compile time. _______________ •Value is evaluated at run time
•Initialized at declaration only. _______________ •Can be initialized in declaration or by code in the constructor.

What are Constants in .Net

Constant is a value that remains unchanged in a class.
! They are specific to classes not to objects.

Methods and Events

Methods - It defines behavior of a class. Main method has one entry point.


Events - Events are specifically for a user action or application action i.e., event is like an action to be performed in an application.