Wednesday, 19 August 2015

What is an Abstract Method in C#.Net

 Abstract Method :
                                    A Method without any method body is known as an Abstract Method. It contains only Declaration of the method. To declare an abstract method we need to use Abstract modifier.
Example : public abstract void Add(int x, int y);
             
                                The class under which we declare abstract methods is known as an abstract class should be declared using Abstract modifier.


Example :
                   The concept of Abstract Method is near similar to a concept of virtual methods (method overriding). In case of virtual methods child class can re-implement the method is the override modifier.Which was only Optional for the child classes. Where as in case of abstract methods the implementations of the method declared in a class should be given by it's child class which was mandatory for the child classes.
                     Either while implementation or re-implementation method under child class we use the same modifier.


Example :   Abstractparent.cs



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication1
{
    abstract class Absparent
    {
        public void add(int x, int y)
        {
            Console.WriteLine(x + y);
        }
        public void sub(int x, int y)
        {
            Console.WriteLine(x - y);
        }
        public abstract void mul(int x, int y);
        public abstract void div(int x, int y);
    }


    }

AbstractChild.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Abschild :Absparent
    {
        public override void mul(int x, int y)
        {
            Console.WriteLine(x * y);
        }
        public override void div(int x, int y)
        {
            Console.WriteLine(x / y);

        }
        static void Main()
        {
            Abschild c = new Abschild();
            c.add(100, 500);
            c.sub(200, 100);
            c.mul(100, 100);
            c.div(200, 100);
            Console.ReadLine();
        }
    }
}

OutPut :

Another Example : 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    abstract class TwoDShape
    {
        double pri_width;   // private
        double pri_height;  // private
        string pri_name;    // private

        // A default Constructor
        public TwoDShape()
        {
            width = height = 0.0;
            name = "null";
        }

        // parameterized Constructor

        public TwoDShape(double w, double h, string n)
        {
            width = w;
            height = h;
            name = n;
        }

        // constructor object with equal width and height
        public TwoDShape(double x, string n)
        {
            width = height = x;
            name = n;
        }

        // constructor an object from an object 
        public TwoDShape(TwoDShape ob)
        {
            width = ob.width;
            height = ob.height;
            name = ob.name;
        }

        // Properties for widht , height and name
        public double width
        {
            get
            {
                return width;
            }
            set
            {
                width = value;
            }
        }
        public double height
        {
            get
            {
                return height; ;
            }
            set
            {
                height = value;
            }
        }

        public string name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public void ShowDim()
        {
            Console.WriteLine("Width and Height are " + width + " and " + height);
        }

        // Now , area() is abstract
        public abstract double area();
    }


    // A derived class of TwoDShape for Triangles.
    class Triangle : TwoDShape
    {
        string style; // private

        // A default constructor

        public Triangle()
        {
            style = "null";
        }

        // constructor for Triangle

        public Triangle(string s, double w, double h)
            : base(w, h, "triangle")
        {
            style = s;
        }

        // Construct an isosceles triangle

        public Triangle(double x)
            : base(x, "triangle")
        {
            style = "isosceles";
        }

        //Constructor an object from an object
        public Triangle(Triangle ob)
            : base(ob)
        {
            style = ob.style;
        }

        // Override area() fro Triangle
        public override double area()
        {
            return width * height / 2;

        }

        // Display a Triangle'style
        public void ShowStyle()
        {
            Console.WriteLine("Triangle is " + style);
        }
    }

    // A Derived class of TwoDShape for Rectangles
    class Rectangle : TwoDShape
    {
        //constructor for Rectangle
        public Rectangle(double w, double h)
            : base(w, h, "rectangle")
        {
        }

        // constructor a square
        public Rectangle(double x) : base(x, "rectangle") { }

        // Constructor an object from an object
        public Rectangle(Rectangle ob) : base(ob) { }

        // Return true if the rectangle is square
        public bool isquare()
        {
            if (width == height)
                return true;
            return false;
        }

        // Override area() for Rectangle
        public override double area()
        {
            return width * height;
        }
    }

    class AbsShape
    {
        public static void Main()
        {
            TwoDShape[] shapes = new TwoDShape[4];
            shapes[0] = new Triangle("right", 8.0, 12.0);
            shapes[1] = new Rectangle(20);
            shapes[2] = new Rectangle(10, 4);
            shapes[3] = new Triangle(7.0);
            for (int i = 0; i < shapes.Length; i++)
            {
                Console.WriteLine("object is " + shapes[i].name);
                Console.WriteLine("Area is " + shapes[i].area());
                Console.ReadLine();
            }

        }

    }
    }

Output :


2 comments:

  1. This blog offers the six months industrial training with live projects...
    C#.Net Training in Noida

    ReplyDelete