Social Icons

Monday, 1 July 2013

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

Abstract Methods and Abstract Classes in C#.Net Oops Programming


In my previous articles i explained Different types of Access Specifiers or Access Modifiers in C#.Net Oops Programming and Various Articles on WPF and Asp.Net.
         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.


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

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

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

Interview Questions:

what are the difference between Abstract Class and Sealed Class and difference between Abstract Class and Static Class.


That's all about Abstract Classes and Abstract Methods.Hope it will help to you.

Related Tutorials

No comments:

Post a Comment

Please Give your Valuable Suggestions,Questions and Comments to improve my blog.