#17 Abstract Classes and Methods

In this tutorial I’m going to cover abstract classes and abstract methods to get started.

Let’s right click on our source folder select new package and let’s call this package game and click Finish. And inside of game that’s right click select new class and let’s name this class game object. That’s right click game again create another new class and in this class player and click Finish.

p182.jpg

and lets have player extend game object. So if you’ve been following along in the tutorials you should have a pretty good idea of what’s going on here; our player class is inheriting all the functionality of the game object class. What we’re going to do in this tutorial is make our base class abstract. So if we go over to GameObject.Java and in the class declaration after public but the key word abstract that’s all there is to it.

Now the game object class is abstract.

p183

So you’re probably wondering what did that do all to see that I’m going to create a main method inside of this class and I’m going to try to create an instance of game object.

If we were to say game object my object equals new game object you’ll see that eclipse doesn’t like that.
p184.jpg

If we hover over it says cannot instantiate the type game object and we cannot instantiate this type because this type is abstract. If on the other hand we were to say player player equals new player Well see this works just fine. And this is because the player class is not abstract.

p186.jpg

So what adding this abstract modifier to the class did, is prevent us from being able to create an instance of this object, which leads into the first big thing you should know about using abstract classes and that is that:

// abstract classes exist to be extended. They cannot be instantiated.

And hopefully that makes sense in the context of the example I’ve chosen to use here where we have this base game object class which is kind of an abstract notion by itself and this much more concrete idea of a player which extends the game object.

So in other words if I were to say to you create me a new player you’d have a much better idea of what I’m talking about than if I were to ask you to create me a new game object.
If I asked you to do that you really have no idea what I’m talking about a new game object could be a player or it could be an enemy or it could be a box or it could be part of the UI. I mean you have no way of telling. So this abstract keyword sort of allows us a way to represent the idea of an abstract object. But more than that it will literally prevent you from creating an instance of something that is abstract.

So what’s the benefit of an abstract class?

Well inside of an abstract class you can use abstract methods. So what’s an abstract method. Well let’s create one. Think about what’s a behavior that a game object would have regardless of whatever it may end up being. Because remember this class is abstract, it’s made to be extended and our player class is just one of potentially many classes that could extend game object.

So it would be something that would be common to every game object one that comes to my mind would be every game object is probably drawn in one way or another inside the game whether it’s your player or party or UI or something in the world.

So a method to draw this game object would be a good candidate to be abstract. So creating an abstract method is very similar to creating any other kind of method. I’m going to say public void draw () .Now before to create a method we would put curly braces in and provide some sort of implementation some kind of behavior that would happen when this method is called.

The difference with abstract methods is that you’re just saying that this method is going to exist inside of this object whatever it may be.

But I’m not going to provide the implementation for it at this point. I’m going to defer that implementation to the object that extends this one.

So the way we do that is we don’t use curly braces we just say draw and we add the modifier abstract and that’s all there is to it.
p187.jpg

Now this is saying is that any class that extends game object must implement the draw method. And if we were to save this right now. Notice that the player class becomes red.

p188.jpg

If we hop over to it and highlight over the red underline, it will say the type player must implement the inherited abstract method game object I draw, and it allows us to add the unimplemented methods, so we can just go ahead and click that and there we go.

p189.jpg

Now if we save this off, you’ll see that our red goes away and this is because the player class is extending game object which has an abstract method called draw and because the player itself is not abstract ,it must provide the implementation for the draw method.

Right now this method doesn’t do anything but we can do whatever we want to do right here.This might make a little more sense if I created an additional example.


So I’m going to create another class instead of the game package and will call this class menu.

Now menu could also be considered a game object so we have to extend game object will be forced to once again implement the draw method, and now you have to very conceptually different objects: a player and a menu and both of them are providing

their own different implementations for the draw method.

For example we could say do a syso control spacebar and you draw and go back to player do a syso ctrl-space bar player draw.

Now you can see that when we call the drama on these objects it will execute the expected implementation.

So to see that let’s go over to game object where main method is let’s come into this line of work because we’re trying to instantiate the abstract class which we cannot do right here we are creating the player.

p190.jpg

After that we could say player.draw have to instantiate a menu and after that let’s say menu.draw ();

And if we were to run this we should see player draw and menu draw. Which makes sense.

p191

Now I’m going to show you something that’s kind of a big deal going forward.

And that is the type of variables here.

We’re using a player and a menu.

p192.jpg

But we could actually say that both of these variables are of type game object.
p193.jpg

Now remember game object is an abstract class. It has an abstract method called draw.

We are creating a player and referring to it by a game object type variable and the same thing here we are creating a menu and referring to it by a game object type variable.

And now we are calling the draw method on each of those game object type variables and if we run the program you’ll see that the output is the same as before.
p194


Now this is a pretty central thing to object oriented programming and object oriented design.

And it’s used heavily by many design patterns out there and you may have heard terms like polymorphism but to really drive the point home and help you to see the value in doing this I’m going to do something that might blow your mind.

Let’s say I were to create an array of game objects and call it game objects equals new game object and the square brackets and the size of array.

So now we’ve set up a place memory to store two references to objects of type game object. And now if I were to say game objects 0 equals player and game objects one equals many you know stored references to the two game objects we’ve created inside of that array.

p195.jpg

Now we can do is use a for loop and we can say essentially for every game object in this array do something. so we can say for game object object colon game objects curly brace you will say obj.draw.

p196.jpg

So how to read this line is for each game object in our game objects refer to the object by

and call the draw method on that object.

So now if we were to comment out these previous draw calls and run the application we’ll see that the player is drawn and the menu is drawn.


Now think about that for a minute. If we had a real video game which could have I don’t know thousands of game objects and all of them being drawn in different ways we’d just handle drawing everything in our game in three lines of code.

Now there is a bit of a downside to referring to variables in abstract ways like we’re doing here and that is : the type of variable dictates the interface of the object.


So what does that mean.

That means that let’s see if we go over to player dot Java and create another method called public void someMethod, doesn’t have to do anything.

The point is this method is part of the player class.

p197.jpg

So if we go back over to game object when we create a new player we would expect to be able to say player.some method. But if we do that Eclipse underlines in red.

And it says some method is undefined for the type game object.

p198.jpg

So if you think about that saying it’s looking in game object for that method and it can’t find it which makes sense because we defined that method inside of the player class and this is only because we are referring to player by the type of game object.

p199

If we were to change the type back to player you’ll see that the red went away because we’re using the type player which as I mentioned before it determines the interface for the object that you’re working with.

And when I use the term interface all I mean is if you were to say player dot this is your interface.It’s everything that you can do with a given object.

p200.jpg

It’s the interface by which you interact with that object. So you should also notice that when I change this type everything else still works.

We can do the same with our menu and refer to that by menu type and we can still assign these to the array of type game object and that is because of inheritance both players and menus literally are game objects so we can store them in an array of type game object where we can pass them to methods that expect game object arguments.

And just to show you that everything still works. Let’s give it a run.

We’ll see player draw menu draw.

p201.jpg

So one final thing to say about abstract classes is that you don’t have to include abstract methods in them.

In fact an abstract class is just like any other class in terms of what you can put inside of it. You can have private variables, you can have regular old methods whatever you want to do inside an abstract class.
p202.jpg

All that adding the abstract modifier does is allows you to use abstract methods.

And as I mentioned before it prevents you from creating instances of that abstract type directly.

So before I finished the video let’s throw in a comment above for one abstract method and let’s say :

// this method must be implemented by any class that extends game object.

p203.jpg

So I think that about does it for this tutorial and this should be a good lead in to our next topic

of interfaces.

Thanks for watching.

Leave a comment