Java Interface Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys Sam here today we're gonna be learning about Java interfaces now interfaces are one of the fundamentals of Java and if you want to be a proficient Java developer you need to understand how they work in this video we're gonna be going from the basics of interfaces all the way to the ins and outs so by the end you guys should all be pros I'm really excited about this video so hopefully you guys are too now in order for you to understand how interfaces work you do need to know what a classes and how inheritance works so I recommend you learn that if you need to and then come back to this video in object-oriented programming sometimes you want to define what a class does but not how it does it if you've worked with abstract methods you know that they give you the signature for the method but not the definition of it it's up to the subclass to provide the implementation of it interfaces take this a step further and they apply it to the entire class so you can fully separate a class interface from its implementation this is done by using the keyword you guessed it interface once an interface is defined any number of classes can implement it also unlike inheritance a class can implement any number of interfaces it likes we'll be looking at examples of that later on alright so enough with the theory talk let's look at a concrete example alright so here we have an empty Java project let's go ahead and create a new interface now I'm using IntelliJ and you can go to file new Java class and you just switch this to interface and let's give it a name like series and we're gonna give it one method called get next now classes that implement the series interface are gonna have a value at 0 and every time this get next method is called it's going to increment the value by a certain amount so let's first create a class that increments the amount by 2 so we've got a file new Java class let's go ahead and call this by twos let's go ahead and give it our value here let's go ahead and create a constructor that initializes the value to zero and now in order for this by two's class to implement series we need to go up between the class name and the first curly brace and we type in implements and then the name of the interface notice that we get an error here saying class by twos must either be declared abstract or implement abstract method get' next so whenever you implement a interface you have to implement all of the methods that it provides so to do this we would just do public int get next and in here we're just going to increment it by two and then return the value and as we see the error went away now before we move on I just want to say that as we mentioned we can implement more than one interface so how do we do that as we just have a comma and say we had you know another interface called series two we don't have that yet so that's why it's showing an error but this is how we would implement one or more classes and then in here we would also have to implement any any method that series two has we would also be able to extend the class as well again this isn't defined yet so we're getting that error but we could also extend classes as well as implement them at the same time now let's hop over to main and let's create an instance of the by twos class let's go ahead and create a for loop that calls the get next method five times so we would simply just say by twos dot get next and if we go ahead and run that we see that it prints out two four six eight ten which is what we were expecting now as I mentioned we can have as many classes as we want implement an interface so let's go ahead and create another class that increments that value by three so again we go to file new Java class we'll call it by threes and you know what let's just go ahead and go to the by two's class and then copy everything over and then let's go ahead and change all the names two by threes and then we also increment by three now instead of two all right let's go back to main and create a by threes object and then let's go ahead and run that and we see down here we now get three six nine twelve and fifteen so as we see here the by twos and by threes both have this get next method but as we mentioned the interface tells it what needs to be implemented but the class can decide how it gets implemented all right so next we're going to talk about interface reference variables something really cool is that you can create a variable of the interface type and you can have it reference objects that implement that interface so if we go back to our example here we can actually go ahead and have a series object and we can point this series object to either of these classes and we can call the get next method and it will call whichever objects version it's pointing to so for example we can go down here to this loop and say OB equals by twos and now we can go ahead and call OB get next but we can also change it to point to the by 3's object so after it prints it out we can have it switch over to point to the by three's method and it'll call get next as well and let's go ahead and write something so we know which one it's calling so let's go ahead and run that and as we can see here it's alternating between which method it's calling which is what we expected so if we go back here this Obi is smart enough to know that hey even though I'm calling the get next method on both of these I'm going to look at which object I'm pointing to and I'm gonna call that object's version of get next so that's pretty awesome one thing to note is that it can only call methods of what's actually defined in the interface so if we go back two by twos and say let's create another method here so we have another method here that decrement Stu from Val and if we go back and we try to call that we see that we get an error here saying cannot resolve method get' pre vin series that's because it only knows about the methods that are in there aren't in its interface if it's defined within the scope of a class it doesn't know about it hence it can't call it alright so let's go ahead and delete some of this stuff here the next thing we're going to talk about is variables and interfaces so variables can be declared in an interface so if we go back to series here let's go ahead and create something that that caps out how high we can go so let's say we only want this to go up to the value 10 so what we could do here is we can say int max equals 10 and then let's also have a string here [Music] now let's go ahead and modify our getnext method so what we're saying here is if value is greater than or equal to max' we're going to go ahead and print out that error message otherwise we're gonna go ahead and add two to Val and then we're just going to return the value and notice that the max and error message are not defined anywhere within this class but since they are in the series interface our by two's class has access to it so if we go back to main let's go ahead and change this so it actually loops through six times and let's go ahead and run that and we see down here it gets to ten which is right but then once we try to add it again it says cannot go above value ten and it prints out that same value of ten one thing to note is that all variables declared in interfaces are public static and final they're public so that any class that implements it has access to the variable they're also static so if we wanted to actually call it without using an object we could simply go series dot max let's go ahead and comment this other print statement out and run that so we see that it prints out ten even though we didn't create an object or anything we can simply do series dot Max and that's because it's static and it's also final meaning that we can't change the value of it another thing to note is that interfaces can be extended so let's go ahead and create another interface and let's call it series sub so let's go ahead and have this get previous method defined in this interface and we also need to have it extend series so now if we go two by twos and we say series sub so as we see here it implements series sub so it has that get previous method so if we actually comment it out here we would get an error saying that it needs to implement get prevalent that but we see that we still have access to things like Max and error message which are in the series interface they're not in series sub right because that's all that's in there is get previous but since series sub extends series we get access to all the stuff in here as well so let's go ahead and change this back to series the next thing that we're going to talk about our default methods so you can actually define the body of a method inside of an interface so if we go back to series and let's go ahead and create another method so let's implement a method called print' stuff and let's have it say something like live from New York it's Saturday night I am filming this on a Saturday so it is appropriate although I'm not in New York now we see we're getting an error here and interface abstract methods cannot have body which is true right they can't have a body however we have to add this keyword default letting you know this is a default method so we are we you know we let it explicitly know that it needs to have a body and we see that the error message goes away all right so now if we go back let's simply have something like by twos and then we see it has access to that print stuff method so if we hit run we see that it prints that out and even though by twos does not implement print stuff it still has access to it because it's implemented within the series interface however if we did want to we could override the print stuff method so if we go to our by twos class we'll say like I am by twos class so now if we print that out we see that it prints the by twos version of it so it overrides the default method in the interface definition all right so I'm going to go ahead and delete that now if we go back to series the next thing we're going to talk about our static methods in interfaces so say we'll keep this print stuff class and instead of making this a default we're gonna make it a static again as we saw with this Max variable we can call things that are static without actually creating an object of the class that's implementing this interface so if we go back to main so now if we go back we see that we we get an error here saying static method may be invoked on containing interface class only so what we would have to do is let's just go out and delete everything and we could simply do series dot print stuff we get no errors we run it and boom we get what we expected so the last thing I want to talk about is private methods within interfaces now this was introduced in Java 9 and you can now include private methods alright so let's go back to our interface and let's change this back to default and let's go ahead and create another another method and let's make it private let's go ahead and call it print more stuff and let's go ahead and print more stuff now what we can do is we can just go to print stuff and we can call print more stuff so now if we go back to main let's go ahead and delete this and create another by twos object let's call print stuff let's run it we see that it prints live from New York it's Saturday night and also prints that private method that says more stuff alright guys so that should be everything and probably more than you need to get started with interfaces and obviously this wasn't like a real-world example that I was using but I wanted to keep things simple just so we can focus on the fundamentals and the concepts of interfaces so hopefully you guys learned a lot from that video I know there was a lot packed in there but interfaces are something that you're gonna be running into a lot in Java and you really need to know them front and back so yeah if you guys liked the video make sure you guys hit the like button as always thank you guys so much for watching thank you for supporting the channel and until next time keep on coding [Music] [Music] [Music]
Info
Channel: Keep On Coding
Views: 44,722
Rating: 4.9574466 out of 5
Keywords: java interface, java interfaces, java interface example, java interface tutorial
Id: Yat8l37XGFA
Channel Id: undefined
Length: 14min 48sec (888 seconds)
Published: Wed Jun 10 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.