In this lab, we are going to talk about abstract classes, then we will talk about interfaces, and how we could implement event listeners in JavaFX.
A lot of times when we are defining our classes, we noticed that a superclass is not created so we could create an object of it, we created that class so we can have a common type between other subclasses.
As you might notice in our previous example, we've created Shape
class, which is the superclass for Circle
, Rectangle
classes, and we've created the method getArea
in the superclass Shape
, but the information in Shape
is not enough to calculate the area, so we defined it and let it return zero, this solution is not correct.
The Shape
class is created only to create a superclass, but we won't have an object of shape, because it doesn't even make sense in the real world.
So this is the concept of Abstract class, it's a class that you couldn't create objects from it, but you can use it to be supertype for other classes.
Notice that getArea
method doesn't have a body, so each concrete subclass should implement its body.
Shape
type has the method getArea
but it doesn't have its body.
An interface describes what a class can do, the class itself should implement certain methods defined in the interface.
As you know, a class can only extend one class, this restriction will save us from a lot of mistakes and errors, but we need something that tells the others that the class can do certain jobs (or methods), this is what an interface if for.
You could define an interface like this:
Notice that the name of an interface is an adjective, if a class implements this interface, it should implement its method interact
, so we could use polymorphism to store an object type that implements this interface in a variable type of the interface.
You could also implement as many interfaces as you like.
This interface defines compareTo
method, which will make your life easier if you want to use the built-in array sorting methods.
Now you can sort an array of Shapes using java.util.Arrays.sort
.
To handle an event in JavaFX, you should handler object should implement the EventHandler
interface, please take a look here.
This interface contains a method handle
that should be impelemented.
Notice that we've created an object for each button, this object contains some information (the number of the button) and implements the handle
method.
Creating a class that implements the method, then creating an object for each button is a time-consuming job, so luckily Java let us create a class and an object from it in one line.
And we've another shortcut we can use for interfaces with only one abstract method, we could write lambda expressions:
You should choose which one you will use, but notice that there is a difference between each form, sometimes it's much easier to create a class that implements EventHandler, but most of the times lambda expression will be enough.
Programming
Java
IUG