Link to home
Start Free TrialLog in
Avatar of Unimatrix_001
Unimatrix_001Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Implementing interface & redefining a method.

Morning,

public interface MyInterface{
	int Calculate();
}
public class MyClass:MyInterface{
	int Calculate(){return 0;}
	int Calculate(){return 1;}
}

Open in new window


Obviously the above code is very wrong as Calculate is defined twice within MyClass, but is there a way I can state that one of the Calculate methods only be accessible through MyInterface and the other Calculate method available through MyClass. So:

void Test(){
	
	MyClass a=new MyClass();
	a.Calculate();	//Returns 0

	MyInterface b=(MyInterface)a;
	b.Calculate();	//Returns 1
}

Open in new window


Any tricks to make this possible without having two different named methods?

Thank you,
Uni
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Unimatrix_001

ASKER

Oh of course!  :)And using the new (well, perhaps not so new now) default arguments...

public interface MyInterface{
	int Calculate(bool aIgnore=true);
}

public class MyClass:MyInterface{

	int Calculate(){
		return 0;
	}

	int Calculate(bool aIgnore=true){
		return 1;
	}

}

void Test(){
	
	MyClass a=new MyClass();
	a.Calculate();	//Returns 0

	MyInterface b=(MyInterface)a;
	b.Calculate();	//Returns 1
}

Open in new window


That should suffice, yes?

Thanks,
Uni
Excellent, thank you very much. :)