-1

In Java Android:

Say, there is an interface:

interface RC
{
  void Run();
  void Turn(Boolean leftRight);
  void Reverse();
  void Stop();
  void Launch();
}

How to mark the method Launch() to be non mandatory implemented in an inherited class?

Getting that

class A implements RC{
  //class A methods... + only 4 from RC
  public void Run();
  public void Turn(Boolean leftRight);
  public void Reverse();
  public void Stop();
  // is not existed Launch
}
  • 1
    You can make that method abstract – Karan Mehta Jan 21 '20 at 06:11
  • 1
    Does this answer your question? [not implementing all of the methods of interface. is it possible?](https://stackoverflow.com/questions/11437097/not-implementing-all-of-the-methods-of-interface-is-it-possible) – tobsob Jan 21 '20 at 06:19
  • 1
    I will second @Nobody's comment - the accepted answer will not even compile. ashok's answer is valid, but typically not desirable (what if other classes implement RC but also inherit from another class?). @ Nobody's answer is the correct choice. EDIT TO ADD: This comment made before accepted answer edited to make an `abstract` class, but @ NObody's answer still matches the question. – racraman Jan 21 '20 at 06:29

3 Answers3

4

For java 8+ you can mark method as default and provide it's implementation as empty body. Although in most cases you will want to separate interface, sometimes it is indeed useful. example would be:

interface A {
    void first();
    default void second(){
       //throw new UnsupportedOperationException(); or do some default logic
    }
}

In your implementing class you would only need to implement first method

Nobody
  • 154
  • 1
  • 8
1

In java interface , compulsory to implement all method.You can use abstract class.

abstract class RC
 {
  void Run(){}
  void Turn(Boolean leftRight){}
  void Reverse(){}
  void Stop(){}
  void Launch(){}

}

class A extends RC{

 //class A methods... + only 4 from RC
 public void Run(){}
 public void Turn(Boolean leftRight){}
public void Reverse(){}
public void Stop(){}
 // is not existed Launch
}
ashok
  • 431
  • 5
  • 8
0

You can try this :

Interface :

interface RC
{
  void Run();
  void Turn(Boolean leftRight);
  void Reverse();
  void Stop();
  void Launch();
}

Your class where you're implementing interface :

abstract class A implements RC{
  //class A methods... + only 4 from RC
  public void Run();
  public void Turn(Boolean leftRight);
  public void Reverse();
  public void Stop();
  // is not existed Launch
}
Karan Mehta
  • 1,442
  • 13
  • 32
  • 1
    Hi. Would you please specify java version of your example? As far as I am aware, it won't work like that on java 8+ (in addition, please explain why interface method should be declared abstract if all methods of interface are abstract by default) – Nobody Jan 21 '20 at 06:19
  • No it won't work like that. – Dorian Gray Jan 21 '20 at 06:30