Jump to content

Interface help


Recommended Posts

Posted (edited)

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
Posted
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.

 

Posted
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
Posted (edited)
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
Posted (edited)
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
Posted
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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