• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using an Interface  RSS feed

 
Aravind Reddy Sama
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
I am learning java. And I am trying to solve a question. I have a situation something like the following. I have three .java files x.java, y.java and z.java







I dont know how to implement this. Should I use an inner class and do it? how can I implement the interface such that it calls a method of other class and gives me the result? Any help with this is appreciated.

Thanks
 
Bill Johnston
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aravind Reddy Sama wrote:Hi ,
I am learning java. And I am trying to solve a question. I have a situation something like the following. I have three .java files x.java, y.java and z.java

package xyz;

interface x{
geta();
getb();
}

package xyz;
public class y{
private int a1;
private int b1;

public int geta1(){
return a1;
}

public int getb1(){
return b1;
}
}

package xyz;
public class z implements x{
geta(){
// this method should call the method geta1() from class y and return a1
}
getb(){
// // this method should call the method getb1() from class y and return b1
}
}

I dont know how to implement this. Should I use an inner class and do it? how can I implement the interface such that it calls a method of other class and gives me the result? Any help with this is appreciated.

Thanks



Well, just as an example, you could do
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Your code won’t compile because you didn’t supplhy return type information in the interface. Please always use code tags; I have added them and you can see howm miuch better yoiur code looks.
 
Aravind Reddy Sama
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Bill for posting the answer and Campbell for suggesting me. But I have one more question. Is there any way that I could do it without extending class y but writing just

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aravind Reddy Sama wrote:Thank you Bill for posting the answer and Campbell for suggesting me. But I have one more question. Is there any way that I could do it without extending class y but writing just

Well that will be confusing, since you already have an interface called x.

If you meant 'z', then one possibility is a wrapper, viz:and so on. Hopefully you get the idea.

BTW, you'd probably be much better off using real names for your classes, rather than x/y/z.
Also, class names should start with a CAPITAL letter.

Winston
 
Athira Vineeth
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Hope this will work
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Athira Vineeth wrote:Hope this will work...

I suspect not - at least, not in any meaningful way - because as defined, the getters will always return 0.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!