Abstract class
- 'abstract' modifier before a class indicates that the class is incomplete and intended to be used only as base class and must be implemented in derived class
- it cannot be instantiated
- it may contain abstract methods
What is an Abstract Method?
Ans: A method which doesn't contain any method body to it is known as an abstract method.
-An abstract method should be declared with the "abstract" modifier.
eg: public abstract void Add();
If a method is declared as abstract in a class the implementation of the method has to be given by its child class which is mandatory for the child using the "override" modifier.
-The concept is similar to your method overriding but in case of overriding it is optional for the child class to override the method but in case of abstract methods it is mandatory for the child class to override or implement the method.
Note: If these abstract methods were defined for u under a class that class is known as an abstract which should be declared using the "abstract" modifier.
-What does an abstract class can contain in it ?
Ans: An abstract class can contain both abstract and non-abstract methods in it.
If the non-abstract methods of an abstract class wants to be consumed by a child class first it needs to give the implementaion for the abstract methods of its parent.
-An abstract class is never useful for itself, it can be consumed only it's child after implementing the abstract methods in it, the reason why it is not useful for it self is the object of an abstract class cannot be created using the "new" operator.
Abstract class can inherit class
We cannot use the keyword abstract along with sealed in c sharp. Since a sealed class cannot be abstract.
We use the abstract class when we want multiple child class to implement a method with same signature irrespective of logic.
Keypoints:
- abstract method is implicitly a virtual method
- signature of the method in base as well as derived class should be the same
why it is must for child class to provide the implementation for all abstract methods of its parent
we can never create the obj of the class which contains any abstract method in it, so if the child class does not provide the implementation the abstract methods of parent get inherited to child class and will not allow to create the object
Virtual----- override (optional) overriding
Abstract---- override (child) abstract
Example
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
public abstract class AbstractDemo // it may or maynot have abstract methods and it cannot be instantiated
{
public abstract double Area(double r); //abstract method
public void Hello() //non abstract method
{
Console.WriteLine("Hello in Base Class");
}
}
public class B : AbstractDemo
{
public override double Area(double r)
{
return r * r;
}
}
public class Test
{
public static void Main(string[] args)
public static void Main(string[] args)
{
B obj = new B();
Console.WriteLine(obj.Area(3));
obj.Hello();
Console.ReadLine();
}
} }
Output:
9
Hello in Base Class
abstract classA
{
publicvoidsum(int x, int y)
{
//Impl method
Console.WriteLine((x + y));
}
publicvoidsub(int x, int y)
{
//Impl method
Console.WriteLine((x - y));
}
publicabstract void mul(int x, int y);
//non Impl method
publicabstract void div(int x, int y);
//non Impl method
}
classB: A
{
publicoverride void mul(int x, int y)
{
//when implementing the abstract
//methods, those methods must
//be preceeded with override
//keyword
Console.WriteLine((x * y));
}
publicoverride void div(int x, int y)
{
Console.WriteLine((x / y));
}
}
classProgram
{
staticvoidMain(string[] args)
{
B obj = new B();
obj.sum(10, 20);
obj.sub(10, 20);
obj.mul(5, 2);
obj.div(5, 2);
// A obj = new A();
//an object cannot be created
//for abstract class
A obj1;
obj1 = new B();
obj1.sum(30, 40);
obj1.sub(60, 90);
obj1.mul(9, 7);
obj1.div(8, 4);
Console.ReadKey();
}
output:
Interface
In object oriented programming we can also declare abstract members under another container i.e is interface a part from abstract class
But these interface can contain only abstract members in it.
Class
Only non abstract methods
Abstract class
Abstract class + non abstract methods
Interface
Only abstract methods
As we are aware that multiple inheritance is not supported through class
But it was still supported with interface
Any interface cant be declare with any variable
Every member of interface by default public
Every member declared in a interface by default public
Every member declared in interface by default abstract
An interface can be inherited from another interface just like a class inheriting from another class
- Interface can be viewed as a 100% abstract class
- interface has no method implementation; has only method definitions without the method body
- it has only signatures of the method and no body
- all the methods and properties in an Interface are by default public and abstract
- method implementaions are provided in the class that implements that interface.
Examples:
interface Area
{
double CircleArea(double r);
double RectangleArea(double l, double b);
}
class B : Area
{
#region Area Members
public double CircleArea(double r)
{
return 3.142 * r * r;
}
public double RectangleArea(double l, double b)
{
return l * b;
}
#endregion
public static void Main(string[] args)
{
B obj = new B();
Console.WriteLine(obj.CircleArea(3));
Console.WriteLine(obj.RectangleArea(2, 5));
Console.ReadLine();
}
}
Output:
Difference Between Interface and Abstract
Difference Between Interface and Abstract
Interface | Abstract |
We cannot define variables | We can define variables |
We cannot define constructor | We can define constructor |
Can contains only abstract methods | Abstract +non abstract methods |
Can inherited from structure | Cannot inherited from structure |
# An abstract class can declare or use any variables while an interface is not allowed to do so.
So following Code will not compile :-
interface TestInterface
{
int x = 4; // Filed Declaration in Interface
void getMethod();
string getName();
}
abstract class TestAbstractClass
{
int i = 4;
int k = 3;
public abstract void getClassName();
}
It will generate a compile time error as :-
Error 1 Interfaces cannot contain fields .
So we need to omit Field Declaration in order to compile the code properly.
An abstract class can have constructor declaration while an interface can not do so.
So following code will not compile :-
interface TestInterface
{
// Constructor Declaration
public TestInterface()
{
}
void getMethod();
string getName();
}
abstract class TestAbstractClass
{
public TestAbstractClass()
{
}
int i = 4;
int k = 3;
public abstract void getClassName();
}
Above code will generate a compile time error as :-
Error 1 Interfaces cannot contain constructors
So we need to omit constructor declaration from interface in order to compile our code .
An abstract Class is allowed to have all access modifiers for all of its member declaration while in interface we can not declare any access modifier(including public) as all the members of interface are implicitly public.
Note here I am talking about the access specifiers of the member of interface and not about the interface.
Following code will explain it better :-
It is perfectly legal to give provide access specifier as Public (Remember only public is allowed)
public interface TestInterface
{
void getMethod();
string getName();
}
Above code compiles perfectly.
It is not allowed to give any access specifier to the members of the Interface.
interface TestInterface
{
public void getMethod();
public string getName();
}
Above code will generate a compile time error as :-
Error 1 The modifier 'public' is not valid for this item.
But the best way of declaring Interface will be to avoid access specifier on interface as well as members of interface.
interface Test
{
void getMethod();
string getName();
}
as we discussed objects of a abstract class cant be created. but there references can be created with the help of
their child class object
using the reference all the non abstract and abstract methods which were declared under it
to test this
abschild obj=new abschild
absparent p=obj
p.add()
p.sub()
we use abstract classes when we want multiple child classes to implement a method with same signature irrespective of logic
Difference between Abstract Method & Virtual Method
A virtual method must have its implementation in the base class and may optionally be overriden in the derived class if some additional functionality is required WHEREAS, Abstract Method has no implementation in the base class; it is implemented by the overriding method in the derived class.
In other words, Virtual method has an implementation & provides the derived class with the option of overriding it. Abstract method does not provide an implementation & forces the derived class to override the method.
A virtual method must have its implementation in the base class and may optionally be overriden in the derived class if some additional functionality is required WHEREAS, Abstract Method has no implementation in the base class; it is implemented by the overriding method in the derived class.
In other words, Virtual method has an implementation & provides the derived class with the option of overriding it. Abstract method does not provide an implementation & forces the derived class to override the method.