Click here to Skip to main content
15,907,183 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
tell me how interface can b called from main...... is it possible to call interface like calling classes by using objects.... give me simple exampless which are easy to understand???
Posted

1 solution

An interface is a view to a class, sort of.

You tell the compiler that a class implements an interface. Compiler will then monitor that the class implements all methods that belong into that interface. If one method is missing or with wrong signature, compiler will not accept it.

So you're sure you can use your class as if it was an instance of the interface it implements (even though interfaces cannot be instantiated, classes that implement it can).
C#
interface IExampleInterface
{
    public string TellMeSomething();
}


public class ExampleClass : IExampleInterface
{
    public string TellMeSomething()
    {
        return("That's Something!");
    }
}


int Main(whatever)
{
    IExampleInterface exampleInstance = new ExampleClass();
    string result = exampleInstance.TellMeSomething();
}
 
Share this answer
 
Comments
Innocent910 30-Aug-13 2:31am    
if a base class implements interface and further derived classes derive base class.... is this inheritance or interface implementation?????
lukeer 30-Aug-13 2:49am    
That would be both. The child classes inherit (from base) and the base class implements (the interface).
Innocent910 30-Aug-13 3:03am    
is this posible for a base class object to call the method of derive class like this....
BC obj = new DC();
obj.show();
Console.WriteLine(obj.Area());
lukeer 30-Aug-13 3:11am    
There's some information lacking in your question. But I'll try:

You declared obj as BC. So you can call all of BC's methods on obj.
Nothing DC-specific is known to BC.

But there are ways around: you can cast ((DC)obj).DerivedClassMethod();
Or, what's better mostly, you can use overrides. If DC overrides a method of BC, the child override will be used even if you declared as base (and used the constructor of child).
Innocent910 30-Aug-13 3:17am    
thnx man

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900