Podcast Lookup

Java Interfaces Explained | Default Methods, Functional Interfaces & Marker Interfaces

The Java Developers Hub

  • December 3, 2026
  • 60 min
  • Episode 33
Episode page
About this episodeInterfaces are a fundamental part of Java's object-oriented programming model. They allow developers to define contracts that classes can…
Interfaces are a fundamental part of Java's object-oriented programming model. They allow developers to define contracts that classes can implement, promoting abstraction, flexibility, loose coupling, and reusable application architecture.In this episode, we take a deep dive into Java Interfaces, exploring how they work and the different types of interfaces available in modern Java.You'll learn about default methods, functional interfaces, and marker interfaces, including how they support abstraction and functional programming. We'll also examine practical examples to help you understand when and why to use each interface type.Whether you're learning advanced Java concepts or preparing for professional software development, this tutorial will strengthen your understanding of interface-based programming.What You'll Learn
  • 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 publisher

This transcript comes from the podcast’s own feed, shown as the publisher wrote it.

Java Interfaces Explained | Default Methods, Functional Interfaces & Marker Interfaces

00:00—
  1. Speaker 1

    Yes, so let's start.

  2. Speaker 2

    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.

  3. Speaker 1

    Okay.

  4. Speaker 2

    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?

  5. Speaker 1

    Okay, look.

  6. Speaker 2

    Interface tells us what can be done without telling us how to do it.

  7. Speaker 1

    Right?

  8. Speaker 2

    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.

  9. Speaker 1

    We just declare.

  10. Speaker 2

    Now, whenever we try to link it with a class, we make a big mistake. Let's say I have written a normal class car. Okay? In this class, let's say there is a method drive. Since this is a normal class and not an abstract class, I'll have to provide the implementation of drive.

  11. Speaker 1

    So, let's say, do something. Okay, very good.

  12. Speaker 2

    Now, if I write the interface of car against this, how do I write it? I write our interface. Let's keep its name as car. And since this is an interface, I provide its declaration here.

  13. Speaker 1

    Void drive. Something like this. Okay.

  14. Speaker 2

    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.

  15. Speaker 1

    Is a contract. It defines a contract.

  16. Speaker 2

    So the contract here is that any class that implements me will have the responsibility to provide the definition of every method.

  17. Speaker 1

    In case any class is not.

  18. Speaker 2

    Able to provide the definition of every method, then we have to make it an abstract class.

  19. Speaker 1

    Now let's do this one by one.

  20. Speaker 2

    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.

  21. Speaker 1

    Okay, very good.

  22. Speaker 2

    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.

  23. Speaker 1

    Very good.

  24. Speaker 2

    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.

  25. Speaker 1

    Explicitly apply it.

  26. Speaker 2

    So if it is public, then when we have to override it, then only it will have to write public.

  27. Speaker 1

    Why is that? You can imagine.

  28. Speaker 2

    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.

  29. Speaker 1

    So basically, this.

  30. Speaker 2

    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?

  31. Speaker 1

    What I generally do? Let's make an object of car, let's say carC equal to new.

  32. Speaker 2

    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.

  33. Speaker 1

    So what will we have.

  34. Speaker 2

    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.

  35. Speaker 1

    And if it.

  36. Speaker 2

    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.

  37. Speaker 1

    Okay, what is that?

  38. Speaker 2

    Look, let's suppose we remove this interface for now and we take a new interface here.

  39. Speaker 1

    Let's say payment.

  40. Speaker 2

    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.

  41. Speaker 1

    Paying via debit card. will come. So now let's run it once. Paying via credit card. Okay, very good.

  42. Speaker 2

    Now if you look carefully, then the best benefit of this is that we don't have to change anything in the code. We can write a credit card dynamically here or dynamically we can change it later and make.

  43. Speaker 1

    A debit card.

  44. Speaker 2

    And as soon as we make a debit card, our code will run completely.

  45. Speaker 1

    Paying via debit card. See. We don't need to change any code.

  46. Speaker 2

    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.

  47. Speaker 1

    Okay, very good.

  48. Speaker 2

    Now see, again I have made an empty file by doing demo2. Because we have to talk about a second rule. Which is variables inside interfaces. Okay.

  49. Speaker 1

    Now, what does this rule say?

  50. Speaker 2

    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.

  51. Speaker 1

    Now, why static?

  52. Speaker 2

    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.

  53. Speaker 1

    That means we have to call it static. Now, why final?

  54. Speaker 2

    Because according to the definition of interfaces, you can't keep any variable in the interface which you can change later.

  55. Speaker 1

    So you can keep constants only. And what are constants? Static constants.

  56. Speaker 2

    So whatever double pi value we write in the interface, we don't need to write public static final. This compiler will put our java itself. Means every value here will be declared as constant.

  57. Speaker 1

    Okay.

  58. Speaker 2

    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.

  59. Speaker 1

    So let's assume that I have a method void fun. So this fun method can access.

  60. Speaker 2

    This pi value. Like system.out.println I can write pi value here. So here my pi value will be printed because random is implementing the math constant and from there my pi value comes. Now if I put an object of random, let's say r1 equal to new random.

  61. Speaker 1

    And now I call r1. fun.

  62. Speaker 2

    So what will be printed on my screen?

  63. Speaker 1

    On my screen pi value i.e. 3.14 will be printed. But can I write math constant instead of random?

  64. Speaker 2

    The parent class reference variable can refer to the child class object.

  65. Speaker 1

    But here I got an error in r1.

  66. Speaker 2

    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.

  67. Speaker 1

    Once it was called because of R1.

  68. Speaker 2

    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.

  69. Speaker 1

    They will automatically become public static final.

  70. Speaker 2

    Right? Now, the second most important principle of the interface is that because of that, multiple inheritance is supported in Java. Right?

  71. Speaker 1

    So, let's see that.

  72. Speaker 2

    I have created a file called demo3. java Now we are going to see about multiple inheritance Now obviously we already know that in java normally multiple inheritance is not supported through classes How?

  73. Speaker 1

    Let's say I have a.

  74. Speaker 2

    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.

  75. Speaker 1

    So, this is fine. This code is absolutely fine.

  76. Speaker 2

    That means, we can implement two interfaces at once. In fact, no matter how many are there, we can implement one class with multiple interfaces. But, one class can only extend one class.

  77. Speaker 1

    We know that.

  78. Speaker 2

    Now, what is the benefit of this? The benefit of this is, let's say, there is a method in this interface, void fun.

  79. Speaker 1

    Okay.

  80. Speaker 2

    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.

  81. Speaker 1

    Fun2.

  82. Speaker 2

    Something like this and it also overwrites it. Now see this code is absolutely fine. Okay, so now let's see one more thing for which I have again made a file named demo4.

  83. Speaker 1

    Java. In this we have to see interface inheritance.

  84. Speaker 2

    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.

  85. Speaker 1

    Okay.

  86. Speaker 2

    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.

  87. Speaker 1

    Its work is only of declaration.

  88. Speaker 2

    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.

  89. Speaker 1

    Dog. Street dog. Okay. And what does he do?

  90. Speaker 2

    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.

  91. Speaker 1

    Eating. Let's write simple eating. Okay.

  92. Speaker 2

    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.

  93. Speaker 1

    Okay.

  94. Speaker 2

    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.

  95. Speaker 1

    Java.

  96. Speaker 2

    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.

  97. Speaker 1

    Something like this. This is possible.

  98. Speaker 2

    But you can't give the definition of the class in this interface. Meaning I can't do this. I will get an error. Abstract method do not specify a body.

  99. Speaker 1

    Okay. Why? Why can't we give any method's declaration?

  100. Speaker 2

    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.

  101. Speaker 1

    Is driving So, I wrote something, vehicle is driving. And see, I didn't get any error.

  102. Speaker 2

    Now, even if this car is not overriding it, it will still work. There will be no error because the drive method has already been written by the vehicle class. Its definition has already been told there.

  103. Speaker 1

    How? By a keyword default.

  104. Speaker 2

    And this default keyword tells the Java compiler that we are going to give a method which we will define in its interface. Now, you see, how can I call this drive method?

  105. Speaker 1

    Very simple. First, I will make an object of this vehicle.

  106. Speaker 2

    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.

  107. Speaker 1

    Why?

  108. Speaker 2

    Because we know that whenever a child class implements a parent class function, it always gets priority and shadows the above function.

  109. Speaker 1

    Okay, very good.

  110. Speaker 2

    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.

  111. Speaker 1

    So by default, we can only write normal methods in interfaces.

  112. Speaker 2

    And it will be the responsibility of the class to complete that contract. Contract means to define all those methods. So, after Java 8, what did Java think? It thought that it should also include default methods in Java.

  113. Speaker 1

    Now, this story is very interesting.

  114. Speaker 2

    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.

  115. Speaker 1

    See, there is no error.

  116. Speaker 2

    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.

  117. Speaker 1

    What can I do directly?

  118. Speaker 2

    The name of our interface, which is vehicle.

  119. Speaker 1

    Sorry. Vehicle.

  120. Speaker 2

    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.

  121. Speaker 1

    So who can call it?

  122. Speaker 2

    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.

  123. Speaker 1

    These two outputs will come. Let's see. Car is driving. Okay.

  124. Speaker 2

    It came because we have overrided this drive method. So I will remove the override method.

  125. Speaker 1

    Okay.

  126. Speaker 2

    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.

  127. Speaker 1

    Why did Java refuse?

  128. Speaker 2

    Look, the initial designers of Java like James Gosling or his team, James Gosling, They saw that because of this multiple inheritance, a diamond problem is introduced. Now, many languages find solutions to this diamond problem in their own way.

  129. Speaker 1

    Like CC + + did.

  130. Speaker 2

    But James Gosling and his team thought that the solution to this diamond problem is very complicated. And it's not worth it to solve it. It's better to make a strict rule that multiple inheritance won't be supported in Java.

  131. Speaker 1

    Okay. Now, what does diamond problem say?

  132. Speaker 2

    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?

  133. Speaker 1

    Right?

  134. Speaker 2

    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.

  135. Speaker 1

    And both of them have a common class D. Okay?

  136. Speaker 2

    We were talking about this problem till now. And we were saying that let's say A has a method fun. B has overrided it. And C has overrided it too. So which method should D have?

  137. Speaker 1

    This was the whole hierarchy.

  138. Speaker 2

    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.

  139. Speaker 1

    Right?

  140. Speaker 2

    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.

  141. Speaker 1

    Java.

  142. Speaker 2

    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.

  143. Speaker 1

    And overwrite it from here.

  144. Speaker 2

    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.

  145. Speaker 1

    So something like this.

  146. Speaker 2

    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?

  147. Speaker 1

    So Java has said that it is very simple, right?

  148. Speaker 2

    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?

  149. Speaker 1

    Neither C nor I want to give anything of my own. So, what will I do? It's very simple. I will override.

  150. Speaker 2

    And what will I do? I will call the fun of B. And how can I do that? It's very simple. I will write the name of my interface as b. And then again what is the keyword we have?

  151. Speaker 1

    Super. So b.super.fun. Something like this. As soon as I wrote b.super.fun.

  152. Speaker 2

    What does b mean? The name of the interface. Dot is our super keyword. And dot is the name of the function so this function will be called and it will also be printed on the screen so now if I run this Right.

  153. Speaker 1

    So basically I did override. But what did I do in override? I told myself from inside. That call the method of B.

  154. Speaker 2

    So I didn't leave any confusion to Java's compiler. What was the job of Java's compiler? To execute this method. As soon as he went to execute. I told him to execute fun of B. So he went to execute fun of B. I told him explicitly.

  155. Speaker 1

    I didn't tell him to look at C.

  156. Speaker 2

    Now if I would have written C dot super dot fun. Then he would have simply called method of C. So my output would have been C. In fact, if I wanted, I would first call the super fun of B and then call the super fun of C. Something like this.

  157. Speaker 1

    My choice.

  158. Speaker 2

    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.

  159. Speaker 1

    Our work would have been done. Why would it work?

  160. Speaker 2

    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.

  161. Speaker 1

    Priority rule. Okay, what is this?

  162. Speaker 2

    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.

  163. Speaker 1

    Can I do this? Yes, you can.

  164. Speaker 2

    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.

  165. Speaker 1

    Something like this.

  166. Speaker 2

    Now see, as soon as I gave this default method, this will also come in our class C. Now if I make an object of C, then if I write like this, C small c equal to new t, then I can fun call on this C small c.

  167. Speaker 1

    And as soon as I fun call, the interface should come inside A. It came.

  168. Speaker 2

    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.

  169. Speaker 1

    Why?

  170. Speaker 2

    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.

  171. Speaker 1

    Okay.

  172. Speaker 2

    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.

  173. Speaker 1

    What else can happen in the interface? There can be static and final variables in it.

  174. Speaker 2

    If this much can happen, then how can we call the interface abstract? Now, it is not almost the same as the abstract class. Because in that too, we can have concrete method instead of default method. Which we call concrete method.

  175. Speaker 1

    Right?

  176. Speaker 2

    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.

  177. Speaker 1

    Intention means. At one place I write interface and at another place I write abstract class.

  178. Speaker 2

    Now imagine Why do you make interfaces? We make interfaces to show any contract.

  179. Speaker 1

    Right?

  180. Speaker 2

    Always remember to show contract. To show what something can do. What functionalities can be there in it. So, I will give a different definition.

  181. Speaker 1

    To show roles.

  182. Speaker 2

    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.

  183. Speaker 1

    What does it mean? It's very simple.

  184. Speaker 2

    If you know that you have multiple classes and you can keep all of them in a family, in a combined parent, then you use Webstack class. So let's say you had this dog, you had this duck, you had this.

  185. Speaker 1

    Let's say it was an elephant or anything. So you thought that.

  186. Speaker 2

    They are all animals. So let's do one thing. Let's make a root-level class animal. And we will keep all of them inside it. What will we get from this? If there are similar methods which are common in all of them.

  187. Speaker 1

    Like eat.

  188. Speaker 2

    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.

  189. Speaker 1

    Is a relationship. Is a relationship. Do you remember I told you? Inheritance is generally called a relationship.

  190. Speaker 2

    Like if we call it dog, then we will say dog is an animal.

  191. Speaker 1

    Duck is an animal. Elephant is an animal. It shows a relationship.

  192. Speaker 2

    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?

  193. Speaker 1

    Let's look at that.

  194. Speaker 2

    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.

  195. Speaker 1

    Okay. What is the second difference?

  196. Speaker 2

    The second difference which is more interesting is that there can't be constructors in our interfaces.

  197. Speaker 1

    They can't be there even now. Right. Why can't they be there again?

  198. Speaker 2

    Because you know what a constructor means. Constructor means that it is linked to an object.

  199. Speaker 1

    Right.

  200. Speaker 2

    And as I told you, there is no connection between interfaces and objects. It tells you what a class can do.

  201. Speaker 1

    It has to be connected to its responsibilities.

  202. Speaker 2

    But on the other hand, you know, in our abstract classes, There can be constructors in abstract classes.

  203. Speaker 1

    Right?

  204. Speaker 2

    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.

  205. Speaker 1

    All access modifiers are allowed. Now we.

  206. Speaker 2

    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.

  207. Speaker 1

    Okay.

  208. Speaker 2

    One is our predicate interface. This is also a functional interface. So we already have a lot of functional interfaces in Java. When we read lambda expressions and functional programming, we will understand.

  209. Speaker 1

    Them well in that lecture.

  210. Speaker 2

    Now other than functional interfaces, we have another type of interfaces in Java, which we call marker interfaces.

  211. Speaker 1

    Marker interfaces. Now, this is also very interesting. What is this? These are such interfaces in which there is no method.

  212. Speaker 2

    So, if you make an interface, A, and leave.

  213. Speaker 1

    It empty as it is. No method is needed. No method.

  214. Speaker 2

    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.

  215. Speaker 1

    Now you think, why is there a marker interface?

  216. Speaker 2

    Why does an interface exist in which no method is defined?

  217. Speaker 1

    It's a very simple rule.

  218. Speaker 2

    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.

  219. Speaker 1

    Something like this.

  220. Speaker 2

    So I can do this only if I write in my student class. If I write in my student class implements implements cloneable Something like this.

  221. Speaker 1

    Only then Java will let me do this. What is the reason for this? There can be many reasons.

  222. Speaker 2

    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?

  223. Speaker 1

    So, how does it make a difference between these two?

  224. Speaker 2

    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.

  225. Speaker 1

    Just Java has given us a special keyword.

  226. Speaker 2

    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.