Java interface πŸ¦…

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's going on everybody it's your bro hope you're doing well and in this video i'm going to teach you guys about interfaces in java so sit back relax and enjoy the show make sure you like comment and subscribe one like equals one prayer for the youtube algorithm well let's talk about interfaces think of interfaces as a template that can be applied to a class it's similar to inheritance but it specifies what a class has or must do classes can apply more than one interface so that's a key thing to take away from this whereas an inheritance is limited to just one direct superclass here's an example i have three additional classes i have a class called rabbit a class called hawk and a class called fish we're going to be creating some interfaces too for this example one called prey and another called predator and we're going to have some of these classes implement these interfaces depending on what the animal is so this is how to create an interface go to file new interface and let's call the first one prey click finish all right so we have an interface called pray public interface pray let's create another interface file new interface this will be predator okay so we have two interfaces prey and predator with interfaces you can declare variables normally like you can do with inheritance you can also declare some methods and you do not need to create a body for these methods so when a class is implementing one of these interfaces they need to implement and define what this method is going to do so let's begin with pre let's write just one method so what's a method that prey will do well pray flee from predators so let's create a method called flea this will be void flea and that's it we do not need a body so void flea that's it there is no body to this and let's say that our rabbit is going to use this interface now to implement an interface when you define the class just type implements then the name of the interface we would like to implement the prey interface implements prey so one thing with interfaces if you declare a method it works like an abstract method we need to define what this method is going to do so within the rabbit class we need to add any unimplemented methods so we're basically overriding this flea method so what do we want our rabbit to do when we call the flea method so maybe just a simple system.out.printline that states the rabbit is fleeing and let's test this let's create a rabbit object rabbit rabbit equals new rabbit and let's see if we can call that method rabbit dot flea there it is the rabbit is fleeing now let's apply the predator template well interface to the hawk class because hawks are predators they are very rarely prey well i could be wrong all right so public class hawk implements predator and we should probably create a method within predator for this example so if a animal is a predator they should have the ability to hunt so let's create a method called hunt void hunt and we do not need to declare a body so within our hawk class we need to add any unimplemented methods and define what this does within this implementation so let's say if the hawk object is going to use the hunts method we will display the hawk is hunting and let's create a hawk object this time so hawk hawk equals new hawk the hawk is hunting now since we're implementing the predator interface there is going to be no flea method for hawks because we're not using the prey interface we're using the predator interface so just to prove that we'll attempt to use hawk dot flea and it doesn't appear that there's a method for that yep method fleet is undefined for the type hawk all right now we can apply more than one interface to a class so with fish fish can be both prey and predators depending on their size bigger fish tend to eat smaller fish so let's add two templates two interfaces to our class fish and this is how to do that implements prey comma predators so we're applying both of these templates these interfaces to our class fish so we're implementing prey and predator but we need to add any unimplemented methods and we have two this time one hunt from the predator interface and another flea from the prey interface so let's define these so system.out.printline within hunt we'll just state that this fish is hunting smaller fish and with the flea method system that out top print line let's say this fish is fleeing from a larger fish and let's try this so let's create a fish object fish fish equals new fish fish dot hunt so our fish has a hunt method the fish is hunting smaller fish and it has a flea method fish dot flea the fish is fleeing from a larger fish so in conclusion an interface think of it like a template that can be applied to a class it's similar to inheritance but it specifies what a class has or must do and classes can apply more than one interface and we did that with our fish class it's implementing both the prey and the predator interfaces so it has access to both of these methods but it needs to actually implement these methods by overriding these methods so that's the basics of interfaces in java if you'd like a copy of all this code i will post all of this in the comments down below but yeah that's how interfaces work in java hey you yeah i'm talking to you if you learn something new then you can help me help you in three easy steps by smashing that like button drop a comment down below and subscribe if you'd like to become a fellow bro [Music] you
Info
Channel: Bro Code
Views: 17,328
Rating: undefined out of 5
Keywords: java interface, java interface tutorial, java interface examples, interface java, interface in java, interfaces java, interfaces in java, interfaces in java explained, interface in java with example, how to use interface in java, java interfaces, java interface example
Id: GhslBwrRsnw
Channel Id: undefined
Length: 7min 51sec (471 seconds)
Published: Tue Oct 06 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.