Sometimes it is simple and helpful to define a class that does nothing but defines a standardised interface that can be used by other classes to make sure the implementation follows a standard set of input and output paramaters. Also this approach results in faster code runs and more maintainable code.
An interface is a reference type that is somewhat similar to an abstract base class that consists of only abstract members. When a class derives from an interface, it must provide an implementation for all the members of the interface. A class can implement multiple interfaces even though it can derive from only a single direct base class.
An interface has the following properties:
An interface is like an abstract base class: any non-abstract type inheriting the interface must implement all its members.
An interface cannot be instantiated directly.
Interfaces can contain events, indexers, methods and properties.
Interfaces contain no implementation of methods.
Classes and structs can inherit from more than one interface.
An interface can itself inherit from multiple interfaces.#include<iostream>
using namespace std;
//Shape is an Interface Class. No data and everything pure virtual
class Shape {
virtual void Area(int length, int breadth) = 0;
virtual void Perimeter(int length, int breadth) = 0;
//Note, no data
};
//Derived class - Inherits Shape as Public
class Rectangle : public Shape {
public:
void Area(int length, int breadth);
void Perimeter(int length, int breadth);
private:
int someData;
};
//Derived class - Inherits Shape as Public
class Triangle : public Shape {
public:
void Area(int length, int breadth);
void Perimeter(int length, int breadth);
private:
int someData;
};
int main()
{
Rectangle r;
Triangle t;
cout<<"\n\n";
r.Area(3,4);
r.Perimeter(3,4);
t.Area(3,4);
t.Perimeter(3,4);
cout<<"\n\n";
return 0;
}
void Rectangle::Area(int length, int breadth)
{
cout<<"\nThe Area of Rectangle for length = "<<length<<" and\
breadth = "<<breadth<<" is "<<(length * breadth)<<endl;
}
void Rectangle::Perimeter(int length, int breadth)
{
cout<<"\nThe Perimeter of Rectangle for length = "<<length<<" and\
breadth = "<<breadth<<" is "<<2 * (length + breadth)<<endl;
}
void Triangle::Area(int length, int breadth)
{
cout<<"\nThe Area of Triangle for length = "<<length<<" and\
breadth = "<<breadth<<" is "<<(length * breadth)/2<<endl;
}
void Triangle::Perimeter(int length, int breadth)
{
cout<<"\nThe Perimeter of Triangle for length = "<<length<<" and\
breadth = "<<breadth<<" is "<<(length * breadth)/3<<endl;
}
In the above example, Shape class is an 'abstract class'. All the interfaces are 'pure interfaces'. It contains no data. The derived classes now have to define the interace functions.
Output:
The Area of Rectangle for length = 3 and breadth = 4 is 12.
The Perimeter of Rectangle for length = 3 and breadth = 4 is 14
The Area of Triangle for length = 3 and breadth = 4 is 6
The Perimeter of Triangle for length = 3 and breadth = 4 is 4
Difference between Abstract class and Interface.
1).Interface have only signature. whereas Abstract class have signature and definition both r allow.
2). Interface have not allow modifier access. whereas Abstract class are allowed modifier access.
3).Thurogh the Interface we can create the Multiple Inheritance whereas Abstract class are not allow the Multiple Inheritance.
4).Interface is slower compare Abstract class.
Reference: Interface Entity vs. Abstract class can refer code project's article.
source:
MSDN
Advance C++ blog
Interview Corner
Wednesday, April 21, 2010
Interface and abstract class
Labels:
abstract Class,
C#,
C++,
Interface
Subscribe to:
Post Comments (Atom)
2 comments:
A T-Shirt For Men with Titanium Blade - TITanium Arts
Custom created in T-Shirt for womens titanium wedding bands Men with Titanium Blade. ecm titanium T-Shirt for Women head titanium ti s6 with Titanium micro touch trimmer Blade - rainbow titanium TITanium Arts.
ez515 hey dude shoes,timberland scarpe,footjoy scarpe golf,ecco shoes australia ,botas danner,muckboot jälleenmyyjät,eccorusland,gaborbelgique,vivobarefootjapan ik597
Post a Comment