Jump to content

Interface help


Jammer

Recommended Posts

So I'm trying to learn about interfaces and abstract classes. 

I understand you can do this: List list = new ArrayList(); but not List list = new List(); since List is an interface.

What I don't understand is why I can do this: RS2Object tree = objects.closest("Tree"); 

Doesn't objects.closest return an RS2Object? Isn't RS2Object an interface?

I thought you couldn't make an object of an interface or am I missing something here?

Edited by Jammer
Link to comment
Share on other sites

18 minutes ago, Qubit said:

Your not creating an interface. You are simply treating an object that implements an interface as the interface. Which at the heart is polymorphism.

I don’t get it. Whats the difference between: RS2Object tree = objects.closest("Tree"); 

and

List list = new List(); which obviosuly doesn’t work

To me it seems like it’s the same thing.

 

Link to comment
Share on other sites

55 minutes ago, Jammer said:

I don’t get it. Whats the difference between: RS2Object tree = objects.closest("Tree"); 

and

List list = new List(); which obviosuly doesn’t work

To me it seems like it’s the same thing.

 

In the first statement, you a calling a method that returns a rs2object. In the second statement, you are calling the constructor of the interface, which does not exist because it is an interface. 

  • Like 1
Link to comment
Share on other sites

1 hour ago, Qubit said:

In the first statement, you a calling a method that returns a rs2object. In the second statement, you are calling the constructor of the interface, which does not exist because it is an interface. 

So we're not creating an object in the first statement but we are making a reference? The RS2Object then changes when the tree is choped down for example?

I just can't grasp my head around the tree being an interface. I thought interfaces were a bunch of abstract methods. Do you have an example of something similar outside of osbot?

 

Sorry if my questions are confusing.

Edited by Jammer
Link to comment
Share on other sites

32 minutes ago, Jammer said:

So we're not creating an object in the first statement but we are making a reference? The RS2Object then changes when the tree is choped down for example?

I just can't grasp my head around the tree being an interface. I thought interfaces were a bunch of abstract methods. Do you have an example of something similar outside of osbot?

I understand what is confusing you. As you have said, RS2Object is an interface, and as such we cannot instantiate it. For example, we cannot do

RS2Object anObject = new RS2Object();

However, we can refer to an RS2Object implementing class, e.g InteractableObject, as an RS2Object. While Objects#closest returns an RS2Object, in reality it is returning an instance of a concrete class which implements RS2Object.

Take this example. Consider some interface:

public interface SomeInterface {
  public String someMethod();
}

We can then have some classes implementing this interface. For example:

public class SomeClass implements SomeInterface {
  public SomeClass() { ... } // Constructor
  @Override
  public String someMethod() {
    return "Hello from SomeClass";
  }
}

... and we can have another class also implementing this interface:

public class SomeOtherClass implements SomeInterface {
  public SomeOtherClass() { ... } // Constructor
  @Override
  public String someMethod() {
    return "Hello from SomeOtherClass!";
  }
  public String someOtherMethod() {
    return "ABC";
  }
}

We can then refer to both of these classes as their parent interface. For example:

List<SomeInterface> list = new ArrayList<SomeInterface>();
list.add(new SomeClass());
list.add(new SomeOtherClass());

for (SomeInterface item : list) {
  System.out.println(list.someMethod());
}

// -- Output -- //
> "Hello from SomeClass!"
> "Hello from SomeOtherClass!"

HOWEVER, note that this would not compile:

SomeInterface example = new SomeOtherClass();
System.out.println(someMethod()); // This works!
System.out.println(someOtherMethod()); // Uh-oh! This does not work as SomeInterface does not have a 'someOtherMethod' method, despite SomeOtherClass having it.

Hopefully that cleared things up. I didn't write the code in an IDE so hopefully I didn't make any silly typos/mistakes...

Let me know if you're still confused

Apa

Edited by Apaec
  • Like 1
Link to comment
Share on other sites

3 minutes ago, Jammer said:

Thanks for taking your time. Really cleared up the confusion about returning an rs2object. Gonna do some more reading on the whole concept.

No problem. Sounds like a good plan - Polymorphism is an important concept!

Let me know if you run into any further problems; i'm always happy to help! (:

Apa

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...