Saturday, July 21, 2018

When to Use Abstract Class in C#, Real World Example


It is a very common discussion when to use Abstract class and what is the benefit of using Abstract class in real time application development. The common properties of Abstract class are it cannot be initiated, functions and implementation can be partially implemented.
Consider an application that calculates salary of full time and contract based employees. There are few common properties of both employees, e.g., both employees have name, address, ID, but different way of calculating salaries. So we can declare one master class named as BaseEmployee and place common properties and Virtual Salary Calculator function with no implementation. Two child classes, FullTimeEmployee and ContractEmployee can inherit from BaseEmployee class and override Salary Calculator virtual function and our problem can be solved, then why Abstract Class should be used?

Difference between Abstract and Interface?


Interface                                                                              Abstract 

1. Interface support multiple inheritance.       1.It does not support multiple interface.

2.interface does not contain data member.      2. It contain data member.

3. Interface does not contain constructor.       3. it contains Constructor.

4. it contains only incomplete member. 
only declaration                                                4.  it contains both compl and incomplete member

5. it does not contains access modifier.       5. it contains access modifier for sub,function                                                                                                                                              properties.
everything by default public. 


6. Member of interface can not be static.    6. only complete member of abstract class cab be                                                                                   static.

What is Interface?

Interface:


* Interface is used as same as a class but interface contain no implementation. it have only declaration.

* interface by default is public. and it can not used access modifier.

* interface can not contain data member.

* interface support multiple inheritance.

* if interface class contain method and inherit from other class then all method must be implement in    a derived class.

An interface can never be instantiated e.g. ICar car = new ICar(); It’s wrong.
It only contains signature of its methods.
An interface has neither constructor nor fields.
It is also not permitted to have access modifier on its methods.
Its members are always implicitly public and cannot be declared as virtual or static.



      =========== what is explicit interface========

* a class inherit from  two interface it contain same method then how could identified the method.
then its used explicit interface.

Note:
 explicit implementation is that an explicitly implemented member cannot be accessed using a class instance,
 but only through an instance of the interface.



Example of Interface:

 class Program
    {
        static void Main(string[] args)
        {
            AbsA t = new TestA();
            t.MethodTest();
            Console.ReadKey();
        }
    }
    interface AbsA
    {
       void MethodTest();
    }
    public class TestA:AbsA{
        public void MethodTest(){
        Console.WriteLine("A");
        }
    }



Purposes of Interfaces:

1. Create loosely coupled software.
2. Support design by contract (an implementer must provide the entire interface).
3. Allow for pluggable software.
4. Allow objects to interact easily.
5. Hide implementation details of classes from each other.
6. Facilitate reuse of software.

Abstract class


What is Abstract class?.

The main Purpose of used abstract class is to provide default functionality of its subclassses.

when be declare abstract method in a base class the ever derived class must be declare own its definition.

Points to remember.


  • * it is madatory to override abstract method in a derived class.
  • * we can not create instance of abstrac classs.
  • * but it can be used as parameter in method.
  • Abstract classes are declared using the abstract keyword.
  • We cannot create an object of an abstract class.
  • If you want to use it then it must be inherited in a subclass.
  • An Abstract class contains both abstract and non-abstract methods.
  • The methods inside the abstract class can either have an implementation or no implementation.
  • We can inherit two abstract classes; in this case the base class method implementation is optional. 
  • .An Abstract class has only one subclass.
  • Methods inside the abstract class cannot be private(virtual member & abstract class can not be private)
  • If there is at least one method abstract in a class then the class must be abstract.
  • we can create Constructor or property in abstract class. 


Why can't create instance of an abstract class in C# 

No, you cannot create an instance of an abstract class because it does not have a complete implementation. 

 The purpose of an abstract class is to function as a base for subclasses. 
 It acts like a template, or an empty or partially empty structure,
 you should extend it and build on it before you can use it.



PERFORMENCE: abstract classes with virtual methods have better performance than interface implementation in the .NET Framework 4.0.

Note:1. Virtual and abstract member can not be private.
         2.  An abstract class cannot be a sealed class
         3 .Declaration of abstract methods are only allowed in abstract classes.
         4 .An abstract class can have abstract members as well non abstract members.
         5. We can create property in abstract class.


Example:

public abstract class AbsA
    {
        public abstract void MethodTest();
    }

    class TestB:AbsA {

        public override void MethodTest() {
            Console.WriteLine("TestA");
        }

    }










What is New, Virtual and Override keyWords


New Keywords:
It is used for Method hiding.

Method Hiding:
For hiding the base class method from derived class simply declare the derived class method with new keyword.

Virtual and overriding Keyboards.
Overriding the base class method in a derived class the base class method is used virtual keyword and derived class method is used override keyword.

Example: 

public class A{
public virtual void test(){

}
}
class B:A
{
public overide void test()
}





Tuesday, July 17, 2018

Polymorphism

1. What is Polymorphism:
=> Poly Means More then One Form

Real Life Example of Polymorphism.
A person at a same time can have different characteristic.
Like a man at a same time is a father, a husband, a employee.
So a same person posses have different behaviour in different situations.

Two Types of Polymorphism:

 1.Compile time polymorphism. ie early binding,or static binding:
 
Method Overloading:
advantage of CTP is execution will be fast.
example:= Overloaded method, overloaded orator.

2. Dynamic Polymorphism: late binding or Run Time Polymorphism
   Example of Method overriding


Method Overloading:
Same method Name with different type of parameter or different set of parameter in a same class is called method overloading.

Method overloading provides more than one form for a method. Hence it is an example for Polymorphism.


Method overriding:

Creating a method in a derived class of same signature as method in a base class is called method overriding.
same signature means method must have same name,same argument, and same type of argument.
method overriding is possible in only derived class not within same class.







Object Oriented Programming(oops) in C#

  1. What is Oops Concepts?
=>Object Oriented programming is programming model where program are organized into objects and data rather then action and logic.

  2. There are 4 Primary concepts of OOPs
  •          Abstraction
  •          Encapsulation
  •         Inheritance
  •         Polymorphism
  •  What is Abstraction?
      Abstraction is used to hide implementation details and display some essential feature.

  • Encapsulation:
  •    Encapsulation is used to hide data member and member function in single unit.
  •    Encapsulation is a way of encapsulating the data [wrapping the data] into a single unit called class. 
  • Class is the best example of encapsulation.
  • Encapsulation is implemented by using access specifiers. An access specifier defines the scope and visibility of a class member. C# supports the following access specifiers.

  1. Public: Access Anywhere
  2. Private: Accesses inside the class
  3. Protected: Access inside the class or also access in the derived class.
  4. Internal: Access inside the assembly.(within Application)
  5. Protected internal: Access inside the assembly or derived object in the assembly

 Real Life Example of Abstraction & Encapsulation:

Abstraction:

To implement abstraction let's take an example of a car. We knows a car, Car is made of name of car, color of car, steering, gear, rear view mirror, brakes, silencer, exhaust system, diesal engine, car battery, car engine and other internal machine details etc.

Now lets think in terms of Car rider or a person who is riding a car. So to drive a car what a car rider should know from above category before he starts a car driving.

Necessary things means compulsary to know before starting a car


1. Name of Car

2. Color of Car

3. Steering

4. Rear View Mirror

5. Brakes

6. Gear

Unnecessary things means  not that compulsary to know for a Car rider

1. Internal Details of a Car

2. Car Engine

3. Diesal Engine

4. Exhaust System

5. Silencer



     

Encapsulation:

you have seen important TV machine, TV connections and TV color tube is hidden inside the TV case which is not been exposed for viewers like us and exposed only neccessary things of a TV like TV Channel keys, TV volume keys, ON/OFF switch, Cable Switch and TV remote control for viewers to use it.This means TV machine, TV connections and TV color tube is an unwanted data and not needed for viewers to see is been hidden from outside the world.

So encapsulation means hiding the important features of a class which is not been needed to be exposed outside of a class and exposing only necessary things of a class.



Abstraction Encapsulation
Abstraction solves the problem in the design level.Encapsulation solves the problem in the implementation level.
Abstraction is used for hiding the unwanted data and giving only relevant data.Encapsulation is hiding the code and data into a single unit to protect the data from outer world.
Abstraction is set focus on the object instead of how it does it.Encapsulation means hiding the internal details or mechanics of how an object does something.
Abstraction is outer layout in terms of design. 
For Example: - Outer Look of a iPhone, like it has a display screen.
Encapsulation is inner layout in terms of implementation.
For Example: - Inner Implementation detail of a iPhone, how Display Screen are connect with each other using circuits














Saturday, July 7, 2018

what is .net

NET is a software framework which is designed and developed by Microsoft. 

Basically .Net is collection of Microsoft product. like c#, vb.net, f# etc.