I want to use RMI in my application. So using the interface is compulsory. I want to force the developer to implement getter and setters. So I want to define the getter and setter methods in my interface. How can I do this? Unfortunately there is no useful article or link to help me. Any helps would be appreciated.
public interface MyInterface {
PropertyType getProperty();
void setProperty(PropertyType property);
}
you can declare
method in interface, you can not define
it's body inside it.
Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.
It will be defined where that interface
will be implemented.
Java doesn't have inbuilt support for get-set properties.You have define your own get-set properties like
int getCount();
void setValue();
write your methods without body
public interface MyInteface {
public String getSomething();
public void setSomething(String str);
}
then any class that implements this interface has to implements these get/set methods
If you declare the getter and setter in the interface any reasonable IDE will tell you that you have to implement them in a class that implements the interface. Even if the IDE does not tell you that, java will print out an error that is reasonable enough and easy to understand. Little room for error there.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.