Welcome back to this course, Java as a second language. This is module three, Java inheritance and polymorphism. Java supports inheritance and polymorphism in a similar way to C++, if not identical to C++ and other object oriented programming languages. Inheritance is implemented using the keyword extends, and that extends keyword is in the class header just to the left of the class from which we are inheriting. A little bit about inheritance. Inheritance is a big part, an important part of object oriented programming. Every time we instantiate an object, we are inheriting the classes, properties, and methods. Also inheritance supports the ability to reuse existing features and functionality. So that's important, saves us time, effort and energy. Provides flexibility. If we think about that reuse again, if there is a class that already has the desired functionality, there's no need to rewrite it, so again, saving us time and effort. But if we think about it, if there is a class out there that has the properties, methods, features we need, chances are that that code's already been approved, it's already been tested. Inheritance and polymorphism not only can save us time and effort, but they can increase quality. We can get to market faster and we can have a better product. If we take a look at these two classes, Bicycle and tandemBike, obviously tandemBike is a type of Bicycle, so you can see the relationship there. Anytime there is a general and specific relationship, there's always an opportunity for inheritance. The class tandemBike is inheriting from Bicycle. We can see the extends keyword right here. That means that tandemBike is the super-class of Bicycle, and Bicycle would be the subclass because it's the foundational or the base class. Now tandemBike has all of the properties and methods of Bicycle. But here we can see that tandemBike has some of its own. Number of seats; number of seats would be something specific to tandemBike. We would want to obviously keep that in the tandemBike class while still being able to return some of the methods and some of the properties of Bicycle. Things like set the number of gears, get the number of gears. We take a look at our editor, and we have used a visual editor. We can see here again that tandemBike is inheriting from Bicycle. After we instantiate tandemBike, we can see that my2Bike is instantiated and is a type of tandemBike. As we go down here, our editor gives us suggestions, so helps us code. This is a visual editor. Again, this is Eclipse. We can see here that as we begin to type, it's offering us methods of Bicycle, and it's also offering us methods and properties of tandemBike. So we can see we have both right here, and our editor is allowing us to use both. Again the concept of polymorphism we can see here as well, because again tandemBike is also an extension of Bicycle, if we look closely, we can see Bicycle in the tandemBike, but we see it in a different form because it's been extended with other methods and properties that are unique to tandemBike. Overloading function. Early on we talked about the plus sign being an overloaded operator, so that one operator can have different functionality. It can do more than one thing. Java also supports overloading functions, and so a class can have more than one function with the same name, provided the numbers of parameters are different in order to provide some type of differentiation. We can see here this class, tandemBike has two methods, both called setNumSeats. The way that we call these will determine which one of these methods actually gets called at run-time. It will depend on how the parameters are passed in. If we pass one integer at run-time, it will be this method that gets called. If we pass no parameters, then number of seats is automatically set to six. If we take a look here, again, this would be after instantiation, so we have my2Bike is a new tandemBike so we have now our tandemBike object right here, and we're calling set number of seats and we're passing in two. That means that it will be this method that gets called, and since we're passing in a two, number of seats is going to be set to two.