Skip to content
November 17, 2011 / Ben Chun


I’ve always found the concept of abstract methods and classes in Java tricky to teach. It’s easier than designating methods as “pure virtual” with special syntax like in C++, and avoids some of that complexity since all methods are “virtual” (unless they’re final, which I think is a nice way to express that idea). Python and Ruby have also taken the all-functions-virtual path, which is an affirmation that it makes sense for most use cases.

In terms of learning curve, students need to have a solid understanding of inheritance and polymorphism before abstract methods make sense, and even then they sometimes wonder why they exist and why a class has to be marked abstract if it contains an abstract method.

It’s been suggested to me that teaching interfaces before even teaching inheritance may help this make more sense. That’s something I’d still like to try.

On a recent homework assignment, I asked, “Now we know it is possible to declare classes abstract. What does it mean to be “abstract” and why would we want a class to have this property?”

The range of answers says something about this challenge. Some students basically said they had no idea:

To abstract means to tell the compiler to stop the code that is running and we want a class like that because making sure that the codes are in the correct place is important.

Others knew the mechanism but didn’t see the utility:

Being abstract means that the class can’t be instantiated. We would want a class to have this property because that will cause the compiler to stop any code from making an instance of that type.

And some, of course, had a better understanding:

“Abstract” means that it is just a reference class that is used for polymorphism. We would want to have this property because it guarantees that the an object of that type will never be created.

I think Java is actually a nice teaching language since it includes some of these concepts without making them as complex and option-heavy as C++, and requires a strict object-orientation and strong typing unlike the syntactically simpler scripting languages. The hardcore among you will point out that the right way to understand all of these language features is to implement them yourself in Lisp, and while I tend to agree, it seems like there just aren’t that many students who can start there.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: