declare

Java Programming: 17 - Abstract classes and methods



Sharing buttons:

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 let's

right-click select new class and let's

name this class game object let's

right-click game again create another

new class and name this class player and

click finish and let's 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 game object

Java and in the class declaration after

public but the keyword abstract that's

all there is to it now the game object

class is abstract so you're probably

wondering what did that do well 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 if you 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

we'll see this works just fine and this

is because the player class is not

abstract 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 a 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

and if I were to ask you to create me a

new game object if I ask 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 and

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 well that comes to

my mind would be every game object is

probably drawn in one way or another

inside of the game whether it's your

player or part of your 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 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 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 draw and it allows us to add the

unimplemented methods so we can just go

ahead and click that and there we go 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 player itself is not

abstract it must provide the

implementation for the draw method right

now this method doesn't do anything but

we could do whatever we wanted to right

here this might make a little more sense

if I create an additional example so I'm

going to create another class inside of

the game package and we'll call this

class menu now a menu could also be

considered a game object so we add a

extend game object will be forced to

once again implement the draw method and

now you have two 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

CISO control space bar menu draw

and go back to player give us this Oh

control spacebar player draw now you can

see that when we call the draw method on

these objects it will execute the

expected implementation so to see that

let's go over to game object where a

main method is let's comment out this

line that won't work because we're

trying to instantiate the abstract class

which we cannot do right here we're

creating the player after that we could

say player dot draw let's instantiate a

menu and after that let's say menu dot

draw and if we were to run this we

should see player draw and then you draw

which makes sense now I'm going to show

you something that's kind of a big deal

going forward and that is the type of

our variables here we're using a player

and a menu but we could actually say

that both of these variables are of type

game object 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 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

I'll call it game objects equals new

game object and then the square brackets

and the size of our array so now we've

set up a place in 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 1 equals menu now we've

stored references to the two game

objects we've created inside of that

array now what 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 : game objects curly brace will

say obj dot draw so how to read this

line is for each game object in our game

objects array refer to the object by obj

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

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 a

variable dictates the interface of the

object so what does that mean that means

that let's say we go over to player Java

and create another method called

public void some method doesn't have to

do anything the point is this method is

part of the player class

so if we go back over to game object

when we create a new player we would

expect to be able to say player dot some

method but if we do that

Eclipse underlines it in red and it says

some method is undefined for the type

game object so if you think about what

that's saying it's looking in game

object for that method and it can't find

it which makes sense because we define

that method inside of the player class

and this is only because we are

referring to player by the type game

object 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

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 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 will see player draw

and menu draw 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 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 finish the video

let's throw in a comment above our one

abstract method and let's say this

method must be implemented by any class

that extends game object 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