Hello, dear students, in this lesson, we are going to talk with you about interfaces from this lesson,
you learn what interfaces are and what do they consist of.
Also, we'll discuss what is the difference between abstract class and interface.
And at the end of the lesson, I will tell you what is the difference between such terms as interface,
class and time.
Let's go as I just start and look at examples to understand what interfaces are.
First of all, let's come up with a definition of an interface interfaces, an abstraction which defines
a behavioral contract that must be followed by every class that implements this interface.
In other words, you specify a set of expected behavior which should be present in each class that implements
this interface.
But it don't specify how exactly this behavior will be implemented.
For example, have interface here for payment processor.
I need to have some type in my program that will be able to process payment and send information about
our payment to Payment Gateway.
That's why I want to declare what method with the name process payment, the text payment data object
as an argument.
Payment data is simple project loss that has the payment amount tax amount idea of a user who wants
to make a payment and currency.
The rest of the message here I just getters and setters.
Now let's get back to our interface.
You see that I declared method without body and without any other modifiers.
Why?
Because invisibly, all matters in interface have modifiers, public and abstract.
So in case I will add here public and abstract modifier, nothing will change.
But there is no sense in any public abstract modifiers because all methods and interfaces by default
are public and abstract.
I can say that this interface said the contract on the process payment behavior by same contract.
I mean, this interface clearly defines what a return type will be, how method will be called and what
parameters will be in it, period.
But this interface doesn't bother about what exact code will be written.
And now when I have such interface in my program, I can create time that will implement this contract.
I have changed payment processor and PayPal payment processor both implements payment processor interface.
That means these classes are obligated to implement process payment methods.
Since this method is abstract in the interface in our concrete class, we must implement this method.
If only our class will be abstract.
There is no need to implement this method.
Let me call on this method and at absolute keyword before class.
You see, there is no compilation error.
But since this class is not abstract, there will be compilation error.
In case we won't implement this method, you can press control plus one and select and implemented methods
to generate stop method.
I will just on existing one.
This method will work differently.
For example, chase payment processor.
Because of the specifics of Payment Gateway, it should obtain security token first for authorization
and only after that we can initialize payment page with secure token and the amount of money to pay.
On the other hand, let's pretend that PayPal payment processor needs to request payment for by using
a specific application key.
And after that we have to fill out this form of payment details and submit it.
You can notice that in both cases we have override annotation here.
This notation exists for the sake of a successful compilation.
Do not switch the topic.
Let's keep focus on our interfaces.
I will tell you more about overwrite annotation in our detailed lesson about polymorphism.
Do we see these two examples?
The behavior is the same both classes, protests, payment data.
But they do this in different ways.
What do all this look like exactly?
This look like implementation of polymorphism principle, one of the main or P principle?
Why else?
We need interfaces to build our application was dependency on the obstruction types, but not on the
concrete classes.
Let me show you this example.
You mentioned we have some client of our code.
It could be some other class so-called facade class or controller class.
That doesn't matter.
And this class can simply rely on the interface of payment processor type.
We can say that by default, variable of payment processor reference to PayPal payment processor.
But in fact, this client doesn't bother with implementation of payment processor.
We will use PayPal payment processor or chase payment processor.
Moreover, somebody can use Setar and that is a chase or PayPal payment process during the runtime.
The good thing about interfaces and they define level of abstraction and we can build our program relying
on this abstraction.
They see this method and location and checkout method no matter what implementation of payment.
Sir, I will have here during Zeron time, I don't need to change this code and also pay attention to
one more thing here.
I can initialize variable of interface type with a reference to an object of any other class which implements
this interface.
OK, Holmes, this is clear.
Now, let's take a look what our interface can contain.
As you already understand, our interface can contain abstract methods.
It can have as many methods as you wish.
But later we will learn other object oriented principles and you will understand that it is better to
have a lot of small interfaces rather than one.
And besides abstract methods, our interface can contain default methods.
Default methods are methods that have body so you can treat them like regular methods in concrete glass.
But the Java syntax wouldn't allow you to declare a method with body and interface without default.
Keyword in case iRace the default keyword compiler tells me that abstract methods don't have money because
compiler treats this method as abstract.
Also, our interface may contain static methods.
We are going to have a separate lesson about static keywords, but right now you can remember the static
methods can be called without any object.
So you don't need to write newcomer's and create objects.
You can just write type name and column method like I did here.
For example, I wrote the name of the interface and invoke static method on this interface.
Notice I didn't use variable of payment processor type.
I use interface.
It is not very object oriented approach.
Why wait for the in the most cited keywords and I will tell you more about it.
So far, you know that interface can contain abstract default and static methods.
What else interface can have constants.
For example, imagine the real life scenario in case our payment failed.
We need to retrieve a few more times.
I create variable of type retry attempts and you probably noticed that the clips for months, this variable
name like a constant I'm in cursive and another color.
That is because by default, all fields and interfaces have public static.
Final modifiers like all constants have public so that everyone could access it static because constant
should exist in a single copy and final so that nobody could override it.
We will talk more about constants in the lesson about static keywords.
And even if I would add these modifiers here, nothing will change.
But usually developers give this modifiers in interfaces.
Let me also removed important things that we didn't discuss yet.
If the fact that class can implement multiple interfaces at once, I'm not sure whether you notice that
our chase payment processor implements two interfaces, the one we discussed already, and another one
is payment for the data.
If this class implements payment for the data interface, that means this class should implement validate
payment methods which will validate payment to be sure that it has all necessary information for payment
processing.
In this payment gateway in Java, it is allowed to implement multiple interfaces.
But in one specific case, this can lead to compilation error.
And I want you to know this case.
Let's discuss it.
For example, in payment processor interface, you have some default method and in payment for the data,
you also have default method with the same signature.
Here it this this can lead to compilation error in chase payment processor class, because during the
runtime it will be unclear which default method should be used.
The one from payment for the data was the one from payment processor.
That's why it doesn't allow us to compile this code until we resolve issue was duplicated default methods.
So now you know really a lot about interfaces.
It is time to understand how good you learned interfaces and ask you what is the difference between
abstract classes and interfaces.
Absolute class we can extend with extensive keyboard interface.
We can implement using implements, keyboard apps.
A class can have properties with all possible modifiers.
On the other hand, all properties interfaces have public static final modifiers.
You can extend only one class in Java, but you can implement multiple interfaces.
You remember that example when our PayPal payment processor implemented two interfaces at a time?
Basically that the most significant difference between abstract classes and interfaces.
It is also worth to mention that the introduction of default and static methods and interfaces made
it harder to distinguish interfaces from abstract classes.
Based on the difference which we discussed, you can select whether you need to use abstract class or
interface.
For example, if you know that you need to list properties with private or protected access modifiers
that are not static, you already know that you need to use abstract class instead of interface.
And if you need to be able to implement specific interface together with other ones, you know that
you need to use interfaces.
Last but not the least question for today and our agenda is to define the difference between such terms
as class type and interface, you already know what a class is.
Ensure that class is a template for your future.
Object's interface is an abstraction which defines application programming interface and contract for
behavior.
But what is a type probably you heard during the lessons that I use type word as a synonym when I reference
to class interface because class is a type interface is also a type.
That means every type is also is a class and interface and enumeration or an array.
Also, each object has a time and every expression has a type.
If during that interview you will be asked what a typist you can answer something like this type summarizes
the common features of a set of objects with the same characteristics.
Class is a concrete data structure and group of methods that represents an implementation of the type.
Let's look at this example one more time attentively.
Payment processor variable has payment processor type, but this variable reference to the PayPal payment
processor object.
So we can say that the object has a payment processor type.
And you see here in the method parameters, this sector can accept any object of type payment processor.
That means this method can work with any object that implements this interface doesn't make sense.
Hope with this examples, things become clearer.
Let's recap what we have learned today.
In this lesson, we learned what interfaces are, what interfaces can contain inside, how to implement
interface, what default methods and interfaces are, what static methods interfaces are.
Now, you know what the difference between abstract class and interface is.
And you also learn the difference between type and object.
That's all what we have for today.
Thanks a lot for your attention and see you in the next lesson.