Thursday, 12 February 2015

Abstract Classes and Abstract Methods in C#.Net Explanation with Example(Oops Programming)

Now i will explain difference between Abstract Methods and Abstract Classes in C#.Net,What is the need of Abstract Classes, In what way Abstract Methods help us in our programming, what are the similarities and dissimilarities between method overriding and abstract methods.

Description:

Abstract Methods:

   A method without any body is known as abstract method. An Abstract method contains only declaration or definition only but not any implementation. If you want to declare a method as abstract method then use abstract modifier.

Synatax for declaring abstract method:
       <access-modifier> abstract <return> Methodname(parameter) 

Method Overriding Vs Abstract methods

The Concept of method overriding and abstract methods are similar to each other. In method overriding if the parent class contains any virtual methods can be reimplemented under the child class using override keyword but it is optional, where as in abstract class it is mandatory to implement abstract methods in child classes.

Abstract Class:

      A class Under which we declare abstract members is an abstract class which also should be declare using abstract modifier. We used abstract classes to provide default functionality to its sub classes. When a class contains atleast one abstract method, then the class must be declared using Abstract Keyword.
          Abstract class can contain both abstract and non-abstract members init. If any child class want to consume non-abstract methods of its parent, first it needs to implement all the abstract methods in it's parent class otherwise consuming non - abstract methods of parent will not be possible.

We can not create object of a class which contain abstract members so it must be inherited to consume its members. An Abstract class can be used only as parent class.

An Example for Using Abstract classes and Abstract Methods:

 Add a class to your console Application project. Name it as Absparent.cs and write the following

  1. namespace OOPSProject  
  2. {  
  3.     abstract class AbsParent  
  4.     {  
  5.         public void Add(int x, int y)  
  6.         {  
  7.             Console.WriteLine(x + y);   
  8.         }  
  9.         public void Sub(int x, int y)  
  10.         {  
  11.             Console.WriteLine(x - y);  
  12.         }  
  13.         public abstract void Mul(int x, int y);  
  14.         public abstract void Div(int x, int y);  
  15.     }  
  16. }  

Now to consume parent's class abstract members we should create a child class inheriting this base class.
add a class to project and name it as Abschild.cs and write following.
The parent class non - abstract members i.e Add() and Sub() and abstract members i.e. Mul() and Div(). To consume non abstract members first we have to create implementation for abstract members and we can consume them. So in the child class write following
  1. namespace OOPSProject  
  2. {  
  3.     class AbsChild : AbsParent   
  4.     {  
  5.         public override void Mul(int x, int y)  
  6.         {  
  7.             Console.WriteLine(x * y);   
  8.   
  9.         }  
  10.         public override void Div(int x, int y)  
  11.         {  
  12.             Console.WriteLine(x / y);  
  13.         }  
  14.         static void Main()  
  15.         {  
  16.             AbsChild c = new AbsChild();  
  17.             AbsParent p = c;  
  18.             p.Add(100, 50); p.Sub(67, 22);  
  19.             p.Mul(10, 23); p.Div(98, 10);   
  20.             Console.ReadLine();   
  21.         }  
  22.     }  
  23. }  

So in child class first we provided implementation for the abstract methods in parent class and then we consumed that methods.
So if we run the Child Class we get output
150
45
230
9
- See more at: http://aspdotnet-sekhar.blogspot.in/2013/07/Abstract-Classes-and-Abstract-methods-In-CSharp.Net-OopsProgramming-Example.html#sthash.gnlolQCr.dpuf

No comments:

Post a Comment