#Interfaces

1 messages · Page 1 of 1 (latest)

lunar knot
#

hello, im swithing from c# to java as my uni course teaches it - im a noob. If I'm creating an interface and are creating a function in which will return a different value on each class that is extending from it. in c# we can use T but I cant find any documentation on it in java. Sorry if im being stupid

compact stirrupBOT
#

<@&987246399047479336> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#
TJ-Bot
Hello, im swithing from c# to java as my uni course teaches it - im a noob. If I'm creating an interface and are creating a function in which will return a different value on each class that is extending from it. in c# we can use T but I cant find any docu

t implements it, how do I do that in Java?

To achieve this in Java, you can define a method in your interface without providing an implementation. Each class that implements the interface will then provide its own implementation of the method, allowing it to return a different value based on the specific class.

Here is an example interface declaration:

public interface MyInterface {
    public int myMethod();
}

And here is an example class implementing this interface with its own implementation of the method:

public class MyClass implements MyInterface {
    
    @Override
    public int myMethod() {
        // Provide a unique implementation for this class
        return 42;
    }
}

You can have multiple classes implementing the same interface with their own unique implementations of the method. This way, each class can return a different value when calling myMethod().

I hope this helps! Let me know if you have any more questions about interfaces in Java.

lunar knot
#

^ this I understand how to override a method in a extended class but I want each class to inherit the function but the data type return will be different in every class that extends the interface

#

please lmk if im not making sense or being stupid

#

literally trying to make a get set function but not want to write it in every class

tough ravine
lunar knot
#

Yes generics

#

If you could point me in the write direction on the documentation

tough ravine
#
class Foo<T> {
  private final T value;
  ...
}
lunar knot
tough ravine
lunar knot
lunar knot
#

as a parameter

lunar knot
#

ahh it worked now I think i was doing something wrong

#

ty