Sunday, May 10, 2009

Puzzle 23 – Implement the Unimplemented! (Code Complete – 1)

Language – Java | Type – Problem (Code) | Last date 14-May-2009 9:00 p.m. IST | Points 3

Twisters were started with the objective of having cute Java puzzles – puzzles that are based on really simple principles yet stubble enough to make you pause and think.

However somewhere down the line I though some of the puzzle were too simple – you could just pick the code compile it and punch in the output as the answer. I decided to raise the bar a bit and see how things go.

So here the first puzzle of the new series Code Complete (well its called code complete – because that's what you have to do - complete the code!)

package com.twisters;

interface TwistedInterface{void getA();}

public class TwistedClass implements TwistedInterface {
public static void main(String[] args) {
System.out.println(
"In Main");
}
}

Get this code to compile - You may add (but not delete) any additional amount of code you like with one exception -
you may not implement the getA() method inside the TwistedClass. Feel free to make any other additions!

Commenting out any existing code - is considered as deleting. Cute statements like public class Twisted /*implements TwistedInterface*/ don't count as correct answer thought I really do appreciate ingenuity! -:)

I think this is a tough puzzle – in case you disagree you might want to leave your solution here.

I'd like feedback for this particular puzzle – is it hard or still too easy? Feedback is as always highly appreciated!

5 comments:

  1. Would you accept this?:

    interface TwistedInterface{void getA();}

    public class TwistedClass extends TwistedHelper implements TwistedInterface {

    public static void main(String[] args) {
    System.out.println("In Main");
    }
    }

    class TwistedHelper implements TwistedInterface {
    public void getA(){}
    }
    }

    class TwistedHelper implements TwistedInterface {
    public void getA(){}
    }

    Regards,
    Stefan

    ReplyDelete
  2. How about we add an "abstract" ?? this way we won't have to implement the method, because it's automatically assumed that it's an abstract method and thus should be implemented by any subclass :)

    package com.twisters;

    interface TwistedInterface{void getA();}

    public abstract class TwistedClass implements TwistedInterface{
    public static void main(String[] args) {
    System.out.println("In Main");
    }
    }

    ReplyDelete
  3. 1) make TwistedClass abstract
    2) public class TwistedSubClass extends TwistedClass {void getA(){}}

    ReplyDelete
  4. package com.twisters;

    interface TwistedInterface{void getA();}

    public abstract class TwistedClass implements TwistedInterface {
    public static void main(String[] args) {
    System.out.println("In Main");
    }
    }


    Make the class abstract and it will compile.

    ReplyDelete
  5. Here is my solution:

    interface TwistedInterface{void getA();}

    class Twisted implements TwistedInterface {
    public void getA() {}
    }

    public class TwistedClass extends Twisted implements TwistedInterface {
    public static void main(String[] args) {
    System.out.println("In Main");
    }
    }

    I added a class that implements the interface and gives a concrete version of the getA method. Then I had the TwistedClass extend that class because subclasses that implement an interface do not have to implement the interface's methods if they are already implented in a superclass.

    ReplyDelete

Solution for this question?