Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
interface ICalculator
{
    void Solve(int startPoint);
}

class BasicCalculator : ICalculator
{
    public void Solve(int startPoint);

    // unfortunately, methods like this one seem to appear by their self in any enterprise code base
    public void UseSinToSolveTheProblemIfStartPointIsZero(int startPoint);
}

void main()
{
    var calculator = new BasicCalculator();

    why tthis  bad! dependency on a specific implementation ???
    calculator.Solve(123);
}

&&&&&&&&&
C#
void main()
{
    ICalculator calculator = new BasicCalculator();

    why tthis good decoupled from a specific implementation??
    calculator.Solve(123);
}
Posted
Comments
BC @ CV 1-Feb-13 18:06pm    
It's not clear to me what you're asking, but the main purpose of an interface is to allow for polymorphism.
Interfaces[^]
Sergey Alexandrovich Kryukov 1-Feb-13 19:42pm    
Look, the whole idea of such questions is really bad. I can dig out any kind of trash. Why somebody would even look at it? The whole idea that every piece of code should have some sense is wrong. In real life, most of the code is nothing but trash. Including this sample...
—SA

1 solution

Talking about your example: If you expose your object as Interface, only methods that defined under the Interface is visible and access to the other party in that way you can hide the other methods of the class from using by the other party.

here the class BasicCalculator has two methods,i.e UseSinToSolveTheProblemIfStartPointIsZero its own method and method Solve is an interface implementation.

C#
void main()
{
    ICalculator calculator = new BasicCalculator();
 
    why tthis good decoupled from a specific implementation??
    calculator.Solve(123);
    calculator.UseSinToSolveTheProblemIfStartPointIsZero(123);//ERROR - you cannot use this method on ICalculator interface instance there by you are implementing method hiding 
}

where as
C#
void main()
{
    var calculator = new BasicCalculator(); //here var is noting but equal to BasicCalculator which is assign at runtime
 
    why tthis  bad! dependency on a specific implementation ???
    calculator.Solve(123);
    calculator.UseSinToSolveTheProblemIfStartPointIsZero(123);//one can access all the public methods that exposed by the BasicCalculator class no method hiding here.
}


read more about Interfaces @MSDN[^]
 
Share this answer
 

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