-1

In OOP, I learned that we can call a method by using its class.

For example:

Person person = new Person();
person.methodA(); // calling the method in person class

Based on javafx docs of ListView (link) , The getSelectionModel() is a method of a class ListView (okay working). But the selectedItemProperty() method is the class of SelectionModel (link)

How can you call a method selectedItemProperty() without using its SelectionModel class? And what class does this addListener method came from?

myListView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TodoItem>() {
///  blah blah blah
}
});
  • No. Even if you use reflection, the class is still involved. There's no need to import it, since you don't declare any variables of that type, but this does not change the fact that a method returning a `SelectionModel` instance is used. As for the `addListener` method. Take a look at the methods of [`ReadOnlyObjectProperty`](https://openjfx.io/javadoc/12/javafx.base/javafx/beans/property/ReadOnlyObjectProperty.html). It's down in the `ObservableValue` section... – fabian Nov 03 '19 at 08:46

2 Answers2

2

MultipleSelectionModel (link) extends SelectionModel (link), in which the selectedItemProperty() method is implemented.

selectedItemProperty() returns a ReadOnlyObjectProperty (link) instance, which extends ObservableValue (link), from which the addListener() method is coming.

As for how you can call these methods- this is just how inheritance works. The subclass (MultipleSelectionModel) inherits all of the properties of its superclass (SelectionModel). Thus any methods defined in SelectionModel you can call against a MultipleSelectionModel instance.

OOP Concept for Beginners: What is Inheritance?

mario_sunny
  • 1,412
  • 11
  • 29
  • I understand that I can call the method of a superclass from a subclass. But how can you call 2 methods at the same time? 'Emma emma = new Emma(); // extends Person class' 'emma.methodA.methodB(); //this is confusing' – user11880836 Nov 03 '19 at 06:46
  • Also i'm aware of inheritance. But I still don't understand how is this SelectionModel class related to ListView if the ListView class does not extend the SelectionModel? – user11880836 Nov 03 '19 at 06:47
  • @user11880836 A `ListView` can have properties of type `SelectionModel` right? It can return this selection model from a method. Something like `class ListView { SelectionModel selectionModel = ...; SelectionModel getSelectionModel() { return selectionModel; } }` Think about the call chain as the compiler creating temporary variables invisible to you: `MultipleSelectionModel temp1 = myListView.getSelectionModel(); ReadOnlyObjectProperty temp2 = temp1..selectedItemProperty(); temp2.addListener(...);` – fabian Nov 03 '19 at 08:56
  • @user11880836 See https://stackoverflow.com/questions/34541477/, https://stackoverflow.com/questions/3441090/, and https://stackoverflow.com/questions/49002/. – Slaw Nov 03 '19 at 16:00
0

When you call ListView#getSelectionModel() you are getting an object which is an instance of SelectionModel—more specifically, an instance of MultipleSelectionModel. Since you now have an instance of MultipleSelectionModel you can invoke methods present in that class, including inherited methods. This means you can do the following:

listView.getSelectionModel().selectedItemProperty().addListener(yourChangeListener);

Which is equivalent to the following:

MultipleSelectionModel<T> sModel = listView.getSelectionModel();
ReadOnlyObjectProperty<T> selectedItemProp = sModel.selectedItemProperty();
selectedItemProp.addListener(yourChangeListener);

As you can see, you are using the class of every object involved. The former, known as method chaining, is just shorthand.

Slaw
  • 37,820
  • 8
  • 53
  • 80