Advertisement
interface A
{
// Unlike regular methods, method of an interface
// should not be implemented within a pair of curly braces {} and,
// should rather end with a semicolon.
void GetName();
}
//C# Example of an interface
//An interface
interface Math
{
//The method Subtract() method is public and abstract by default
void Subtract(int a, int b);
//The method Add() is public and abstract by default
void Add(int a, int b);
}
class implementer-class-name : interface-name
{
//The body of a class, which is
//implementing an interface
}
//C# Example of implementing an interface
using System;
//An interface
interface Math
{
//The method Subtract() method is public and abstract by default
void Subtract(int a, int b);
//The method Add() is public and abstract by default
void Add(int a, int b);
}
//class A implementing Math interface
class A:Math
{
//Implementing the method Subtract() of interface Math
public void Subtract(int a, int b)
{
Console.WriteLine("{0}-{1}: {2}",a, b, a-b);
}
//Implementing the method Add() of interface Math
public void Add(int a, int b)
{
Console.WriteLine("{0}+{1}: {2}",a, b, a+b);
}
public static void Main(String[] ar)
{
//Creating an object of class A
//Which has implemented interface Math
A ob = new A();
//Calling the implemented method Subtract() of class A
ob.Subtract(10, 5);
//Calling the implemented method Add() of class A
ob.Add(10, 5);
}
}
10-5: 5
10+5: 15
//C# An interface cannot contain the instance variables
using System;
//An interface
interface A
{
int a = 10;
}
A.cs(11,6): error CS0525: Interfaces cannot contain instance fields
Advertisement
//C# You cannot instantiate or create an object of an interface
using System;
//An interface
interface A
{
void MethodA();
}
class B
{
public static void Main(String[] ar)
{
//instantiating an interface, throws a compile error.
A ob = new A();
}
}
A.cs(17,9): error CS0144: Cannot create an instance of the abstract class or interface 'A'
//C# An Interface can extend multiple interfaces and,
//it doesn't have to provide an implementation of methods of those interfaces.
using System;
//First interface
interface A
{
void MethodA();
}
//Second interface
interface B
{
void MethodB();
}
//Third interface C, which is inheriting interface A and B
//without providing implementation of their methods.
interface C:A,B
{
}
//A concrete class, D is implementing interface C
class D:C
{
//Implementing method MethodA()
//Which interface C inherited from interface A
public void MethodA()
{
Console.WriteLine("A");
}
//Implementing method MethodAB()
//Which interface B inherited from interface A
public void MethodB()
{
Console.WriteLine("B");
}
public static void Main(String[] ar)
{
//Creating an object of concrete class, D
//Which has implemented interface C
D ob = new D();
//Calling the implemented method, MethodA()
//by using the object of class D
ob.MethodA();
//Calling the implemented method, MethodA()
//by using the object of class D
ob.MethodB();
}
}
A
B
//C# A class can implement multiple interfaces
//and it must provide an implementation of all their methods.
using System;
//First interface
interface A
{
void MethodA();
}
//Second interface
interface B
{
void MethodB();
}
//Third interface C, which is inheriting interface A and B
//without providing implementation of their methods.
//interface C:A,B
//{
//}
//A concrete class, D is implementing interfaces A and B
class D:A,B
{
//Implementing method MethodA()
//Which is inherited from interface A
public void MethodA()
{
Console.WriteLine("A");
}
//Implementing method MethodAB()
//Which is inherited from interface A
public void MethodB()
{
Console.WriteLine("B");
}
public static void Main(String[] ar)
{
//Creating an object of concrete class, D
//Which has implemented interface C
D ob = new D();
//Calling the implemented method, MethodA()
//by using the object of class D
ob.MethodA();
//Calling the implemented method, MethodA()
//by using the object of class D
ob.MethodB();
}
}
A
B
//C# An abstract class can implement an interface and it doesn't have to implement its methods.
using System;
//An interface
interface Math
{
//Signature of Add() method
int Add(int a, int b);
}
//An abstract class implementing an interface
//without having to implement its methods
abstract class A:Math
{
//Implementing a method Add() of interface Math
public int Add(int a, int b)
{
return(a+b);
}
}
//A concrete class inheriting abstract class, A
class B:A
{
public static void Main(String[] ar)
{
//Creating an object of class B
B ob = new B();
//Calling the inherited method of abstract class, A
//By using the object of class B
Console.WriteLine(ob.Add(10,20));
}
}
A
B
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement