Java class implementing an interface and another class extending this class -
i have java interface has few abstract methods.
public interface interface { void test(); void test1(); }
now have class implements interface. requirement want extend class multiple client classes. class should implement test(). want class should keep test1 abstract , implementation of test1 should done in clients of class a. possible achieve in java. if yes, can correct way implement requirement ?
first of all, every method interface public , abstract default. have no choice.
here's how i'd write it, better names:
public interface { void test1(); void test2(); } public abstract class b implements { { public void test1() { // here; concrete implementation } // note: nothing test2. // compiler doesn't complain because it's abstract class, , abstract method. } public class c extends b { // note: nothing test1 - abstract superclass // note: compiler complain if nothing done implement test2(), because c isn't abstract public void test2() { // here; concrete implementation } }
class c
can override test1()
if chooses to, if nothing done it'll inherit behavior specified in class b
.
Comments
Post a Comment