1

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!

HStoltz
  • 183
  • 1
  • 8
  • 1
    1. Compare strings with `equals` method instead of `==`. 2. Declaring classes inside of a method is really weird, not sure why would you want to do that. – Amongalen Mar 13 '21 at 16:55
  • Thank you so much, it worked! I think I mixed with Python. And, well, yeah, declaring classes inside methods is weird, I'm still figuring my way though coding structures tbh, and I don't really know how to work with Eclipse IDE properly - I can't do anything without the main class, so I'm gonna try and clean this up a bit. Thanks a lot! – HStoltz Mar 13 '21 at 17:11
  • 1
    Another note. When overriding it is worth to add `@override` annotation on the overriding method (e.g inside of class `A`). It doesn't change how the code works but it is a "hint" for the compiler on what you wanted to do and it might throw some errors if you make some silly mistake. In your case, your interface contains `static` method, your implementation have `non-static` methods, you're not actually overriding the interface method. – Amongalen Mar 13 '21 at 17:12
  • Thank you so very much for your inputs! Took the static out of the interface, and included the @override annotation – HStoltz Mar 13 '21 at 17:26

1 Answers1

1

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?

Sorin
  • 977
  • 3
  • 12
  • I just put my classes in separated files like you said and it got so much better! Thanks a lot!! – HStoltz Mar 13 '21 at 17:25