You're going to need to fully understand how objects work to make any sense out of Java inheritance. I recommend heading back to the intermediate Java tutorials if you want a full, three tutorial overview of Java objects.
By now you should have been using classes and objects to represent different parts of your program. For example, you can create a Dog class to make dog objects that have all of the characteristics of a dog, a Cat class to make cat objects that have all of the characteristics of a cat, and a Bird object to make bird objects that also have all of the actions and features you would expect a bird to have. As you can imagine, there are some similarities you can probably come up with between all of those objects, and that code is duplicated in each of the classes.
What if I told you that you could make an Animal object that would do this for you? That's where Java inheritance comes in.
The first thing we'll look at when discussing Java inheritance is the concept of a super class. A super class is basically the same as any other class in Java, except that we know that we want other classes to be created from it. Don't worry about how that's done yet; first let's just see what a super class can look like.
But wait, that looks like just a normal class? That's right. A superclass can look like any typical class. So, the magic of the superclass does not lie within the superclass itself, but within the subclass.
A subclass is a child of the parent class. Essentially, subclasses come from their superclasses. Here is an example, using the superclass from above:
There's a new keyword in here that you use in the creation of the class, extends. Extends is what you use to declare that this class is a subclass of the specified class. You can only ever extends ONE class.
So what's the point of inheritance? The point is that we can reduce the amount of code we write and make our code seem a whole lot more like the real world by using Java inheritance. Let's say we have dog and cat objects. Both dogs and cats are animals, so naturally we could create a superclass called animal that defines characteristics of all animals, and then create special characteristics for just a dog and just a cat.
Let's start with our superclass, the animal class. Here we can create variables and methods that all animals will have. For now we will keep things simple and create two variables and two methods that every animal will share. Here is the code:
We have created basic variables and methods that all animals will have. In this case, all our animals have a boolean called isAPet which is set to true, and the owner is set to a String with the value of "Fred". All animals can also sleep and eat.
So, now that we have defined what all of our animals will have, let's start creating subclasses of Animal. We've already created a subclass called Dog, so we'll just use that. The class is empty, but in reality it actually has access to any public or protected methods from the superclass. It's as if I had written those methods in the Dog class. Here's an example:
Notice how this doesn't give any errors because the superclass shares all of its public and protected variables and methods with its subclasses.
So you might be wondering then, what the heck is public and protected? These are two of the three access level modifiers that Java uses. You've seen public a lot if you've been following these tutorials, but protected is new. I'll save the in-depth discussion for the next tutorial; for now what's important is that those two modifiers are the only ones that will allow subclasses to use superclass variables and methods.
Now we can write code that's specific to Dog objects, let's write a method called bark:
Only Dogs can bark, so the subclass has this method. The superclass has no access to this method whatsoever.
Let's now create a Cat class which will also be a subclass of the Animal superclass.
This class is also empty, but it has the same access to the superclass methods and variables. Let's now create a meow method:
This method exists only in the Cat class. Dogs do not know of this method, and neither does the Animal superclass. This is what makes Java inheritance so powerful: the ability to define common variables and methods in a single place and use them again and again. Java made its object hierarchical like this on purpose, and it's nice to take advantage of. There are even more reasons for why using Java inheritance is powerful, but we won't go into those reasons in this tutorial.
Methods and classes in Java can be declared abstract. An abstract method has no implementation, and an abstract class cannot be made into an object. This will make a lot more sense with a code example, using our Animal class that we created earlier:
With this class you cannot do Animal a = new Animal(); because it is abstract. However, other than that the class can be defined exactly the same way as any other class. Except now, you cannot create just any Animal, you're forcing yourself and other programmer to create specific animals like Cat or Dog.
Abstract methods are the more interesting part of Java inheritance. You get to create a method but not actually fill in any of the code inside of it! Here's an example of an abstract method which we'll add to the Animal class, the method move():
Note: It is important that you not use any curly brackets with abstract methods! Also remember to put a semicolon at the end of the line, just as you would do if you were writing a regular line of code.
So what can you do with these abstract methods that have nothing in them? The answer lies within the subclasses. Because the method is abstract in the superclass, subclasses MUST implement the method. This means that now it is up to the subclass to determine what code goes in the method, and the method has to exist within that subclass. Let's demonstrate the power of this by implementing the move() method in both the Cat and Dog classes.
Now any Animal can have common methods but still perform those methods differently. Note that if you do not implement the abstract methods from the superclass in your subclasses, your subclasses will have an error. This means you MUST implement the abstract methods.
So now you should have at least a basic understanding of Java inheritance. If you didn't get it right away, that's ok. Play around with abstract and extending classes. See what's allowed and what's not allowed. Try to extend classes that extend other classes and see what happens. Experimentation is the best way to learn, so go ahead and try it. If you have any questions, comments, or concerns, feel free to contact us.
Leave Java Inheritance and return to Advanced Java Tutorials