People Keep on Speaking About interface now let us sees what interface is all about.
I have a class called sportscar
public class sportscar
{
public void ProduceHighSpeed()
{//somecode }
public void MaintainGoodcc()
{//somecode }
}
Now I have one more class called luxurycar
public class LuxuryCar
{
public void ProvideComfort()
{ //somecode }
public void FitAircooler()
{ //somecode }
}
Now I want a have a car which should be of both sports and luxury
How can I achieve in c#
If I need to achieve this I should create a class which should have methods of sportscar and luxury car.
So I should inherit the both the classes sportscar and luxury car for making my newcar
This the concept of MULTIPLE INHERITANCE is not possible in c# ,So now what is my alternative way to make my newcar.
INTERFACE comes to my rescue to create mynewcar.let us see how this does.
Interface sportscar
{
void ProduceHighSpeed();
void MaintainGoodcc();
}
Interface Luxurycar
{
void ProvideComfort();
void FitAircooler();
}
Now if we compare the classes sportscar,Luxurycar and Interfaces sportscar,Luxurycar we don’t have any implementations for the methods in interface,but only the function declartion.
Interfaces is a contract where we declare only the functions and don’t have any implementation.
Now let us see how do I create a mynewcar
public class myNewCar:sportscar,Luxurycar
{
public void ProduceHighSpeed()
{//somecode }
public void MaintainGoodcc()
{//somecode }
public void ProvideComfort()
{ //somecode }
public void FitAircooler()
{ //somecode }
Public static void Main()
{
myNewCar TataKanth=new myNewCar();
TataKanth. ProduceHighSpeed();
TataKanth. MaintainGoodcc ();
TataKanth. ProvideComfort ();
TataKanth. FitAircooler ();
}
}
Points To Rememeber:
1. When we inherit the interface its mandatory to implement the methods in the interface
good article
ReplyDelete