Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

How to create abstract class in c#?

user-image
Question ajoutée par Syed Sumair , IT Project Manager , Saudi National Bank
Date de publication: 2016/03/14
Shubham Karaulia
par Shubham Karaulia , Customer Service Associate , GE Capital

We can create an abstract class by putting the keyword abstract before a class definition as follows:

namespace AbstractClassDemo   

{   

    abstract class iPhone { } //Definition of an Abstract Class   

    class Program   

    {   

        static void Main(string[] args) { }   

    }   

 

}  

Mosam Vadiekar
par Mosam Vadiekar , Senior dot net developer , Remote Solutions System Limited

In this example, we will create a base class for four legged animals and then create a Dog class, which inherits from it, like this:

namespace AbstractClasses { class Program { staticvoid Main(string[] args) { Dog dog =new Dog(); Console.WriteLine(dog.Describe()); Console.ReadKey(); } } abstractclass FourLeggedAnimal { publicvirtualstring Describe() { return"Not much is known about this four legged animal!"; } } class Dog : FourLeggedAnimal{publicoverridestring Describe() { return"This four legged animal is a Dog!"; } } }

And this is how abstraction is carried out in c# 

Utilisateur supprimé
par Utilisateur supprimé

By Using the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes.

 

abstract class ShapesClass{    abstract public int Area();}

class Square : ShapesClass{    int side = 0;    public Square(int n)    {        side = n;    }    public override int Area()    {        return side * side;    }}

salman hussain
par salman hussain , WBMS

 

abstract class ShapesClass { abstract public int Area(); } class Square : ShapesClass { int side = 0; public Square(int n) { side = n; } // Area method is required to avoid // a compile-time error. public override int Area() { return side * side; } static void Main() { Square sq = new Square(12); Console.WriteLine("Area of the square = {0}", sq.Area()); } interface I { void M(); } abstract class C : I { public abstract void M(); } } // Output: Area of the square = 144

Abstract classes, marked by the keyword abstract in the class definition, are typically used to define a base class in the hierarchy. ... Instead, you have to subclass them, as taught in the chapter on inheritance, and create an instance of your subclass.

Ehsan Al-shraideh
par Ehsan Al-shraideh , Web Developer / FREELANCER , ARABIA CONSULTING

abstract class Letter{abstract public int A();}

 

Praveen Kumar
par Praveen Kumar , Sr. Software Engineer , FastLink Group LLC

If you want just to create abstract class check below code

Public abstract class MyAbstractCL

{

//if you creating any class with abstranct keyword then you have to add at least one abstract method in this and abstract method does not create body it just the declaration not method body.

public abstract void sum(int i,int y);

 

//you can add furhter function or method as well whichare not abstract 

}

 

لطفي عادل صالح ????
par لطفي عادل صالح ???? , SENIOR ASP.Net MVC DEVELOPER , Abraj

the abstract class created by Writing the keyword abstract before class definition as:// this is the Original class public class C { public virtual void work(int n) { //the Original implementation. } } // this is the abstract class public abstract class D : C { public abstract override void work(int n); } public class F : D { public override void work(int n) { //the New implementation. } }

yassine staili
par yassine staili , designer , NOunPRINT

you can use the keyWord -abstract - or if you work with framework 5 you can add it directly in the solution ,the abstract class it means that it's the base of classes of a  application's hierarchy

LEENA K
par LEENA K , Software Engineer , Assuretech Business Solutions (I) Pvt. Ltd

An abstract class cannot be instantiated. .e can create abstract class using the keyword 'abstarct'.We cannot able to create the object of abstract classusing new keyword.A object of a class derived from an abstract class can be created using new keyword.

 

eg:

 

namespace class

{

   public abstract class classA

     { 

           public int A();

           public void B

            {

             }

           abstract public void C();

 

     }

    public class classB:classsA

    {

        public override C()

{

}

     }

public class Program

{

     private static void Main(string[] args)

{

      ClassB obj=new ClassB();

}

}

}

Public MustInherit Class WashingMachine Sub New() ' Code to instantiate the class goes here. End sub Public MustOverride Sub Wash Public MustOverride Sub Rinse (loadSize as Integer) Public MustOverride Function Spin (speed as Integer) as Long End Class // C# abstract class WashingMachine { public WashingMachine() { // Code to initialize the class goes here. } abstract public void Wash(); abstract public void Rinse(int loadSize); abstract public long Spin(int speed); }

More Questions Like This