Vous êtes sur la page 1sur 3

using System;

abstract class mcn


{
public int add(int a, int b)
{
return (a + b);
}
abstract public int mul(int a, int b);
}
class mcn1 : mcn
{
public override int mul(int a, int b)
{
return a * b;
}
}
class test
{
static void Main(string[] args)
{
mcn1 ob = new mcn1();
int result = ob.add(5, 10);
int r = ob.mul(5, 10);
mcn a = new mcn1();
Console.WriteLine("the result is {0}, {1},{2},{3}", result,
r,a.add(9,9),a.mul(9,9));
Console.Read();
}
}

using System;
abstract class Shapes
{
abstract public int Area();
}
class Square : Shapes
{
int side = 0;
public Square(int n)
{
side = n;
}
public override int Area()
{
return side * side;
}
}

class Rectangle : Shapes


{
int length = 0, width = 0;
public Rectangle(int a, int b)
{
length = a;
width = b;
}
public override int Area()
{
return length * width;
}
}
class test
{
static void Main()
{
Square a = new Square(3);
Console.WriteLine("Area of Square:"+a.Area());
Console.Read();
}
}

The purpose of abstract class is to provide default functionality to its subclasses.

When a method is declared as abstract in the base class then every derived class of that class must
provide its own definition for that method.

An abstract class can also contain methods with complete implementation, besides abstract methods.

When a class contains at least one abstract method, then the class must be declared as abstract class.

It is mandatory to override abstract method in the derived class.

When a class is declared as abstract class, then it is not possible to create an instance for that class. But it
can be used as a parameter in a method.

Characteristics of an abstract class:

1. A class may inherit only from only one abstract class

2. Cannot be instantiated directly

3. An abstract class may contain abstract methods and accessors.

4. Methods marked as abstract cannot contain any code.

5. A non-abstract class derived from an abstract class must include actual implementations of all
inherited abstract methods and accessors otherwise the Compiler Error CS0534 will occur.

6. An abstract method is implicitly a virtual method.

Abstract Method
In an abstract class a method which has a keyword "abstract" and doesn't provide any
implementation is called abstract method.The implementation logic of abstract methods is provided
by the child classes or derived classes.Child classes use keyword "override" with same method name
(as abstract method name) to provide further implementation of abstract methods.

Non Abstract Method

In an abstract class a method which doesn't have a keyword "abstract" and provide any
implementation is called non abstract method.

Difference between Abstract and Virtual methods

abstract function doesn't contain any body but a virtual function contain body 2. we must be implement
the abstract function in derived class but it is not necessary for virtual function 3. abstract function can
only use in abstract class but it is not necessary for virtual function 4. abstract function are called pure
virtual function

Vous aimerez peut-être aussi