- Podcasts
- The Java Developers Hub
- Java Interfaces Explained | Default Methods, Functional Interfaces & Marker Interfaces
Java Interfaces Explained | Default Methods, Functional Interfaces & Marker Interfaces
The Java Developers Hub
- December 3, 2026
- 60 min
- Episode 33
About this episodeInterfaces are a fundamental part of Java's object-oriented programming model. They allow developers to define contracts that classes can…
- Understanding interfaces in Java.
- Why interfaces are important in object-oriented programming.
- How to declare and implement interfaces.
- Understanding interface methods and fields.
- Differences between interfaces and abstract classes.
- Implementing multiple interfaces in a single class.
- Understanding default methods introduced in Java 8.
- Overriding default methods in implementing classes.
- Understanding static methods in interfaces.
- What functional interfaces are and how they work.
- Understanding the @FunctionalInterface annotation.
- Exploring built-in functional interfaces.
- Understanding marker interfaces and their purpose.
- Examples of marker interfaces such as Serializable and Cloneable.
- Understanding interface inheritance.
- Best practices for designing flexible Java applicati
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
Java Interfaces Explained | Default Methods, Functional Interfaces & Marker Interfaces
Welcome back Coder Army to our Java series. And today we are going to read interfaces in depth. Till now we had seen the introduction of interfaces. But now let's go inside and see what can be done in interfaces. What new things are introduced in interfaces after Java 8. You will enjoy it a lot and you will definitely learn a lot of new things. So let's quickly go to the screen and start. Yes, so look, we had understood that there are two ways to implement abstraction in Java.
There used to be abstract classes and interfaces. Today we are going to learn about interfaces in depth. So let's start with interfaces. What do interfaces tell you? First of all, let's see how we make an interface. We have to write the keyword interface. And whatever its name is, let's say we write car. So we have created an interface, car. Now what can come in it? Any method can come in it, which we just declare and not define. So let's say I write a method void drive. Okay, so I have a drive named method, which I have just declared here, not defined. Okay, so this interface that we have created here, what does it actually tell us? Basically, if we want to see the definition conceptually, then what message does the interface want to give us?
That's why it only gave the definition of the drive, not the definition. Now, this was the definition of our abstract class. Right? That what can be done without telling us how to do it. And what was the work of interface? To implement abstractions. So, actually it implements abstraction with this definition. So, let me write its definition once. So here I have written the same definition that defines what an object can do without telling how it does that. Right? That's why we always write the same methods in interfaces which.
Now, till we read about our classes, Till now, we have only studied about classes. So, definition wise, we have also studied that any class is a blueprint of an object. So, we have studied the blueprint of an object. So, what is the interface? Is this also a blueprint of an object? Absolutely not. In fact, interfaces have nothing to do with any object. Because you think, we can't make an object of an interface. We implement an interface with a class. And what does an interface tell us? It tells us some methods which have only declared it and left it. And who is responsible for defining it? The class which will implement this interface. So, if we look at the interface, then the blueprint of a behavior is a blueprint of a So, blueprint of behavior. So, interface tells us that what all behaviors are possible and any class which will implement this interface, it will have to give definition of every behavior. So, whenever we define interface, we know from another name, we say interface.
So I have given my empty file here by doing demo.java. So first of all, I make an interface here. So let's say I made an interface here, car. Okay, now let's say I have given another method here, void drive. Okay, I just declared it and left it. Now, if I want to make a class that implements this interface, then let's say I make a class and we make a specific car, let's say thar. Okay, it implements this car interface. Before that, I should increase the name of the car in capital letter. So implements car, something like this. Now what is this thar? This is the name of the class which implements our car interface.
Now what will be the work of this class? Override all the methods of this car interface. So we will write void drive here and we will overwrite it. Before that, what do we generally like to write? At the rate override. You know it is optional, it is not necessary to write it. But by writing it, I understand that we are overriding a method.
Now see, it is a void method, so I don't need to return anything. But there is an error here. Once we read that error, what is it saying? Cannot reduce the visibility of the inherited method from car. What does this line mean? See, actually what happens is that whatever method we write in the interface, whatever it is, it is by default public. That means even if we don't write a public keyword here, Then Java will.
I said that it is by default and there is no need to write it. If it is public here and I don't write public here. And this is its child class. In a way, it is implementing the car. So if I don't write public in the Thar function in the drive, then by default, which access modifier is used here? Default modifier is used. And you know, public access modifier is more loose than default modifier. Default is more strict, right? Public says you can access it from anywhere. But default says that you can access it from the same package.
Drive method is outside this Thar. Only this package can be accessed. But this car interface, its drive method says that it is public itself. That means anyone can access it from anywhere, no matter which package it is. And this is a very basic rule of Java. The rule defined by the base class, the access modifier defined, you can't narrow it down. You can extend it if you want, but you can't narrow it down. Okay, so here I will have to write public. Something like this. So as soon as I wrote public, my error went away. Now I will print something here. System.out.println And we will write thar is driving. Something like this. Now what I have to do?
Now I can't write car here because car is an interface. So I have to make an object of a concrete class like thar. And now if I do c.drive, then my output will be printed on the screen that thar is driving. So this is the whole concept of interface. Now let's see a few things about it. Look, first of all, we said that whatever class It will be the responsibility of the interface to override all the methods of this class. Let's assume that this class does not want to override the drive. So it wants to leave it as it is. It also wants to leave it by declaring it. So let's say it does not want to override it for any reason.
To do in that case? In that case, we will have to make this method abstract. And this class will also have to be abstract. Now we have called this class abstract. As soon as we called it abstract, an error appeared because we can't make an object of thar because it is abstract itself. Now I have to make a third class. Let's say I name it black thar and it will extend the class. Now its responsibility will be to override this method. So it will write public void drive. And now it will override it in its own way. So basically either this thar class will implement the drive method of this car itself.
Doesn't want to implement then it will declare itself abstract. It will say that I am abstract. Someone else will inherit me like black thar. And it will implement this drive method. So you must have understood this rule. Now interface uses polymorphism very well and gives us a very good benefit which is dynamic dispatch or what we also call dynamic polymorphism.
Okay, now because this payment is an interface, so let's keep a method in it which we write as void pay. Okay, and we declare it and leave it. Now I make two new classes. Let's say I have made one class credit card which implements implements payment ok now its work will be to overwrite this credit card method so obviously we have to write public public void pay and you know why public because by default in interface every method is public and let's say here I will print I will write paying via credit card ok very good Now I will create another class, let's say I name it debitCard. And it also implements the payment interface, and here I write paying via debitCard. Now how does it take advantage of polymorphism? In a way that I can write the payment object, let's say p, which is a reference variable, equal to new. Now you know that I can write creditCard and debitCard here. If I write creditCard and now I write p.new, Paying via credit card.
We just dynamically change this class. Right. Let's suppose we do it with any condition. And we call it p.pay. So, our payment method will be called. Whether it is of credit card or debit card. Right. So, this is a very good benefit. And if you think carefully. So, base provides the whole spring boot framework. So, when we talk about dependency injection in spring. So, actually this is what happens. Interfaces and abstract classes provide us the whole framework. Provide the whole base. Anyway, when we start the Spring Boot series, you will understand it better. And this is the whole base of clean architecture. That's why we can write more clean code in Java. Because we use dynamic polymorphism very well. Which overrides the method dynamically.
Now, look, this rule says that it is not necessary that you declare only methods in interfaces. You can also make variables in interfaces. Okay, what does it mean? Let's say I have an interface, math constant. Okay, so let's say I will give some values of math, some variables. So, if I want, I can define a variable in it. Let's say I wrote double by value. And its value would be 3.14. Okay, something like this. Now, why have I written everything in caps? You know we write in capital only if a variable is constant. Constant means it is final declared. But I haven't written final here. So whenever we declare a variable in an interface, by default, when it compiles from Java, it will put here, public static final. So, it will automatically connect it. We know that public connects to every method and every variable.
Because if you are declaring variables and if you don't make it static, then you know that it will be linked to an object. But we can't make an object of any interface, right? And the interface has nothing to do with the object. Now, Since the interface has nothing to do with the object, we can't declare a non-static member here.
Very good. So let's say I can do one more constant here. Let's say normal int value is equal to 10. So I can do this. If you look at it carefully. If I make a class tomorrow. Let's say random. And I make it implement. With this math constant. So you know that I can't change the value of this pi value but I can access it.
Fun because I didn't define fun inside the math constant. Fun function comes in a random class. Now what is r1? r1 is a reference variable of the math constant type. So we can call the same method on r1 which comes inside the math constant. So the method fun is undefined for type math constant. For that what I have to do is void we have to declare fun and this will define it so we have to make it public and again we write overwrite here as usual now in our math constant r1. 1 will work and we will get the same 3.14 output Now see, as we discussed that whatever variables we declare in any interface are by default public, static and final. So whenever we are working on any project, we will need such files in which we will want to make static and final variables or we will want to declare some constants. So in that case, if we make an interface without making a class, then our work will be very easy. Because you know how we can call these constants. We can also call directly through the class name. I mean, I don't even need to do all this. I could have simply done MathConstant.PiValue I could have called it like this And if I wanted I could have printed it on the screen So I could have written like this So I could have written like this And my MathConstant would have been printed on the screen There would have been no problem 3.14, 3.14.
1 And once it was simply called like this So imagine I have an interface MathConstant And it has all the constants And there is no function in it So let me comment it for now And this too Okay Let's suppose I have an interface named MathConstant and I have declared a lot of mathematical constants If I want to call it, I can call the interface name MathConstant dot that constant's name like this In fact, we use this principle a lot in production We make an interface in which only constant variables are defined and we can use those constants in our entire program You can also make it through a class. You can make it class math constant. But for that, you will have to call every variable static and final by itself. You will have to put public static final. What is better than this is that you make an interface and keep writing normal values in it.
Class A and I have a class B And suppose I have another class C So I can't write like this extents A, B This is not supported. We will get an error. Syntax error. Because it will not support A, B. It will say write A or write B. Now, why is multiple inheritance not supported in Java? We have talked about this. Because it comes from diamond problem. Now, what is diamond problem? We have seen it before. We will see it again in this video. But before that, multiple inheritance is supported through interfaces. So, imagine if we have an interface A instead of a class. And this is also an interface B. So, in this class, we can write, class C implements A, B.
Which I have just declared. And this interface has a method void fun2. I have declared it here. Now C will be responsible for overriding this fun method and fun2 method. And it can take both the functionalities. So let's say it will do public void fun1. Not fun1, it's just fun. I have declared this too. And at the rate override. The next method is public void.
You can inherit one interface from another interface in Java. So let's say if we have an interface, let's say animal. And there is a method inside it, let's say void eat. And we have another interface. Interface dog. And this dog interface wants to inherit this animal name interface. So what will be our keyword here? Again keyword will be extends. One interface extends the other interface.
So here we will write animal. And we have extended it. Why do we extend one interface to another? Because it can't implement it. We can't write implement keyword here. Implementing means that it has to provide the functionality of eat. But the work of an interface is not to provide the functionality of someone.
So, because this dog interface is not implementing this animal interface, and it is only extending it, means it is adding new functionalities. So, here we write the keyword extends. Now it can have its own method. Let's say bark. So void bark. Something like this. Now let's say I made a class and let's name it.
He implements the dog interface. So as soon as he implemented the dog. And the dog had already extended the animal. So the responsibility of the street dog is. To override eat and bark both. So see until the error doesn't go. Until I override both. So first let's say I public void. I will override eat. So, I will write system.out.println.
And, still there is no error because it will say the type street dog must implement the inherited abstract method dog.barg. That means, it will have to override the dog method. So, I will override this too. Override public void void Let's say bark. Something like this. And here instead of eating we will write let's say barking.
We don't have any error. So this is called interface inheritance. One interface like a dog can inherit another interface. And what is the keyword? In that case it is extends. Now we already know a lot about interfaces, but after Java 8, a lot has changed in the interfaces. And why did it change and what were the changes? Let's discuss about it once. So here I have made a file called demo5.
We are going to see what changes happened after Java 8 and how many interesting changes are there in the interfaces. So till now I had told you that if I define an interface, let's say I called it wickl. So, in that interface, and I will write it here first. After java 8. In that interface, we can only keep normal declarations of the method. So, here I can write void drive and I will have to leave it empty here. I cannot define it. And who is responsible for defining it? A class. Let's say class car which implements an interface. So, we have written here implements vehicle. And what will it do here? It will overwrite this. So it can write public void, void, let's say drive.
Because we know that the methods in the interface are by default public abstract. We don't need to write this, it automatically writes the compiler of Java. So even if we write it, there is no problem. And even if we don't write it, the compiler will write it automatically. So by default, all methods are public and abstract. So we just declare it and leave it. But after Java 8, Java has introduced a very interesting concept, which we call default methods. Default methods. That is, if I put a keyword, default. If I put a default keyword, now I can define this method. See, I didn't get any error. and let's say I define it as system.out.println and here I write let's say vehicle.
Let's say, I said vehicle v equal to new, let's say car. Because concrete class is our car. So I have written new car. Now when I will do v.drive. So which drive will be called? Obviously this one. Because car did not override drive. So our default drive will be called. Like in our abstract class case. So if I run it once. So vehicle is driving. And if you want you can override it. So our above method will be override. Let's say void drive. I have override it. And here obviously public will also come. Public void drive. And I have printed here, let's say, car is driving. Now in this case, which drive should be printed? In v.drive, car is driving or vehicle is driving? So what will be printed on our screen? Car is driving.
Let's first understand what is the need for Java to give default methods in interfaces. Because when we defined the interface, by principles, it came in such a way that the interface will only tell us what something can do. Basically, it will tell us about the contract without telling us how it can do it.
You know that Java has a list interface. When we read the collection framework of Java, our list type data structures like ArrayList and LinkList are defined. Now, in this list interface, Java had to introduce some new methods. But if Java would have introduced those methods directly, and what does it mean to introduce a method? Suppose Java has an interface called list, something like this. And Java has to introduce some methods in it. Let's say it has to introduce a new method, let's say pushback. I'm giving an example, it doesn't have any method, but let's say you have to introduce a method called pushback to Java. Now if it would have just declared the pushback, then all the projects that we have written in this world, all the classes that have implemented this list interface, those classes will be destroyed because their job will be to override this pushback method. Right? You can imagine that Java has directly pushed that I have put a new method in this list interface, pushback, and it has put this new feature in Java. Now when we take a new version of Java, then what will happen with us? If we have used the list interface, then Java will force us to overwrite this pushback method. Our production code will break. How many classes will we have to do changes in the world? So Java thought that if we don't have to do this, then I will make a new method which I call default. Default void pushback. And I give it a definition here. Now all the classes in this world. Which implement the list. They don't need to give this pushback definition. They can use its default method if they want. If they want to give their own implementation. Then they can override it. Like we had written drive here. In base class. But still we overrided it. But they don't have any necessity. This is not a hard and fast rule. That you have to override pushback. It became optional. This was the reason that. Java has introduced default methods. Let's remove this for now. Now you understand the reason. In fact, Java 8 doesn't only have default methods, but also static methods. Static methods. We can declare a static method in an interface. Let's create a static method here. static void break So we can also do something like this.
And let's say I simply print here. So here I can write, vehicle is applying brake. Right? So here I write, vehicle is applying brake. Something like this. So see, I can also keep my default method. I can keep a static method if I want. Now how can I call this static method? Now I don't even need an object for it.
Dot and simply I can call break like this. If I comment this and call vehicle.break, will my output come directly? And the answer is yes, it will come. So as we can call the static member of the class, how do we call? The name of the class is dot and the name of the static method. In the same way, we can call the static methods of this interface. Just like the name of our interface, dot is the name of our methods, vehicle.break, so this was also introduced by Java 8. So in Java 8, we made interfaces to a great extent, to a great extent, like abstract classes. That our normal methods can also come in it. Now in interface, we call it default method, but in abstract class, we call it normal or concrete methods. And not only that, after Java 9, this was after Java 8, from Java 8, 9. Private methods can also be used in our interfaces. So how to write private method? Let's say I can write private. So for the first time, I can write access modifier of a method in interfaces. Let's write private void. Let's say accelerate. So I can also make a method like this. Now the problem is that since this method is private, no one can call it outside this interface.
If you want another method inside this class, then you can call it. So let's say I have just printed here system.out.println and here I write vehicle is accelerating. Now who can call this? Let's say drive calls vehicleIsDriving first and then accelerate method calls. So we can call this from inside, no one can call accelerate method from outside. So now if I call v.drive, you know what will happen. First call is vehicleIsDriving, then accelerate method call and then vehicleIsAccelerating.
Now let's see. Our output should come. Vehicle is driving and vehicle is accelerating. Absolutely correct. Okay. I hope you must have understood a lot about interfaces. Now let's move on to our most important question. Java initially said that it doesn't support multiple inheritance. And then it said that it will support it through interfaces. So let's see why Java thought that it won't support multiple inheritance. Then how did it support it through interfaces? Let's understand this in depth. So let's first revisit the diamond problem. What was our diamond problem? Diamond problem used to say that let's say we have a class A, which extends two classes B and C. These are all classes. So now we are talking about classes and not interfaces. Now let's say I have a fourth class D and that D class inherits from both B and C. First of all, it can't do that because this is what multiple inheritance is. A D class is trying to inherit two classes. But we have to understand why Java can't do that.
It says that Let's say in this particular case, where I said that there is a parent class A, which is extended by two classes B and C. And there is a class D, which is extended further by B and C. If this was allowed, then let's assume that this A has a method fun. Fun, which it had defined, not just declared, but properly defined. So, through inheritance, this fun would have come here and here as well. Now, suppose, these two classes, B and C, override this fun. So, this will give some of its own implementation and this will give some of its own implementation and both will have different implementations. Now, because this D class extends both B and C, or inherits both of them, and let's say Java also has that support, then if I call fun method in this D class, that is, if I write something like this, first I write the object of D, D equal to new D, something like this, and then I write D dot fun. So which fun method would call? This fun method or the one coming from here?
This is what we call the diamond problem. Basically, a confusion is created that this D is extending two classes and both have the same method. So which method should I override? Now this was the initial design. Now as soon as Java invented interfaces, and Java said that interfaces are a very interesting thing. In interface, we didn't provide definition initially. So Java said that we can support multiple inheritance through interface. Okay, how can we do this? Let's first look at the same hierarchy in our interface. A is a base class. We have two child classes B and C.
Now let's assume that A, B, C. A, B and C. All three have interfaces. are interfaces. If this was the case, then what would have happened? Let's talk about old Java before Java 8, when there were no default methods. So if these are the three interfaces, then what should happen in all three? This function method should only have a declaration. Now if this is only a declaration, then B and C themselves Interfaces. Meaning that B is doing echo Extend and C is also doing echo extend And if because they both are just Extending it then they won't provide Implementation of function. So they also have What will happen of function. Only declaration Will happen. Definition will not happen In fact if we want here Then there is no need to write fun here Because by default in B and C Fun method will be coming from A. We write this when it tries to overwrite something. But we can't overwrite C and B. Because we don't give implementation in Java. We are talking about old Java. So in that case, D is our proper class. Now our class D, which implements two interfaces B and C. But no diamond problem will arise here, no confusion will arise. Why? Because D is a class and B and C are interfaces. So D's responsibility is to provide implementation of fun.
Because here also fun is declared. So D is responsible for providing the definition of this function. And as soon as it provides the definition, the confusion is gone. Now when I write dd equal to new d and now I will do d.funcall, then Java compiler will know that we have to do this funcall which D has overrided. Hence our diamond problem is completely erased. For this reason, we break the diamond problem through interfaces in Java. That's why our multiple inheritance is supported. So again, I have made a file in demo6.
If we look at the same thing in code, let's say we have an interface A. There is a method void fun which it has just declared. And we have an interface B which extends the A interface. And in the same way, we have an interface C which extends this A interface. Now we have a concrete class D which implements B, C in this way. We can do this in Java. Now our responsibility is to override this. We write public void fun to this fun method.
Now you will say that we have not written fun method here. No problem, it will come from here. If you want, you can add here too. Let's say here is our own fun method and here is our own fun method. Something like this. Okay, so I wrote fun method in both. But here as soon as it overrides fun, all our error is gone. Now as soon as it provides any implementation, So let's say it wrote default implementation.
Now whenever I make this d object, let's say dd equal to new d. And now I'll call d.fun. So Java doesn't have any confusion. Java will call this function directly. How will it call? If I run it, then our method is default implementation. Right? Our entire diamond problem gets eradicated by using interfaces. But now let's talk about Java 8. After Java 8, we can write default methods. So let's assume that I have written a default method in this B class, fun. And I have provided some implementation of it. Let's say system.out.println And here let's say we call just a. And similarly, I have made a default method in interface C too. Same method in the name of fun. Let's print the class name which is C. So here I should print b not c. So here I printed b and here I printed c. Now see, as soon as my class d implements these two interfaces, what will happen now? Now fun is coming from here and here too. Now how will you break the diamond problem?
If I know that my class d is implementing two interfaces, b and c, which have the same function, both of which have the function fun, then I will make a rule. The rule is that If such a situation comes, then it is the responsibility of class D to override this fun method. So basically I have to override this fun method. So I have to write public void fun. Only then it will work, otherwise I will keep getting errors. Now I can print anything here. Let's say I printed my D. Now I have created an object of d and I will do d.fun and my answer should be d. So I can check once and my answer is d. So java has made a rule that if you are implementing two interfaces and both of them have a default method then which method should be called? You have to overwrite your own implementation. But suppose I want to call the method of B or C. I don't want to write my own function. For now, let's assume I want to call the method of B. So, I want to print B on the screen. So, basically, I want this function to call. Right?
I didn't let Java get confused. I told it specifically to call the fun of B and then C. And as soon as I run it, my B and C will get printed. So basically again, if there is a case of diamond problem, then it will be the responsibility of this class D to override the fun. Because you know what happens in a normal case? If this class D implements only one interface, let's say B. So if it implements only B and if it doesn't override it, then also.
Because our interface B has given us its own fun method. So we don't need to implement or override this D. But because it's doing both, B and C, now Java's compiler is confused that if someone makes an object of this D, let's say like this, D.funcall, then which funcall should I do? Should I do this funcall or this funcall? This is the rule to solve this confusion. As you hover over it, you will get the same error. Duplicate default methods named fun with the parameter empty and empty are inherited from the type C and B. It will tell you that two methods are coming and I am getting confused. Quickly override it and solve the confusion. Now see, I have made a file called demo7 because I want to explain you one more thing which is very interesting. Which is Java's resolution priority rule.
Java's resolution priority rule. Now see, what does Java's resolution priority rule say? This is very interesting. It says, let's say I have an interface A. Okay, an interface A. Now let's put a method in it. And let's say I have a class B. Something like this. And imagine I have another class C. Which extends our B class and implements our A interface.
There will be no error. In Java, I can extend A class. But how much interface can I implement? So I chose to extend B class and implement A class. There is no problem till now. But let's assume I gave a default method in this interface A. So default void fun. Something like this. It prints something on the screen. System.out.println And it writes inside A interface.
Now assume that my class B also has the same method. So let's say it also has the method public void fun. Something like this. Right? Now, obviously, in the interface, by default, the method is public, so there is no need to write that. So, here I have written public void fun. And let's say it prints on this screen, inside B class. Something like this. Now, you think, has this become a diamond problem again? Now, on the object of this C class, when I do c.fun, then our Java compiler will call this fun or this fun. In the previous file, we saw that to break this, we had to override the fun. In that case, we had an interface B and C, both of which had a fun method. And if a class is D, which implements both the interfaces, then it will have to overwrite this fun method because Java will be confused whether to call this fun or that one. If someone made an object of D, let's say like this, So Java has to be clear about which function to call. So Java has made a rule that D has to override the fun. And D will tell what to call. Its own implementation. Or D can say call fun of B or C using super keyword. But in this case, we are not getting any error. We are not getting any error. That means compiler is not forcing us to override this fun. If it is not forcing me, then I should get some output. So let's see what happens. And my output is inside B class, which means the class method is called automatically.
This is called Java Resolution Priority Rule. Java says that if there is a case where a class implements an interface and extends a class, and both have the same method, then we will always give priority to the class function. The class function will always be called automatically. If you don't want this to happen, then you can override it yourself. So you have written public void fun. Something like this. And here it provides its own implementation. Let's say inside C class. Now when I will run, then my output should come inside C class.
I hope you have understood all the interfaces Now let's answer a very interesting question Which is also an interview specific question Before Java 8, things were very simple That there can't be any default methods in the interface Which we implement It can be a normal method, which we declare But after Java 8, we can have default methods in the interface In fact, we can have static methods Static methods In fact, if I write java 9 here, then we can also have private methods.
It can have static methods, private methods, and abstract methods. You know the abstract method is normal. By default, it is abstract if we declare it. So basically, there was a very small gap between interface and abstract class. Let's see if there is any gap between these two. The biggest gap is of their intention.
Or to show functionalities. So, what can any class do? That's why you see, generally practice is that we give the name of interface like a verb. So, for example, let's take runnable. Walkable The thing that can be done So if I make an interface of payment Then the better name will be payable Something that can be done So if a class implements runable interface Then I will understand that In that class there are runable functionalities Or if it implements walkable Then there are walkable functionalities If it implements payable Then there are payable functionalities So what is any class So for example if I write class A And here I write implements And next I will write the name runable So, class A has become runnable. If I write here, walkable, then it has become walkable. So, this tells us roles or responsibilities or functionalities. But on the other hand, abstract class tells you families of similar class.
I will define that in animal. So they won't need to define their own eat. Something like this. So the first thing is the difference in their intention. The intention of interface is functionalities. And what is the intention of abstract class? The intention of abstract class is families of similar class. In fact, that's why we call interface normal.
But in interfaces, we generally call it can-do relationship. Can-do relationship. For example, if I say that there is a class A, let's say, it has implemented runnable interface so it is similar to say that A can do run Basically, A has runnable functionality. If it has done to walkable, then A has walkable functionality. So, what functionalities does it have? What it can do? That is what our interface tells us. And families, that is, what is something or where does it belong? That is what our abstract class tells us. Like, dog is an animal. So, dog and animal came into a family. This was their conceptual difference. Now, what are the syntax differences in Java?
Here we have our interfaces and here we have our abstract classes. You know, No matter how similar the interfaces are, there can't be normal fields in the interface. Normal fields means we can't define a variable that is static and final. We can't define a variable that is instance variable. But normal fields can be found in the abstract class. So if we have an abstract class A, I have declared it abstract. In this way, I can declare normal int x, no problem. But in any interface, if I have an interface A, if I declare any value in it, let's say I declare int x here, then you know that Java by default puts public static final in it. That means my x will become final. But this is not a compulsion in the abstract class. So, this is your first difference.
Okay, there is some other change. We haven't talked about the biggest change yet. With interface, we can support multiple inheritance. Multiple inheritance is supported. But here our multiple inheritance is not supported. We can't support abstract classes with multiple inheritance. Right? And another rule can come in which we can say that all the methods we have in interfaces are by default public If you want you can make private methods after java 9 but generally by default they are public But in abstract class you can put any access modifier, you can put private, you can put public You can use protected or any other interface.
Are going to see two very special interfaces. One is called functional interfaces. Functional interfaces. First of all, what are these? These are the interfaces in which there is only one method and that method is declared. Who has the responsibility to define it? The one who will implement this interface. For example, if I make such an interface, not class. I made an interface, let's say A. And I put the same method in it, void. fun and declare it and leave it now because it has only one method so I will call this type of interface as functional interface now any class let's say I have made a class B which implements this so implements A now the responsibility of this class will be to override this fun Something like this. Now you will ask, why is there a special name for it? Functional Interface. Now there are many methods, there should be only one. But its special name is because the functional interface unlocks a very important concept in Java. Which we call functional programming. And how do we unlock it? Using lambda expressions. We will study this in a separate video. What are lambda expressions? And what is functional programming in general? But this is a very interesting topic. Which came latest in Java. Basically it didn't exist before. It came after Java 8. Which is very interesting. So we will see this in a separate video. But you understood. An interface in which only one method is declared. That is called functional interface. In fact in our Java. Originally many functional interfaces already exist. If I give it some name. So we have one comparable interface. This is a functional interface.
What will we call this interface in Java? We will call this marker interfaces. So, these are marker interfaces. Okay. There are three types of marker interfaces in Java. One is called InterfaceClonable. This is a marker. There is no method defined in it. The other one is called InterfaceSerializable. This is also a marker. There is no method defined in it. And the third one is called InterfaceRandomAxis. And again, this is also our marker interface, which means that no method is defined.
Any interface in which no method is implemented, where can we take advantage of it? To mark anything. For example, a clonable interface. Let's see this. So, we know that the parent class, object class, which is parent of every class in Java, has a method called clone. And we say that any class which has to override this clone method, has to implement a clonable interface. So, let's say, if I tell it like this, that I have an object class, this is the root of every parent class, and it has a method called clone. And I made my own class, let's say student class. And I have to override this clone method in the student class.
Because Java doesn't want everything to be cloned. Because there are many critical resources that should not be cloned. Like database resource, etc. Or threads resource. Now because Java knows that everyone cannot be cloned. So clone should be done to them only. Which you will explicitly tell that I want to clone this. So you will literally write like this. Class student implements clonable. What does ImplementsClonable mean? You have specifically told Java that I want to clone this class or give it the functionality of clone so that I can override the clone method. Something like this. Now there is no method in clonable. So here you don't need to override any method of clonable. You just had to write ImplementsClonable so that Java can check that if you have written ImplementsClonable then only I will make your student class clonable or let the clone method override in it. If you don't write like this, you will get a runtime error. I hope you have understood the marker interfaces. I said that there are three types of marker interfaces in Java. That is our clonable, our serializable and our random access. Now see what happens when you define an interface in Java. So let's say you have written an animal interface in Java. Something like this. And obviously you can write some methods in it. So I will write a method void run. And I declare it and leave it So this became our interface Now whenever you define an interface animal And when you compile the java code So let's say you write javac And let's say the file name is animal So you write animal.java So you know what happens, a bytecode is created So this class will convert to animal.classfile Animal.classfile Something like this If you go inside this classfile So, Java would have treated this animal as a class. So, basically, even if we have written an interface here, Java calls this animal class class only. Right?
If you go inside the animal class file, you will get a special tag. So, for example, an animal class would have been made, which obviously it would have made by itself. And inside that, you will get a tag, ACC interface. Something like this. ACC underscore interface. This tag always tells Java that this class is not a normal class but an interface. But from inside Java treats it like an interface. Like I told you earlier, enum also becomes a class by going inside.
Similarly, interface also becomes a class by compiling inside Java. It has given us a special keyword named interface. Everything inside is executed like classes. I just told you that there is a small difference. The interface, let's say animal, when it makes its class, it will put a tag named acc.interface inside. So that Java knows that this is an interface and not a class. Everything else is implemented from inside like a class. Right? I hope you understood well. You must have understood better about interfaces. That's all in this video. See you in the next video. Till then, thank you so much.
