java - How do I define constants that must be overridden inside an interface? -


to create kits players can choose, have made interface:

public interface kit {} 

and have implemented each kit:

public class ninja implements kit {} 

now, want set constants related class, not instance. want these static across implementations of interface, , want each implementation override them.

try #1:

public interface kit {     string display_name;     // blank final field display_name may not have been initialized } 

try #2:

public interface kit {       static string getdisplayname();     // illegal modifier interface method getdisplayname; public & abstract permitted } 

an interface can not hold data way class can hold field. if not want kit instantiated, want abstract class. see them interface can have implementation , fields.

note, please read further clarfication: read more

so want in have abstract class in background, not interface. how look?

public abstract class kit {     protected final string name = "foo";      public string getname () {         return name;     } } 

here have our kit, every class implementing kit have access name field. might recommend putting in caps if supposed constant. might best static property well. more of can read here.

to illustrate i've made 2 classes inherit our abstract class kit. ninja , test.

public class ninja extends kit { } 

this class purpose check if name has value of foo or not.

then need our actual test class well.

public class test extends kit {     public static void main (string[] args) {         test ninja = new test ();         system.out.println(ninja.getname());    // foo         ninja ninja2 = new ninja ();         system.out.println(ninja2.getname());   // foo     }  } 

they both of different types, test resp. ninja both have value of foo in name field. true every class inherits kit.

if must overriden requirement suggest add constructor of kit force user add data base class.

public abstract class kit {     protected string name;      public kit (string name) {         this.name = name;     }      public string getname () {         return name;     } } 

now every class inherits kit must invoke super (string), meaning name field set every object. can different class extends kit , class b extends kit. searched for?

if so, implementing class a , class b along these lines.

class extends kit {     public (string name) {         super (name);     } } 

and b following.

class b extends kit {     public b (string name) {         super (name);     } } 

now different classes, can hold different fields , methods, both need set name field of base class: kit.


Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -