Abstract Classes and Interfaces

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is a brief lecture to introduce you to the cons classes and interfaces so let's jump right into it what is an abstract class well an abstract class is a mouthful in terms of the definition we've got sitting in front of us a class that represents a generalization and provides functionality but is only intended to be extended and not instantiated that's a big run-on sentence that basically tells you that an abstract class the entire purpose and existence of an abstract class is to be extended by other classes it's meant to be a superclass and other classes are then meant to inherit from that class and take on its functionality or extend that functionality as they see fit so it's really nice about abstract classes is that it lets us define a generalization that can then be shared amongst a number of specializations or specific concrete implementations of classes and a couple other things that will we'll talk about as we go through this but but really the key takeaway here is abstract classes are meant to be extended and they can never ever ever be instantiated so let's talk a little bit about abstract classes and then how they differ from regular classes first of all an abstract class may contain instance or static variables there's no difference from regular classes in that aspect abstract classes may also contain instance or static methods that's no different than regular classes here's where things start to get a little bit different since an abstract class is only meant to be extended it is never meant to be instantiated we need to talk a little bit about some rules regarding the constructor so in case of an abstract class you can define a constructor in your abstract class but it can never be called directly because an abstract class again is never meant to be directly instantiated in other words you will never call new on the datatype of the abstract class if you would want to define a constructor the class that is extending the abstract class this subclass would need to call the constructor for you so that's one of the things that's important we can't directly call constructor in an abstract class it must be done through one of the the subclasses another thing that's unique to an abstract class that is not normal in a normal class is that we can have what are called abstract methods and abstract methods are methods that have no implementation and we remember that the implementation of a method is that part between curly brackets so there's no need to have implemented methods well where do those methods get implemented they actually get implemented in the subclass so we're going to be able to defer the definition of our method implementations to the class that is sub classing our abstract class and one of the thing to note about abstract methods is that any class that contains even a single abstract method is itself an abstract class regardless of how it is defined so even if you forget the apps the abstract keyword when defining your class public abstract class you would then go ahead and and accidentally put an abstract method in there you have automatically made that class abstract so whether you have the keyword at the class definition or you have the keyword abstract in front of a method in either place it's enough to make your entire class an abstract class what does this look like in UML well what you notice here is we have a class called animal and the one thing that you should notice is that the name of this class is actually in italics and italics and UML indicate an abstract class so if we come down a little bit you'll notice that I have a regular instance variable and what I'm doing is you know defining a generic animal all animals have weight I'm gonna store the weight as an instance very limit double so that's kind of nice and notice this idea of an animal like what is a generic animal it's just you know if we had to talk about I'm not a biologist but you know animals have certain characteristics and I would say one of those characteristics is that they have weight okay so let's go down look at behaviors well notice I've got a get weight and a set weight behavior and these are nothing different than what we've seen in the past in terms of regular accessors and mutators so these I can write right these are methods that we'll get and set the value of weight but here's something that's little unique I have an eat method and notice that eat is also in italics well since this is an abstract class eat indicates by being in italics that this is an abstract method and what that means is the user will not define this method an animal we will not have any code between curly brackets before this method and what will happen is the definition of this method will be deferred to the cat class because guess what animal is a generic concept that's an abstraction of a concept okay ak at the sub class here in this case that is extending animal is the concrete example of an animal that we want to use in our code so notice that by inheriting from the animal class the cat class automatically gets the get weight and set weight methods it also gets the weight instance variable and what's being told in this process is that the cat class needs to conform to the contract set up by the abstract animal class in other words because eat is an abstract class cat being the fact that it's not going to be an abstract class a class we want to instantiate we actually want to make cat objects we'll need to actually implement the eat method so you can think of it as the fact that every animal eats but but animals tend to eat in different ways like a cat might eat different than a dog a cat probably definitely eats different than a bird or a snake or something along those lines they're all animals but they have different ways to eat so it lets you say look I know that all animals eat but I'm gonna leave the implementation of how they eat up to you when you subclass the animal class because I don't know exactly what type of animal you're gonna create and I know that animals can eat differently so let's define this as an abstract method and leave and or defer the implementation of that method to later what's a good visualization of this well I mean has anyone ever seen a generic animal it would be some frightening genetic mishap possibly but we all know what a cat looks like but you know so animal is a concept cat is an actualization so I would probably imagine that a generic animal would look something like Meatwad from Aqua Teen Hunger Force right I mean some really really scary like kind of moldable shapeable being that that really doesn't conform to any concept of an animal so this is our generic animal up here cat is a class that extends animals so when we go ahead and we extend the abstract class animal to form a cat we're basically taking this general asia's generalization called animal and we're turning it into a concrete representation of a type of animal so again we've got this abstract concept of animal and then when we subclass it we get all of those kind of abstract behaviors and proper animals should have but then we can build on them to shape and make a concrete example of what an animal is and in this case cat how does this different from an interface well an interface is a completely abstract class that defines a protocol for object interactions so in our previous example you'll notice that I've got in my animal class I have a mix of abstract and non abstract methods and I've got regular instance variables in that class and abstract classes you know the really the only difference between an abstract class and a regular class is that we can't instantiate the class and that we can have these kind of undefined methods that we call abstract methods in them but an interface is unique in that it can only contain abstract methods and a few other things so let's run through this just like we did with the previous example so interfaces may contain only static final variables well that's unique right no instance variables Y well interfaces are not meant to be instantiated directly there will be no instances of a specific interface datatype only implementing data types and you know we'll talk a little more about those ramifications as we progress through the class but an interface may only define static final variables and that's an important thing to keep your to keep in your mind when you're defining interfaces interfaces may also only contain abstract methods so you may only define methods without implementations in an interface a couple other thing is that interfaces cannot contain a constructor because again they're not meant to be instantiated they're meant to be implemented by other classes which would assumably have their own constructor one of the cool things about interfaces is that they can extend other interfaces so we're used to having this inheritance hierarchy of classes when we utilize inheritance we can also have interfaces that inherit from other interfaces seen kind of even abstract out general concepts in terms of interfaces and have like this whole set of really nicely defined hierarchies of protocols that people can implement at whatever level they deem appropriate a couple things that are really nice we know that this fact that in terms of inheritance that classes can only extend class in other words each child class or subclass can only have a single parent class because of single inheritance in Java but classes can implement any number of interfaces that they want up to I'm whatever hard limit Java has I would not want to implement more than a couple of interfaces on object because that would just be annoying but you know there I'm sure there's a limit around 255 or I have no idea what the implementation limit is for interfaces but feel free to figure that one out on your own but that's really nice here is that I can implement as many interfaces as I need and this is one way that people often say that Java gets around this idea of single inheritance because one of the things that happens is when you implement an interface you are that data type in other words a class that implements an interface has an is a relationship with that interface and so normally we can usually say that something like a cat is an animal well you know a cat in that case really is only two datatypes it is a cat and it is an animal but if I went ahead and implemented an interface like pet well now my my cat could be a pet my cat could be a cat and my cat can be an animal so I can have a number of data types or in other words my object can be more polymorphic by implementing interfaces because when you implement an interface you are that given data type just like when you inherit from a parent class you are that given data type what's this look like in UML well if we go up to the top you're gonna see Gyo maize which of these less than signs and greater than signs and the word interface and basically what this does is it indicates that this block is an interface and it gives the name of the interface in this case of valuable so anyone implementing this interface would be of data type valuable so we could say like hey gold is valuable yeah because this interface is being implemented by this concrete class gold notice that in this case it defines or allows me to define behaviors and basically what I'm saying here is if you comply to my valuable interface then your class must have a method called is valuable and it must return a boolean what hap inside of that method I don't care I don't know all I'm saying is someone who wants to work with a selection of valuable objects should it should know that there's a method called is valuable there and it should return a boolean and notice here that we have this interface defined notice that in Argo it throws in what's called a realization mark here basically because when you implement an interface it's said to be that you are realizing that interface so in this case gold implements the valuable interface it's realizing that interface by agreeing to conform to the contract and anytime you decide to conform to an interface you are basically saying that yes I want I will follow your rules because if you don't you'll get compiler errors so in this case gold is valuable gold is gold and gold will have a method you know called is valuable so one of the things to note in UML is the difference here notice that when we do inheritance we have a solid line when we do realization of interfaces we have a dashed line so that's the only real difference in terms of UML in the case of those items so here are some key takeaways abstract classes and interfaces allow us to define a set of standard protocols for programmers to implement in their code so what's really great here that a lot of I think students don't quite get is that and I made some comments about this on the course is that you can all agree to name methods certain things and you can everyone on your software team or around the world can agree to have methods named a certain thing that returns a certain value but the reality is people are gonna you know they like to do their own thing or they get tired or they get lazy one of the cool things about abstract classes and interfaces is it allows you to define in code a set standard or a set way for people to code to that that guarantees through the compiler that if you are say a valuable object you implement the interface valuable the compiler will guarantee that your method has an is valuable method and that's so much more powerful than just you know having someone proofread it or just a handshake agreement around a table so this idea that having standardization in code is excellent but having the compiler enforce that standardization is amazing because it allows for a lot more power so when we define abstract classes and we defined interfaces what we're really doing is not you know not only are we allowing flexibility into our code but we are defining a standard way of doing things for other programmers who work with our code and once you have standard ways of doing things you know then people can build a pop on top of those standards with your code and do some really powerful things because they can they can expect that your code works as specific ways interfaces also allow objects to be polymorphic I just said that beyond the constraints of single inheritance again if you implement an interface you have an is a relationship with that interface so you are a type of that data and also one of the things that I often tell students to keep in mind is that you know for every interface that you define someone has to code all of the implementations for all of those methods so as we start thinking more and more about oo design you don't want to see interfaces as kind of this end-all solution because for every time you implement an interface you have to write all the implementations for all of its methods and and that gets to be a lot of work and as we get into talking about design patterns we get into talking more in depth about design we'll look at some ways to reduce the need to rewrite many many many many many many many methods over and over and over and over again because you have implemented interfaces let's take a look at some of the samples discussed in the concepts section of this lecture and how they translate to code sitting in front of me is some code that represents a class that represents an animal and what we look here is that we've got public abstract class animal the only difference in the definition of this class is the abstract keyword and we know that abstract classes by their definition cannot be instantiated so the compiler is going to enforce that so if I go over to my driver here and weave that up so we can see both I'm trying to instantiate an instance of the animal class I'm going to compile that and what's going to happen is you're going to notice that I get an error well that is because animal is declared as abstract and it cannot be instantiated so there's proof that we cannot instantiate abstract classes and that makes sense right because we don't want some we don't want the concept of an animal to exist within our code we only want a specific type of animal to exist so I have a class and it is called cat and cat extends animal so notice that animal is an abstract class cat is extending that class and now I can go ahead and if I change what I'm trying to instantiate here - cat will be fine in terms of compilation because I can instantiate an object that is not abstract even though it extends that abstract class why because I'm saying okay here's this general concept of animal what I would like to do is make a specific example of that animal so the semantics play a large role in this and abstract classes allow us to control those specific semantics so in an abstract class I can define you know variables just like I would normally so we'll do private double weight and I'm not going to go ahead and well yeah let's do like a getter right so public double get wait and there's a full method right and I can return weight and if I wanted to I could you know in my driver say something along the lines of a dot get weight and we would see that represented in our class so we get no errors and obviously that's not doing anything it's not printing anything but the the key here is that we're not seeing any errors upon compilation and even if I run it right nothing's gonna happen but again we're getting no runtime errors we're getting no compile errors so we know that that's cool and that means we know that the cat class is inheriting this method but one of the other things we can do is have abstract classes or sorry abstract methods inside of our abstract classes so I'm going to do public abstract void eat now the big difference between this method and the previous method is that this method has no implementation and instead of having a set of curly brackets that have a block of code inside of them abstract methods have a simple semicolon at the end and the abstract key word to indicate that hey this is a method that has no implementation and it's abstract and that means that I'm deferring the implementation of this method to my child class so I can compile that and you can see that my class compiles fine because it's abstract and if I come over my driver I'm gonna go ahead and I'm going to compile and I'm gonna run mmm and I didn't get an error let's come over here compile this ah there we go that was interesting that I didn't get the error from the other class but anyway not gonna dig into why that happened but I'll leave that in the lecture because that was kind of an interesting thing to happen it might I'm not sure what happened there anyway notice that when I try to compile my cat class I'm getting an error I'm getting cat extends animal but it's not abstract and it does not override the abstract method eat so here's the thing whenever you choose to extend an abstract class if there are any abstract methods in that class then you as the programmer must implement those abstract methods it's a contract so the person who set up the animal class me has said that look anyone who extends this class must either implement the abstract class eat or themselves be abstract so I can get rid of that method by making cat abstract although isn't really what I want to do all right so I can hit compiled and notice now that air goes away but I want people to be able to actually make cats so I can you know but so I'm not gonna really want to do that so how do I make this happy well I need to go ahead and I need to implement the eat method and all I'm gonna do is put a little print statement in here and something like grrrrrr and now I can compile this and I'm good notice that cat is not abstract notice that it gives me no errors if I come over to my driver I'll change this to eat compile run and I get girl so what I've been able to do here is defer the definition of this method to the cat class in other words tell any classes that extend for me to go ahead and implement eat so what's really nice is we get this mix of having pre-written code and code that we can just inherit and use right off the bat so that that's nice there's a reusability factor there and what also is nice is that we have this idea of abstract methods that say well you know not every animal eats the same or not every animal speaks the same so I'm gonna let whoever extends me write those methods but what I'm gonna do is I'm going to define these methods here anyway so that I can guarantee that everything that extends animal or everything that is an animal has a common name or a common method signature for how it goes about performing these actions and that's really really important now let's go ahead and look at how this relates to interfaces in this class I've defined an interface called valuable and we talked about this interface before now the difference between interfaces and abstract classes that interfaces can only contain abstract classes okay so or sorry interfaces can only contain abstract methods so I can go ahead app public abstract is valuable and now I've got an abstract class notice by the way I can compile that we should be good Oh helps if I have bullying in there I need a return type so now I get compilation and it works fine and I can make this bigger so you can see there were no errors notice by the way that because an interface can only contain abstract methods you can just leave the abstract keyword off because the compiler just knows hey this is an interface I know any method signature I see in here is going to be abstract so I can get rid of the keyword and I'm still fine so if you like seeing it leave it in if you don't take it out it doesn't really harm anything but what happens if for example I have something like I should put a method in here I call it going to error and I actually have a real method in here I'll just like return true so now what happens when I compile this well I get an error because it says interface methods cannot have a body right the body being that segment between the curly brackets so it's really trying to say hey look if you're going to go ahead and you're going to put method definitions into an interface they have to be abstract in other words they cannot have an implementation they cannot have a body they cannot have the component between the curly brackets so there's my interface it's compiling we know it's good so now I'm going to go over to my class called gold and what I'm going to do is I'm going to implement valuable so now you'll see that my class is going to implement the valuable class and implements is the keyword for interfaces in Java so I go ahead and pilot says yellow gold is not abstract and does not override abstract method is valuable so we get the same error basically that we got before it's saying hey look you agreed to implement this interface and by agreeing to implement that interface you must provide an implementation or definition for every method that that interface provides so let's go ahead and do it oops public boolean is valuable let's make this we can see we're doing here some spaces in there so it's readable and all I'm gonna do is just put return true cuz it's gold right I don't need anything beyond that now when I compile this it compiles there's really nothing to run Reich's I haven't created anything so what's interesting here is that we note that when we have an interface and we implement the interface then just like in an abstract class we're required to implement all of the abstract methods in that class and again the implements keyword is what allows us to indicate that we want to implement a specific interface by the way if we had multiple interfaces that we wanted to implement you just separate them with a comma after the implements keyword so let's say for example I want gold to implement valuable and comparable all I have to do is go ahead and put a comma between each of the interfaces that I wish to implement up to whatever the limit is on interfaces being implemented in Java so that's the basics of using interfaces and of using abstract classes and again these are some more design tools that we have available to us one thing if we want to take a look at at the driver I guess before we we exit is note that cat can actually be stored in an animal variable because cat is polymorphic right a cat is an animal so if I go ahead and I run this compile it no errors pull this up so we can kind of see everything in the window they use the skinny window so that the code doesn't get fuzzy when I compress the videos so it's a little bit of an odd size to work with and let's run it it works it still works why because animal or sorry cat is a subclass of animal so it's a specialization of my abstract class and the animal reference knows about the eat method why because the animal reference actually went ahead and and defined that interface for eat so I could actually write like a zoo application that would have many many many animals and I could just store them all into an array and that stores animal components and just call the eat method on them we have the same ability to do the do this with gold because gold is of type valuable remember when you implement an interface then you are an item of that data so this object is type gold it is type valuable okay so we have this idea of polymorphism and we can really leverage that using using interfaces using inheritance using abstract classes and building you know and designing our classes in such a ways that are appropriate right there's that word in oo design again for the task at hand
Info
Channel: Jason Wertz
Views: 293,982
Rating: undefined out of 5
Keywords: lecture
Id: AU07jJc_qMQ
Channel Id: undefined
Length: 26min 33sec (1593 seconds)
Published: Mon Apr 23 2012
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.