Abstract Class & Methods
Abstract Class & Methods
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;
}
}
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.
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.
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.
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.
In an abstract class a method which doesn't have a keyword "abstract" and provide any
implementation is called non abstract method.
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