• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 280
  • Last Modified:

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
0
Unimatrix_001
Asked:
Unimatrix_001
  • 2
1 Solution
 
AndyAinscowFreelance programmer / ConsultantCommented:
You could force one to require a parameter so they would still have the same name but be different.

Having said that what you are wanting will lead to really difficult to read (and debug) code
0
 
Unimatrix_001Author Commented:
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
0
 
Unimatrix_001Author Commented:
Excellent, thank you very much. :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now