abstract class in C#

  • Abstract classes are one of the essential behaviors provided by .NET. 
  • Commonly, you would like to make classes that only represent base classes, and don't want anyone to create objects of these class types. You can make use of abstract classes to implement such functionality in C# using the modifier 'abstract'.
  • A method without body is called abstract method.
  • Syntax:
    • public abstract void <method_name> ();
  • An abstract class means that, no object of this class can be instantiated, but can make derivations of this.
  • An abstract class is a class that must be inherited and have the methods overridden. When a class contains at least one abstract method then that class must be declared as an abstract class.
  • An abstract class can contain either abstract methods or non abstract methods. Abstract members do not have any implementation in the abstract class, but the same has to be provided in its derived class.
  • When a class contains methods with body & without body then it is said to be partial abstract class
  • When a class contains only abstract methods then it is called as full abstract class or interface class
  • The abstract methods must be override.
  • Abstract classes are not instantiated, but a reference can be created.
  • For e.g.,
  • using System;
    //wirtten by Suneel Gudipati
    namespace abstractDemo
    {
        class Program
        {
            public abstract class shape
            {
                private int x = 10, y = 20;
                public void print()
                {
                    Console.WriteLine("From Base Class");
                }//print
                public abstract void FindArea(); //abstract method which doesn't have body
            }//shape
            public class circle : shape
            {
                private double r = 10;
                public override void FindArea()
                {
                    double area = 3.14 * r * r;
                    Console.WriteLine(area.ToString());
                }//FindArea
                public void Disp()
                {
                    Console.WriteLine("From Dervide class");
                }
            }
            static void Main(string[] args)
            {
                circle c = new circle();
                c.print();
                c.FindArea(); ;
                c.Disp();
                shape s; //s is reference, we can not create instance
                s = c;
                s.print();
                s.FindArea();
                Console.ReadLine();
            }
        }
    }
    

0 comments :

Post a Comment

> Related Posts with Thumbnails
 

Copyright © 2012. GS dot net - All Rights Reserved - Design by BTDesigner - Proudly powered by Blogger