interface

Hi,

interface a{

void a();

void b();

}

interface b{

void a();

void h();

}

What happend the following code should compile?

class abc implements a,b

{

////////

////////

}

Thanx in adv

[278 byte] By [kissha] at [2007-11-27 10:21:51]
# 1

> interface a{

> void a();// bad practise

> void b();

Implement the methods defined in the interface. a, b, h

haishaia at 2007-7-28 17:12:28 > top of Java-index,Java Essentials,Java Programming...
# 2

What?

What code do you actually have in your class?

class abc implements a,b

{

////////

////////

}

You will have to implement the methods defined in your interfaces or decare your class abstract. What error messages do you get?

Message was edited by:

Richcoleuk

Richcoleuka at 2007-7-28 17:12:28 > top of Java-index,Java Essentials,Java Programming...
# 3

Interfaces do not have any implementation code. so what you have done wont cause any conflict.

Message was edited by:

Jahvah

Jahvaha at 2007-7-28 17:12:28 > top of Java-index,Java Essentials,Java Programming...
# 4

No, it shouldn't compile. Implementing an interface is you promising the compiler - and client code - that your class will provide an implementation for all those (abstract) methods on that interface. By not doing so, you've broken your promise, and the compiler will pout and sulk until you keep it

georgemca at 2007-7-28 17:12:28 > top of Java-index,Java Essentials,Java Programming...
# 5

I think that what the kiddo is trying to know is what happens if two distinct interfaces declare a method with the same name ant a class implement those interfaces:

interface One{

void a();

}

interface Two{

void a();

}

class Imp implements One, Two{

void a(){

System.out.println("this refers to interface One or Two?");

}

}

is this your doubt?

manuel.leiriaa at 2007-7-28 17:12:28 > top of Java-index,Java Essentials,Java Programming...
# 6

the OP needs to pipe in and clarify his or her question.

petes1234a at 2007-7-28 17:12:28 > top of Java-index,Java Essentials,Java Programming...
# 7

yes extatly correct i was compliled the code and run so it was executed

my dought is

interface one shold aim of a() method is addition operation for example

interface twoshold aim of a() method is divsion operation for example

so class Imp can give one implementation either addition or division

one obj=new Imp();

obj.a(); (addition shold required)

but implemented for division in Imp class so aim shold fail!!!!!!!!!!!!!

kissha at 2007-7-28 17:12:28 > top of Java-index,Java Essentials,Java Programming...
# 8

> yes extatly correct i was compliled the code and run

> so it was executed

> my dought is

> interface one shold aim of a() method is addition

> operation for example

> interface twoshold aim of a() method is divsion

> operation for example

>

> so class Imp can give one implementation either

> addition or division

> one obj=new Imp();

> obj.a(); (addition shold required)

> but implemented for division in Imp class so aim

> shold fail!!!!!!!!!!!!!

of course! You should rename your methods (give them meaningful names) in the interfaces, like

void add();

void subtract();

and so on

manuel.leiriaa at 2007-7-28 17:12:28 > top of Java-index,Java Essentials,Java Programming...
# 9

public class kk implements ab,abc

{

public int calculate(int a,int b);

{

return (a+b);

}

public void b()

{

}

public void bc()

{

}

public static void main(String args[])

{

System.out.println("hi...............");

}

}

interface abc{

public int calculate(int a,int b);// adding two variables

public void bc();

}

interface ab{

public int calculate(int a,int b);// subtract two variables

public void b();

}

int the aboue code class kk gave implementation to calculate method as a addtion

so for example

class exp

{

ab=new kk();

int subtractionvalue=ab.calculate(10,20);

/* here aboue step should expect subtraction value(througt interface ab specification, but he will get addition value so here main core concept should wrong know !!!!!!!!!*/

}

kissha at 2007-7-28 17:12:28 > top of Java-index,Java Essentials,Java Programming...
# 10

When u post u should use the "code" button this is how u do it:

select all the code (and only the code), like this one:

public class test{

public void add(){

//return addiction result

}

public void sub(){

//return subtraction result

}

}

and then click in the "code" button he will make a initial tag in where the code begins, and an ending tag in where the code ends

[code]public class test{

public void add(){

//return addiction result

}

public void sub(){

//return subtraction result

}

}

[/code]

HighMagea at 2007-7-28 17:12:28 > top of Java-index,Java Essentials,Java Programming...
# 11

Wat i think u want is this:

u have the Interface:

public interface Operation {

public int result();

public void sum();

public void sub();

public void mul();

public void div();

}

And in the class tht implements u HAVE to use the methods from the Interface

public class Calculation implements Operation{

private int num1 = 0;

private int num2 = 0;

private int result = 0;

public Calculation(int num1, int num2){

this.num1 = num1;

this.num2 = num2;

}

public int result(){

//Obligated by the implementation

}

public void sum(){

//Obligated by the implementation

}

public void sub(){

//Obligated by the implementation

}

public void mul(){

//Obligated by the implementation

}

public void div(){

//Obligated by the implementation

}

}

HighMagea at 2007-7-28 17:12:28 > top of Java-index,Java Essentials,Java Programming...