interface
package example;
interface abc{
publicint calculate(int a,int b);// adding two variables
publicvoid bc();
}
interface ab{
publicint calculate(int a,int b);// subtract two variables
publicvoid b();
}
publicclass kkimplements ab,abc
{
publicint calculate(int a,int b);
{
return (a+b);
}
publicvoid b()
{
}
publicvoid bc()
{
}
publicstaticvoid main(String args[])
{
System.out.println("hi...............");
}
}
class exp
{
ab=new kk();
int subtractionvalue=ab.calculate(10,20);
}
in the class kk gave implementation to calculate method as a addtion.
and in class exp subtractionvalue(integer variable) should expect subtraction of two orguments(througt interface ab specification,) but it will get addition value for two orguments
am i correct?
if correct then here main core(java) concept should wrong know !!!!!!!!!
is it correct or not?
Thx in adv
[2692 byte] By [
kissha] at [2007-11-27 10:27:59]

If you implement two interfaces with the same method signatures, you will probably have problems. The language requires that you provide that method, and the compiler ensures that it's there. But if the meanings of the two methods are incompatible, there's nothing the compiler can do about it. You the programmer are responsible for making sure this situation doesn't arise.
jverda at 2007-7-28 17:49:06 >

that program was compiled and run succesfully
kissha at 2007-7-28 17:49:06 >

> that program was compiled
Of course. That's what I'm saying. If two interfaces both define method foo(), and your class implements both interfaces, then all that means is that it must provide method foo(). Interface1 requires method foo(), and Interface2 requires method foo(). As long as your class has a single method foo(), it will satisfy both interfaces and the compiler.
> and run succesfully
Then either the meanings of the method defined in both interfaces is the same, or your test was insufficient.
jverda at 2007-7-28 17:49:06 >

> > and run succesfully
>
> Then either the meanings of the method defined
> in both interfaces is the same, or your test was
> insufficient.
If If1.foo() means "add" and If2.foo() means "subtract" and your test does foo(0,0) and gives 0, then yes, it will look like it was successful. But if you do foo(1, 2), then, even though it may run without throwing an exception, then the result must be wrong according to at least one of the interfaces' requirements.
jverda at 2007-7-28 17:49:06 >

> > and run succesfully
>
> Then either the meanings of the method defined
> in both interfaces is the same, or your test was
> insufficient.
or you got lucky :)
thanq
kissha at 2007-7-28 17:49:06 >

u cant say tht...
it is u who has committed a mistake over here...
once a method has been declared in an interface...it is up to the implementer class to giv his own implementation according to his requirement...
kk class implements calculate() method wit respect to the addition...so it will be computing the addition...tht's it...
had it given a subtraction definition..it coud hav done the subtraction...
but the thing is tht here since kk method can implement calculate() method only once either thru addition or subtraction or multiplication only once...there is no amiguity in te selection of a method during the execution..
hope u got it!!!!
> u cant say tht...
Who are you talking to?
Use real words, for ****'s sake.
> kk class implements calculate() method wit respect
> to the addition...so it will be computing the
> addition...tht's it...
> had it given a subtraction definition..it coud hav
> done the subtraction...
>
> but the thing is tht here since kk method can
> implement calculate() method only once either thru
> addition or subtraction or multiplication only
> once...there is no amiguity in te selection of a
> method during the execution..
> hope u got it!!!!
You're missing the point. It's not about ambiguity in which method to select. It's about the fact that you can't correctly implement both methods even though you may implement both interfaces.
interface Adder {
/**
* Adds two ints and returns the result
*/
int calc(int i, int j);
}
interface Subtractor {
/**
* Subtracts to ints and returns the result
*/
int calc(int i, intj);
}
class Calculator implements Adder, Subtractor {
public int calc(int i, int j) {
return i + j?
return i - j?
}
}
I can satisfy the compiler, but Calculator cannot correctly implement both Adder and Subtractor. It can't meet both of their contracts for calc().
Another example: No class can correctly implement both java.util.List and java.util.Set, because their respective equals methods' contracts are incompatible.
jverda at 2007-7-28 17:49:06 >

> once a method has been declared in an interface...it
> is up to the implementer class to giv his own
> implementation according to his requirement...
*HOW* he implements it is up to his requirement. *WHAT* the method does, however, is defined by the method's documentation in the interface.
jverda at 2007-7-28 17:49:06 >
