Skip to content
Advertisement

How can I make classes implement an interface and print their methods based on a given parameter in Java?

I think this might be a very basic Java question, and I apologize since I’m a beginner, but I want to understand what am I getting wrong here: I’m supposed to create a package, and inside it, I must create the following:

  • an interface with a method (the question says nothing besides it, so I created it empty)
  • 2 classes, A and B, which must implement the method created in said interface and print their own names
  • A third class, C, which must override B’s implementation
  • And an Execute method inside the main class. This method must receive a letter as a parameter, no matter if it’s capital case or not, and execute the method of the corresponding class (i.e. if this method receives as a parameter the letter A, it must execute the method belonging to class A)

So far I came up this this, but the code receives the input, and doesn’t do anything:

Interface

public interface Test {
    
    public static void testInterface() {
        
    }

}

Classes

public class Teste {

    public static void main(String[] args) {
        
        class A implements Test {
            
            public void testInterface() {
        
                System.out.println("A");
            }
        }
    
        class B implements Test {
        
            public void testInterface() {
            
                System.out.println("B");
            }
        
        }
    
        class C extends B {
        
            public void testInterface() {
            
                System.out.println("C");
            }
        
        }
        
        Scanner inputLetter = new Scanner(System.in);  // Create a Scanner object
        System.out.println("Enter a letter from A to C: ");

        String resLetter = inputLetter.nextLine();  // Read user input
        
        if (resLetter == "A") {
            A a = new A();
            a.testInterface();
        }
        
        if (resLetra == "B") {
            B b = new B();
            b.testInterface();
        }
        
        if (resLetra == "C") {
            C c = new C();
            c.testInterface();
        }

    }

}

To be quite honest, I may be messing up with the code’s structure too, since I’m not too sure of how should I organize it – I didn’t create the Execute method because I had a lot of trouble creating classes without the main method, and couldn’t put a method inside another, and I want to make it as simple as possible to make it work before I can try bolder things, so any help will be of great value!

Advertisement

Answer

You’re on a good way. I’ll just post some information to get you over your current roadblock.

public interface MyTestInterface {
    void testInterface();
}

Interfaces will just “announce” a method. This just tells you (and the compiler) that any Class that implements MyTestInterface has to supply a method called testInterface(). Don’t make them static, as this would prevent any class implementing the interface from overriding the method.

Put your classes in their own .java file. While you can define a class within a class (so called Inner Class), it has some implications.

A.java

public class A implements MyTestInterface {
    @Override
    public void testInterface() {
        // Objects of Class A do something here
    }
}

MyMain.java

public class MyMain {

    public static void main(String[] args) {
        MyTestInterface implementedByA = new A();
        implementedByA.testInterface();
    }
}

Since it implements MyTestInterface, an Object of Class A is both an instance of A and an instance of MyTestInterface. This allows you, to declare a variable of type MyTestInterface and assign it an implementation of one implementing class.

And as @Amongalen mentioned: How do I compare strings in Java?

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement