Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In a previous video, I described the concept of encapsulation, one of the major concepts of object-oriented programming. There are two other concepts that you need to master to be a great object-oriented programmer, and they are named Inheritance and Polymorphism. I'll describe them here. Inheritance means that there's a relationship between classes in your application. An inheritance relationship lets you inherit or extend functionality from one class to another. In the world of C++ there is multiple inheritance.
A single class can inherit functionality from multiple other classes but in Java, there is only single inheritance, each new class can extend or inherit functionality from only one other class. This actually makes it a lot easier to manage your code because if you've created an inheritance relationship and you run into a bug that bug can either be in the class you are working with or in the class that you inherited from, but the issue has to be somewhere in that very linear path. There are a number of ways to describe an inheritance relationship and they are just different vocabularies.
You'll hear some developers describe the relationship as a parent-child relationship, where the parent has the functionality and the child is inheriting. You will also hear the terms base and derived, where a base class has the functionality and the derived class is extending it. In the world of Java, the terms that are most commonly used are Superclass and Subclass. The Superclass has the functionality and the Subclass is extending the Superclass. And again, each subclass in Java can only have one Superclass, in fact even if you don't explicitly extend a class all of your classes are extending another class, a Superclass.
There is a special class named object with an uppercase O and this is at the top of the Java inheritance tree. If you don't explicitly inherit from some class, you're inheriting from object automatically, so that's Inheritance. Polymorphism is the last important term to understand. Polymorphism means that when there is an inheritance relationship between a Superclass and a Subclass you can deal with an object that's instantiated from the subclass either as its native type, the subclass or as it's inherited or to extend a type the Superclass.
To do this, you declare the object and instead of declaring the native data type you declare the Superclass type. If it's a class that you're instantiating by calling a constructor method, you call the constructor method from the subclass but you use the data type of the Superclass. When you've done that it becomes possible to take that object which you declared as the Superclass and pass it to any method that's expecting an instance of the Superclass. When you have an inheritance relationship this can give you enormous flexibility in designing your code.
Let's visualize an inheritance relationship. Let's imagine a class named Olive. Well there are many kinds of olives in the world and so you might declare a Superclass named Olive that has functionality that's common to all olives but then you might create multiple subclasses, each representing one particular kind of Olive. This is how we would diagram the relationship with an arrow pointing to the Superclass. That arrow means that the subclasses are extending the functionality of the Superclass.
The Superclass is olive and all the others are the subclasses. We call this single inheritance because each subclass can only have a single superclass, but as you can see, a superclass can have many subclasses. superclasses don't need any special code, any class can be a superclass. And all of the fields and methods of that class can be inherited, unless they're marked as private. You'll see that it's very common to set fields or data values as private in a class and methods of a superclass as protected or public.
Private means that only the superclass can deal with the field directly, protected means that a method can be called either by the superclass or by its subclasses, and public means that those methods can be called by anybody. Let's take a look at code of a sample superclass. This might be the class Olive, the class Olive might have a couple of fields or data values. I've declared a double value called volume and a Boolean value called crushed which is initially set to false. I have a method called getVolume and the logic of that method says if the value crush is true then return the volume and otherwise return zero.
This is using the ternary operator, a shortened conditional syntax. There is a protected method named setVolume, it's protected so that it can only be called by the class itself or its subclasses. And there is a public method named crush which basically says this Olive has been crushed. The fields or data values are marked as private only the superclass can deal with them directly. To get volume method which is a getter accessor method can be called by anybody in the application.
This setVolume method, the setter can only be called by the class itself and the subclass and the crushed method is declared as public. Now again this is just one way of designing the code, it follows some standard recommendations in Java by keeping private data private, but you could choose to do it differently. Now the subclasses that extend the superclass. In order to extend the superclass you declare the class in your Java file and you use the extends keyword and reference the superclass.
It might look like this, I'm placing my class into a package named olives and then I create the class named Kalamata and I use the extends keyword and then the superclass named Olive. The subclass implementation might have a lot of details but here's one small example. As the subclass is constructed, and its constructor method is called it would be responsible for setting its own volume or the amount of oil it's capable of producing. So within the constructor method Kalamata which matches the name of the class itself it calls this.setVolume(2).
SetVolume is a protected method, so the subclass can call it, but the actual data it's affecting is stored in the superclass, it's hidden from the rest of the application. Another subclass of the same superclass would call the setVolume method also but it would set its numeric volume differently. So now Liguria is saying, I can get five units of oil and Kalamata is saying, I can get two units of oil. They're both calling the same method, they're both using that same hidden data that's in the superclass, but they are determining what the actual value is.
When you go to the main class code, you can now say I'm going to declare an array of Olives. Olive, bracket, bracket, means multiple Olives. But then when you actually declare the instances of the object, you use the constructor methods of the individual subclasses. I have an array with two Kalamatas and one Liguria, but they're all olives because of the inheritance relationship. I could then create an instance in the OlivePress call to getOil method and within that method it would then go and call the appropriate methods of the olive object and for Kalamata's, it would get two units, for Liguria's it would get five units and so on.
The rest of the application does not need to know the mechanics of how the volume is being calculated. All the top-level application has to do is say, I've got these three olives and they're of these types, go ahead and crush them. The actual mechanics are hidden within the class implementations. This idea that you can create an instance of a class by referring to it uniquely named constructor method but then take the object and fit it into a data type of its superclass is polymorphism at work.
You're taking an object that has a native type in your saying I'm going to use it as though its supertype, that's polymorphic and it allows you to build really elegant applications Within the implementation of the OlivePress, it would simply loop through the olives and call the methods, it would get the oil, it would add that to the olive oil, and the end result would be a correct calculation. The OlivePress doesn't need to know how much oil you get out of each olive and it doesn't even need to know that they're all these different kinds of olives out there.
From the point of view of the OlivePress, there is only one kind of olive an Olive, and you can crush it and you can add it to the oil. The details are hidden within the implementation. So that's the idea of inheritance and polymorphism. In the rest of the videos of this chapter I'll show you how to implement inheritance and put it to work polymorphically in your applications, and then in a later chapter we'll go on to more advanced concepts using constructs called interfaces and abstract classes that also adds to the inheritance model of Java.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97511 Viewers
61 Video lessons · 84750 Viewers
71 Video lessons · 68928 Viewers
56 Video lessons · 101379 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.