Conflict of method call using Interface in Java

Question

Yesterday, I had an interview and I was given the following scenario:

There are 3 classes namely Main.java, MobilePhone.java, DeskPhone.java and one Interface ITelephone.java. powerOn() method is implemented in both classes MobilePhone.java and DeskPhone.java.

How can I call powerOn() method in DeskPhone class after creating an instance of MobilePhone class? In other word, how can I print "You are in DeskPhone class" and "You are in MobilePhone class" in last two calls in Main class?

Is there any another way to solve this problem without renaming powerOn() method in either of class?

Main.java

public class Main {

public static void main(String[] args) {
    ITelephone timsPhone;
    timsPhone = new DeskPhone(123456);
    timsPhone.powerOn();
    timsPhone = new MobilePhone(45678);
   //Question begins here 
    timsPhone.powerOn();
    timsPhone.powerOn();

    }
} 

ITelephone.java

public interface ITelephone {
     void powerOn();
}

MobilePhone.java

public class MobilePhone implements ITelephone{

    private int myNumber;

    public MobilePhone(int myNumber) {
        this.myNumber = myNumber;
    }
    @Override
    public void powerOn() {
        System.out.println("You are in MobilePhone class");
    }
}

DeskPhone.java

public class DeskPhone implements ITelephone {

        private int myNumber;

        public DeskPhone(int myNumber) {
            this.myNumber = myNumber;
        }

        @Override
        public void powerOn() {
            System.out.println("You are in DeskPhone class");
        }
}

Show source
| java   | interface   2017-08-08 21:08 3 Answers

Answers to Conflict of method call using Interface in Java ( 3 )

  1. 2017-08-08 21:08

    Assign the MobilePhone object to a different local variable.

    In the current code, once the value of the timsPhone variable is replaced by the MobilePhone object, the DeskPhone object is unreachable and you cannot call its powerOn() method.

    Suggested code:

    ITelephone timsDeskPhone = new DeskPhone(123456);
    timsDeskPhone.powerOn();
    ITelephone timsMobilePhone = new MobilePhone(45678);
    timsMobilePhone.powerOn();
    timsDeskPhone.powerOn();
    

    Output

    You are in DeskPhone class
    You are in MobilePhone class
    You are in DeskPhone class
    
  2. 2017-08-08 22:08

    You might try to isolate in a static method the calling to your interface method..

    public class Main {
    
        public static void main(String[] args) {
    
              phonePowerOn(new DeskPhone(123456));
    
              phonePowerOn(new MobilePhone(45678));
    
        }
    
    
        static void phonePowerOn(ITelephone timsPhone){
    
            if (timsPhone != null){    
               timsPhone.powerOn();
            }
        }
    
    } 
    
  3. 2017-08-09 16:08

    Either you misunderstood the question, it was a trick, or possibly a little of both. Were those first four lines of main given to you like that? Did they tell you that you can't change them? Then the tactful response might be adding a few lines strategically like this:

    static void main(String[] args){
      ITelephone timsPhone;
      timsPhone = new DeskPhone(123456);
      timsPhone.powerOn();
      ITelephone timsDeskPhone = timsPhone;
      timsPhone = new MobilePhone(45678);
      ITelephone timsMobilePhone = timsPhone;
      timsPhone = timsDeskPhone;
      timsPhone.powerOn();
      timsPhone = timsMobilePhone;
      timsPhone.powerOn();
    }
    

    This meets their specifications and shows an understanding of interfaces and references. With a question like this, the code is only of secondary importance. I would think they're really trying to assess communication, response to stress, and maybe even creativity. So just immediately saying, "You can't do that" really isn't much better than one that throws a ClassCastException.

    The fun way is to fight fire with fire:

    public class MobilePhone implements ITelephone{
    
      private int myNumber;
    
      public MobilePhone(int myNumber) {
          this.myNumber = myNumber;
      }
    
      private int powerOnCallCount = 0;
    
      @Override
      public void powerOn() {
        if (powerOnCallCount == 0){
          System.out.println("You are in DeskPhone class")
        } else {
          System.out.println("You are in MobilePhone class");
        }
        powerOnCallCount++;
      }
    }
    

    Again, we've only added code, it meets their specifications, and shows the same understanding of interfaces and references.

    Or course, a sarcastic answer like this usually won't win points with the interview panel, but has use in the right situation. Remember, they're on trial, too. If I've already tried to gently steer towards a solution like the first and they don't back down, now I'm getting suspicious of their technical knowledge and company culture. If I've noticed a few other yellow flags, then I'd consider something like this to turn the tables. The kind of company I'd want to work for would appreciate that I already tried the tactful approach, met their specifications without compromising technical knowledge, and had enough spine to try my own test on them. I'd probably only resort to it if I was already convinced I didn't want the job, but wanted to give them one last chance to win me over.

    Plus, if they practice test driven design, they'd pretty much have to offer me the job right on the spot.

Leave a reply to - Conflict of method call using Interface in Java

◀ Go back