Java Essentials - Interfaces in java
6K views
Oct 24, 2024
Java Essentials - Interfaces in java
View Video Transcript
0:00
Interfaces in Java
0:03
So now what is one interface? Interface will have only the function prototypes
0:09
And all the variables which will be defined under the interface with initialization
0:14
will be treated as a constant type of variable. So in case of interface, we don't write the function bodies
0:20
We'll be writing only the function prototypes. So that's why you can say that interfaces are going to contain only the abstract methods
0:28
So there is a special features. a special features of interface which is making it different from the class let us
0:34
suppose we're going to organize one party for our friends in that case we are hiring
0:40
or we are doing the food preparation outsource to one of the catarers so we are
0:45
making a menu that these are the items will be there in our respective menu so
0:50
whenever you were making a deal with the caterer so in that case we are telling
0:54
that we required food number one food number two food number three and say food
0:58
number four we are not mentioning what is the respective recipe to prepare those foods
1:03
that means we are defining only the function prototypes not defining the function
1:08
bodies now it will be the headache or it will be the responsibility of the respective caterer to make the food ready according to the recipe they might be knowing and they should present that that food in front of
1:21
us when the party will be on so that is a concept so that is here in case of interfaces we
1:27
will be writing different methods only the prototypes will be written not the respective method
1:33
bodies so let us suppose we are going to define one interface that is a bank account in that case
1:38
we're having some set of methods will be there what are the operations that you can
1:42
perform on the bank account that is the account balance check it is one
1:46
method depositing the depositing the money that is another method withdrawing the
1:52
money that is another method checking the current balance that is might be the
1:57
another method so you see we're having multiple different operations that you can
2:01
carry out on a bank account so whenever we define one interface against this bank account
2:08
will be mentioning this function prototypes without their respective bodies. So that is the concept of interface
2:16
An interface is a collection of abstract methods, that means only the prototypes will be there
2:21
A class can implement an interface and we will use the word implements remember implements keyword to use an interface That means to implement one interface in a class I require to use the keyword implements
2:38
We cannot instantiate an interface because the methods are not defined and it does not contain any constructor
2:47
Multiple inheritance concepts can be implemented using this interface concepts in Java
2:55
When a class is implementing an interface, all the methods which are present in the interface
3:00
must be defined in the respective implementing class. So let us go for one practical demonstration where we'll be showing all these concepts
3:09
into details, writing the codes, executing and all to clear your conception better
3:16
In this demonstration we shall discuss and we shall write code on interface in Java
3:21
So at first we are going for one interface. definition so my interface name is my interface one so public interface
3:35
my interface one I just defining two functions here from Java 1 onwards interface can also contain function with the function
3:53
bodies if they are of type default or static so let us define such functions here
4:10
Thank you. So in this way we have defined my interface one
4:50
Let us go on defining my interface 2 which will inherit it which will inherit my
4:56
interface 1 so I'm going to define another one
#Computer Education
#Computer Science
#Java (Programming Language)
#Programming