Saturday, 2 May 2015

What is a Interface in C# .Net

An interface looks like a class, but has no implementation.The reason interfaces only provide declarations is because they are inherited by classes and structs, which must provide an implementation for each interface member declared.

Properties Of Interface:-

  1. Supports multiple inheritance(Single Class can implement multiple interfaces).
  2. Can not Contains data members.
  3. By Default interface members is public (We Can not set any access modifier into interface members).
  4.  Interface can not contain constructors.



Interface IMyInterface
{
    void MethodToImplement();
}

Interface is a type which contains only the signatures of methods, delegates or events, it has no implementation. Implementation of the methods is done by the class that which implements the interface.
Interface is a contract that defines the signature of the functionality. So if a class is implementing a interface it says to the outer world, that it provides specific behavior. Example if a class is implementing ‘Idisposable’ interface that means it has a functionality to release unmanaged resources.

If a class implements a interface then it has to provide implementation to all its methods.



Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using System.IO;
Using System.Globalization;
Using System.Data;

Namespace ConsoleDemoApp
{
    Class Test
    {
        /* program to implement interface in c# */

        Public static void Main(string[] args)
        {
            double dueAmt, ServiceCharge, Receipt;

           
            Console.Write("\nEnter Due Amount : ");
            dueAmt = Convert.ToDouble(Console.ReadLine());
            Console.Write("\nEnter Service Charge: ");
            ServiceCharge = Convert.ToDouble(Console.ReadLine());

            Payment pay = new Payment();
            // make payment
            pay.PayDueAmt(dueAmt, ServiceCharge);
            //print payment history
            Receipt = pay.getTotalPaidAmt();
            Console.Write("\nTransaction is successful");
            Console.Write("\nTotal paid amount is : {0}", Receipt);
            Console.ReadKey();
        }
    }

    // Interface
    public interface IPayment
    {
        // member of interface
        Void PayDueAmt(Double dueAmt, Double serviceCharge);
        Double getTotalPaidAmt();
      
    }

    // class Payment implements the above interface
    Public class Payment : IPayment
    {
        Double paidAmt, ServiceCharge, Total;
        // method 1
        Public void PayDueAmt(Double dueAmt, Double serviceCharge)
        {
            this.paidAmt = dueAmt;
            this.ServiceCharge=serviceCharge;
        }
        // method 2
        Public Double getTotalPaidAmt()
        {
            this.Total = this.paidAmt + this.ServiceCharge;
            Return this.Total;
        }
    }
}


  Output














Summary
You now understand what interfaces are. You can implement an interface and use it in a class. Interfaces may also be inherited by otherinterface. Any class or struct that inherits an interface must also implement all members in the entire interface inheritance chain.

Friday, 1 May 2015

What is an Abstract class in c# ?

Here, first am expaling the comcept of Abstract class
Abstract classes, marked by the keyword abstract in the class definition, are typically used to define a base class in the hierarchy. What's special about them, is that you can't create an instance of them - if you try, you will get a compile error. Instead, you have to subclass them, as taught in the chapter on inheritance, and create an instance of your subclass. So when do you need an abstract class? It really depends on what you do.

Why we need to define Abstract classes ?
The purpose of abstract class is to provide default functionality to its sub classes.
When a method is declared as abstract in the base class then every derived class of that class must provide its own definition for that method.
 An abstract class can also contain methods with complete implementation, besides abstract methods.
When a class contains at least one abstract method, then the class must be declared as abstract class.
It is mandatory to override abstract method in the derived class.
When a class is declared as abstract class, then it is not possible to create an instance for that class. But it can be used as a parameter in a method.

The  example below  creates three classes called Calculation1, Circle and Rectangle where circle and rectangle are inherited from the class Calculation1and implements the methods getArea() using override keyword and call the base class methods getCircle_Circumference() and geRectangle_Circumference that are implemented in abstract class Calculation1 itself and as Calculation1 class contains abstract methods so it is declared as abstract class.


Source code example in c# 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Globalization;
using System.Data;

namespace ConsoleDemoApp
{
    class Test
    {
        /* program to write a exception in c# */

        Public static void Main(string[] args)
        {
            Double length, width, radius;

           
            Console.Write("\nEnter Length of a Rectangle: ");
            length = Convert.ToDouble(Console.ReadLine());
            Console.Write("\nEnter Width of a Rectangle: ");
            width = Convert.ToDouble(Console.ReadLine());
            Console.Write("\nEnter the Radius of Circle : ");
            radius = Convert.ToDouble(Console.ReadLine());

            /* Class Rectangle */
            Rectangle rec = new Rectangle(length, width);
            // getting Area rectangle
            Double area = rec.getArea();
            Console.Write("\nArea of a Rectangle is: {0}\n", area);
            // getting Circumference
            Double Circum = rec.getCircum();
            Console.Write("\nCircumference of Rectanle is: {0}\n", Circum);
          

            /* Class Circle */
            Circle cl = new Circle(radius);
            // getting Area of circle
            Double CircleArea = cl.getArea();
            Console.Write("\nArea of Circle is: {0}\n", CircleArea);

            Double CircleCircum = cl.getCircum();
            Console.Write("\nCircumference of Circle is: {0}\n", CircleArea);

            Console.ReadKey();
        }
    }

    Public abstract class Calculation1
    {
      Public Double length, width, radius;

        // abstract function it must be override in derived class
        Public abstract Double getArea();

        // Common function 1
        Public  Double getCircle_Circumference(Double Radius)
        {
            Double result = 2 * 3.14 * Radius;
            return result;
        }

        // Common function 2
        Public Double geRectangle_Circumference(Double length, Double width)
        {
            Double result = 2 * (length * width);
            Return result;
        }

    }

    Public class Circle : Calculation1
    {
        // constructor method to asign calss variables
        Public Circle(Double Radius)
        {
            this.redius = Radius;
        }

        // overriding the base class function
        Public override Double getArea()
        {
            Double area = 3.14 * this.redius * this.redius;
            return area;
        }

        // calling base class method
        Public Double getCircum()
        {
            Double circumference = this.getCircle_Circumference(this.redius);
            Return circumference;
        }
       
    }

    Public class Rectangle : Calculation1
    {

        // contractor to asign class variables
       Public Rectangle(Double length, Double width)
        {
            this.length = length;
            this.width = width;
        }

        // overriding the base class function
        Public override Double getArea()
        {
            Double area =this.length * this.width;
            Return area;
        }

        // calling base class methods
        Public Double getCircum()
        {
            Double circum = this.geRectangle_Circumference(this.length , this.width);
            Return circum;
        }

    }
}

 Output















Note:-

In above example I created the Constructor in both derived class to asign the variables of Abstract base calss.

.