Basics of Object Oriented Programming
Base Class:
When we have few common functionalities between 2 or more related classes then we can move the common functionalities to the base class. Base class is a way
to group objects which share a common set of functionality.
Abstract Class:
If a class is marked as abstract class then you can't create an instance of it. A abstract class can have a combinations of implemented methods and abstract methods. A non-abstract class can't have abstract method
Understanding Interface & Abstract Class
An abstract class creates an "is-a" relationship. Cat is an Animal.An interface creates a "can-do" & "Has" relationship. Cat can Run & Cat has legs.
You can only have property & method declaration in interface with no implementation. The class which is implementing that interface has to provide the
implementation of all properties & methods.
Using Abstract we can not achieve multiple inheritance but using an Interface we can achieve multiple inheritance (A class can only inherit one class but
can inherit multiple interfaces ). We can not declare a member field in an Interface.We can not use any access modifier i.e. public , private , protected ,
internal etc. because within an interface by default everything is public. An Interface member cannot be defined using the keyword static, virtual, abstract or sealed. An interface can inherit from another interface only
An abstract class can have constructor & it will be invoked when the derived class constructor is called. You can call the abstract method in the constructor of an abstract class and it will invoke the derived class implementation of that method when the derived class constructor is called.
If a class is marked as sealed then no other class can inherit it.
Method Overriding:(Polymorphism)
The virtual keyword is used to modify a method, property, indexer, or event declaration and allow for it to be overridden in a derived class.The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event.Overriding is resolved at run time. By default, methods are non-virtual. You cannot override a non-virtual method.You cannot use the virtual modifier with the static, abstract, private, or override modifiers. You cannot override a non-virtual or static method. The overridden base method must be virtual, abstract, or override. You should use the New keyword in the method of derived class if hiding is intended and you want to invoke the base class method at runtime.
Refer for example: http://www.codeproject.com/Articles/816448/Virtual-vs-Override-vs-New-Keyword-in-Csharp
Method Overloading:
It is the mechanism to have more than one method with same name but with different signature (parameters). Overloading is resolved at compile time. A method can be overloaded on the basis of following properties:
- Have different number of parameter
- Having same number of parameters but of different type
- Having same number and type of parameters but in different orders
A method cannot be overloaded on the basis of:
- Different return type
- Different access modifier
- Normal and optional parameters
Operator overloading provides a much more natural way of implementing the operations on custom types. The operator function should be a member function of the containing type.The operator function must be static.The operator function must have the keyword operator followed by the operator to be overridden.The arguments of the function are the operands.The return value of the function is the result of the operation.
public static Complex operator +(Complex c1, Complex c2) {
return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}
The comparison operators, if overloaded, must be overloaded in pairs; that is, if == is overloaded, != must also be overloaded. And similar for < and >, and for <= and >=
Access Modifiers:
public: The type or member can be accessed by any other code in the same assembly or another assembly that references it.
private: The type or member can be accessed only by code in the same class or struct.
protected: The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.
internal: The type or member can be accessed by any code in the same assembly, but not from another assembly.
protected internal: The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly.
Interfaces can be declared as public or internal, interfaces default to internal access. Interface members are always public. No access modifiers can be
applied to interface members.Enumeration members are always public, and no access modifiers can be applied.Delegates by default have internal access when declared directly within a namespace, and private access when nested.
Inheritance, Association, Aggregation & Composition:
Is a type of relationship, then inheritance
Using each other relationship but have their own object life, then association
Using relationship with parent but have their own object life, then Aggregation
Death relationship with parent, then composition
Association is a *has-a* relationship between two classes where there is no particular ownership in place. It is just the connectivity between the two classes.Aggregation is a weak type of Association with partial ownership. For an Aggregation relationship, we use the term *uses* to imply a weak *has-a* relationship.Composition is a strong type of Association with full ownership. This is strong compared to the weak Aggregation. For a Composition relationship, we use the term *owns* to imply a strong *has-a* relationship.
Refer Link: http://www.codeproject.com/Articles/330447/Understanding-Association-Aggregation-and-Composit