question about java interfaces

Let's say I have the following ruby code :

def use_object(object)
  puts object.some_method

and , this will work on any object that responds to some_method,right?

Assuming that the following java interface exists :

interface TestInterface {
   public String some_method();

Am I right to presume that interfaces are java's way to achieving the same thing ( with the only difference that the parameter's type would be TestInterface ) ?

Asked by: Grace543 | Posted: 28-01-2022

Answer 1

You are right except that you can not define the body of a function in Java Interfaces, only prototypes.

Interfaces are the only way to implemente a pseudo multi-derivation in Java, since normal class derivation is only simple (just one parent).

Answered by: Joyce467 | Posted: 01-03-2022

Answer 2

No, interfaces in are not implemented. You can have multiple implementations of it though.

An interface would look more like:

interface TestInterface {
   public String some_method();

And it could be implemented in a class:

public class TestClass implements TestInterface {
   public String some_method() {
       return "test";

And maybe more classes that implement this method differently. All classes that implement an interface have to implement the methods as declared by an interface.

With interfaces you can't achive exactly the same as in your Ruby example since Java is static typed.

Answered by: Edgar640 | Posted: 01-03-2022

Answer 3

Java interfaces define method signatures which an implementing class must provide. The JavaDoc explains all this in great detail.

Answered by: Kellan941 | Posted: 01-03-2022

Answer 4

In Java interfaces can only be used to declare methods, not the define (implement) them. Only classes can implement methods. But classes can implement interfaces. So you could for instance use the Adapter pattern to realize the same thing you did in ruby.

Answered by: Maria861 | Posted: 01-03-2022

Answer 5

Yes, but only if you want to abstract out "anything having a some_method()" as a separate concept. If you only have one class that has some_method(), you need not specify an interface, and the parameter of use_object() will be that class.

Note also, that in Java we use camelCase instead of underscore_separated names.

Answered by: Nicole465 | Posted: 01-03-2022

Answer 6

It look like you are trying to program in Ruby using Java, you want want to rethink your approach to use more the idioms of the language.

Answered by: Roman100 | Posted: 01-03-2022

Similar questions

java general question about interfaces

considering i have a method which gets a List passed as an param. Within this method i want to use for instance an ArrayList specific function on that list (lets say trimToSize()). What would be the general approach to deal with a problem like this ? Here two example: First approach (i don't think this is good) private void doSomething(final List<T> list) { // ... do something ((ArrayList<...

Java Interfaces Question

Hi I have the following interfaces that I need to create. Can you just answer one question. Am I supposed to create a variable to hold the value of the operator and the values of the two operands? If so, should these be created inside the interfaces or inside the class with my main method? interface ArithmeticOperator { // Returns the result of applying the operator to operands a and b. double ope...

General question about interfaces in Java

This question already has answers here:

java - spring and interfaces

I read all over the place about how Spring encourages you to use interfaces in your code. I don't see it. There is no notion of interface in your spring xml configuration. What part of Spring actually encourages you to use interfaces (other than the docs)?

java - JPA mapping interfaces

I am having trouble creating a mapping when the List type is an interface. It looks like I need to create an abstract class and use the discriminator column is this the case? I would rather not have to as the abstract class will just contain an abstract method and I would rather just keep the interface. I have an interface lets call it Account public interface Account { public void doStuff(); }

java - How do I inherit from multiple extending generic interfaces?

I have a few classes such that: public class XMLStatusMessage extends XMLMessage {} public abstract class XMLMessage implements IMessage {} public interface IMessageListener { public void onMessage( IMessage message ); } public interface XMLMessageListener <T extends XMLMessage> extends IMessageListener { public void onMessage( T message ); } public interface XMLStatusMessageListener ...

oop - Java Interfaces?

Closed. This question does not meet Stack Overflow guid...

Best way to get started on simple 3D user interfaces using Java?

I'm writing a time management application and I have an idea for presenting timelines and todo items in 3D. Visually, I imagine this as looking down a corridor or highway in 3D, with upcoming deadlines and tasks represented as signposts - more important items are larger and upcoming deadlines are nearer. I want to do this in Java, however I have no idea how to begin. For example, I would like to be able to render t...

java - Why are interfaces preferred to abstract classes?

I recently attended an interview and they asked me the question "Why Interfaces are preferred over Abstract classes?" I tried giving a few answers like: We can get only one Extends functionality they are 100% Abstract Implementation is not hard-coded They asked me take any of the JDBC api that you use. "Why are they Interfaces?". Can I get a better answer for t...

java - Scala and interfaces

In Java I would typically declare my entire domain as interfaces, possibly with some kind of Factory to get me implementations. This is partly because I am so old I can remember when some persistence layers required implementation classes to subclass a specific class but also so that I can easily: mock objects for testing purposes proxy objects at runtime if nec...

java - What methods and interfaces do you (almost) always implement in classes?

Which methods and interfaces do you always implement in your classes? Do you always override equals()? If you do, do you also do hashcode()? toString()? Do you make it a habit to implement the Comparable interface? I've just written some code where I needed to implement compareTo() and override equals() to get my program to work in a sane manner; I now start seeing ways of using these everywhere... ...

java - Interfaces declaring methods with abstracts as parameters

I have this question about best practices in following examples: interface Request; interface Service { void process(Request request) } class MyService implements Service; class YourService implements Service; class MyRequest implements Request; class YourRequest implements Request; But how to ensure that MyService will always receive MyRequest and YourS...

java - JiBX: How do I keep using interfaces in my code?

How can I keep my using interfaces in classes I want to use JiBX binding with? Example: I have this very simple model in java: public interface A { B getB(); void setB(B b); } public interface B { String getData(); void setData(String data); } public class AImpl implements A { B b; @Override public B getB() { return b; } @Override public void setB(...

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)