• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Limitation with interface for plugability

 
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First I use class extension to achieve plugability:


abstract class Base {
String desc;
void m();
}
class X extends Base {
void m() { // imp }
}
class Y extends Base {
void m() {// imp}
}
// In another class I have:
void amethod(Base obj) { //how cares it is X or Y?
obj.desc = “sometext”;// I need to access instance variable of obj!!
obj.m();
}
/*
Note in method I deal with Base class only and I don’t care if X or Y is passed in.
Now let’s use interface
*/
Interface IBase {
void m();
}
class X implements IBase {
void m() {// imp }
}
class Y implements IBase {
void m() {// imp }
}
//In another class I have:
void amethod(IBase obj) { //how cares it is X or Y?
obj.desc = “sometext”;// this does not work!!
obj.m();
}


You see with interface you have trouble with instance variables!
What’s the problem? How can I use interface correctly?
Thanks.
[ October 14, 2002: Message edited by: Bruce Jin ]
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Define getter and setter in your interface.
Use any instance attribute in your class.
getter and setter are public.
attribute should be private.
interface IBase {
void m();
void setDesc(String desc);
String getDesc();
}
class X implements IBase {
private String desc;
public void setDesc(String desc){
this.desc = desc;
}
public String getDesc() {
return desc;
}
public void m() {
System.out.println("X. " + getDesc());
}
}
class Y implements IBase {
private String description;
public void setDesc(String desc){
description = "Description: " + desc;
}
public String getDesc() {
return description;
}
public void m() {
System.out.println("Y. "+getDesc());
}
}
class Test1 {
static void amethod(IBase obj) {
obj.setDesc("sometext");
obj.m();
}
public static void main(String[] args){
X x = new X();
Y y = new Y();
amethod(x);
amethod(y);
}
}
 
Bruce Jin
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Muriel.
This will work.
If I have a few douzen instance viriables (for example my X class could be a Swing GUI class), then I have to define all setters in the interface.
Use abstract class seems alot simpler. I put all instance viriables in the base class and all sub class inherent them.
But I am not sure which approach is better.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See https://coderanch.com/t/97982/patterns/set-functions-evil
 
Won't you be my neighbor? - Fred Rogers. tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic