Many of us think we have inheritance topic that will solve any problem and we are implement anything in inheritance concept except one i.e Multiple inheritance.Yes,Multiple inheritance not applicable in c#.net that's why we are go for knowing interface.for implementing multiple inheritance we are using the interface in object oriented programming languages.In the interface we are only use abstract methods and properties.For getting More on interface Concept please read the following paragraph and example.
Tutorial of Interface in C#.net
Interface |
- To define a interface we have to use interface keyword.
- Interface looks like a class but it has no implementation.
- An interface is a collection of abstract members,by default interface members are public and abstract.
Interface specifing example in C#.Net
Syntax:
interface <Interface name>
{//abstract members
}
Note:Interface name should be start with I
ex:Iemployee,Istudent,Iwork.....
- Interface members we should implement within derived class without using override keyword.
- An instance cannot contain fields.
- Interface cannot contain constructors
- we cannot implement a property (or)method in interface.
- Interface cannot contain static members.
- Using interface we can implement multiple inheritance.
- we cannot create an object for interface but we can create reference variable for interface.
Example to implement Single inheritance by using interface.
interface Imyinterface
{
void print();
int x
{
get;
set;
}
}
class ram:Imyinterface
{
//implementing interface members
public void print()
{
console.writeline("print is calling");
}
int x;
public int x
{
get
{
return x;
}
set
{
x=value;
}
}
}
claa Program
{
void main()
{
Imyinterface obj=new ram();
obj.print();
obj.x=100;
console.writeline("x value is:"+obj.x);
console.ReadLine();
}
Output:
print is calling
x value is:100
Example for multiple inheritance by using interfaces
namespace interface2
{
interface Inokia1
{
void calling();
void Receiving();
void sendmsg();
void Endcall();
}
class nokia1100:Inokia1
{
public void calling
{
console.writeLine9"nokia1oo is calling");
}
public void receiving
{
console.writeline("nokia1100 is receiving call");
}
public void sendmsg
{
console.writeline("nokia 1100 is sending msg");
}
public void Endcall
{
console.writeline("nokia 1100 is ending call");
}
}
interface Inokia2
{
void WiFi()
{
void vediocalling();
}
class nokiaAsha:Inokia1,Inokia2
{
public void calling
{
console.writeline("nokia asha is calling");
}
public void receiving
{
console.writeline("nokiaAsha is receiving call");
}
public void sendmsg
{
console.writeline("nokia asha is sending msg");
}
public void endcall
{
console.writeline("nokia sha is ending call");
}
public void WiFi
{
console.writeline(nokia asha wifi is on");
}
public vedio calling
{
console.wroteline('nokiaasha vediocall is supporting");
}
}
class program
{
void main()
{
Inokia1 obj1=new Nokia1100();
obj1.calling();
obj1.receiving(0;
obj1.sendmsg();
obj1.endcall(0;
obj1=new NokiaAsha();
obj1.calling();
obj1.receiving();
obj1.sendmsg();
obj1.endcall();
Inokia2 obj2=(INOKIA2)OBJ1;
obj2.WiFi();
obj2.vediocalling();
console.ReadLine();
}
can we implement multiple inheritance with a combination of one class and multiple interface.
Yes,but first we have to write class name then we have to write interfaces names like below
interface I1
{
}
interface I2
{
}
class ram
{
}
class ram:bc,I1,I2
{
}
Q)Whan we will go for interface?
whenever we wnt to implement all the behaviours in future derived classes.we will go fo interface.As well as to implement multiple inheritance we will go for interface.
Realtime example for interface
namespace Bankex
{
interface Iaccount
{
void openAccount(double IntAmt);
void withdraw(double amt);
}
class currentAccount:Iaccount
{
int acNo;
String acName;
double bal;
internal current account(int acno,string acname)
{
this.cno=acno;
this.acname=acname;
}
//implementing interface methods
public voidnopenAccount(double IntAmt)
{
this.bal=IntAmt;
Console.writeLine('your account is created successfully");
Console.writeLine('your account details are'");
Console.writeLine('your account no is;"+acno);
Console.writeLine('your account name is:"+acname);
Console.writeLine('your account current balance is:"+this.bal);
}
public void withDraw(double amt)
{
if(this.bal>=amt)
{
this.bal=this.bal-amt;
Console.writeLine('your withdraw is sucessful");
Console.writeLine('your current balance is:"+this.bal);
}
else
{
Console.writeLine("insufficient funds");
}
}
}
class savingAccount:Iaccount
{
int acno;
String acName;
double bal;
internal SavingAccount(int acno,string acname)
{
}
public void openaccount(double InAmt)
{
if(InAmt>=1000)
{
this.bal=InAmt;
Console.writeLine('your account is created");
Console.writeLine('your account current balance is:"+this.bal);
}
else
{
Console.writeLine('your account cannot be created,min bal should be 1000");
}
}
public void withdraw(double amt)
{
if(this.bal-amt>=1000)
{
this.bal=this.bal-amt;
Console.writeLine("withdraw is successful");
Console.writeLine('your balance is:"+this.bal)
}
else
{
Console.writeLine("Insufficient funds");
}
}
}
class program
{
void main()
{
{
iaccount cust1=new currentAccount(111,"Rama");
cust1.openaccount(2000);
cust2.WithDraw(2000);
cust1=new savingaccount(222,"Ravi");
cust1.openAccount(4000);
cust1.withdraw(2000);
Console.ReadLine();
}
}
}
Recommended Articles
1.About Master Pages
2.Passing Parameter Concept
3.Methods Concept
iaccount cust1=new currentAccount(111,"Rama");
cust1.openaccount(2000);
cust2.WithDraw(2000);
cust1=new savingaccount(222,"Ravi");
cust1.openAccount(4000);
cust1.withdraw(2000);
Console.ReadLine();
}
}
}
Recommended Articles
1.About Master Pages
2.Passing Parameter Concept
3.Methods Concept
0 comments:
Post a Comment