Object oriented programming with Java polymorphism. In this module we will discuss the mechanics of polymorphism and best practices for designing with polymorphism in Java Understanding how polymorphism works in a real Java JRE can be tricky. That's why we're going to use our simplified concept, which you've looked at previously. And remember this is just conceptual, not an expression of reality. So here's the idea, there is a table that will be managed, and in that table we have the type of the reference. Remember, and this is the Important idea I've tried to get across over and over and over. The type of a reference is what you can now say to it, what interface it exhibits. The implementation or class of the thing associated with that reference, the thing that the reference points act determines how that interface is implemented. So, the type of the reference is what you can say and the class that the reference is basically pointing at, is how it will work. So in this particular table Type, what is your interface? What is your type? The attribute block is where's your instance data? The method block is where's your class? What's your implementation? And again, this is to be understood as simply conceptual So every reference has a type associated with it, every reference points to an object, which has instance data and is associated with a class that implements the methods Of the interface. So what happens here, Car is the interface. SportsCar is the class, is the implementation. What I can say is dictated by Car. How it works is dictated by Sports. Ford's car. So Java creates an entry in this conceptual reference table, enters the type as car gives it a name. Where in the heap is the data store the attributes are instance variables. Of the sports car and the class is the sports car, so that is going to be, how the interfaces implemented, that's the class. What if something doesn't make sense? What if I say car, my car equals new sports car, and then I try to access, the race method. By the compiler won't let you car doesn't have a race method only sports card does. So the compiler will simply say no, this is not an available option. You won't even get to the runtime. So Java has looked and said, nope, sorry, you can't do that. The method is not in the interface, the type of the reference When you're dealing with a single object, you might not think polymorphism is very important. But in the large it is one of the most important things we deal with in Java. here will be an example very similar to the one we've been doing all this time. We have an array of car or in our case now an array of vehicle And it's got different types, and we set their speed. And just like we've been doing in our example, sports cars go faster cargo vehicles goes slower depending on their cargo load. So that's why even though we set them all to fit 50 we get different results based on which ones are the speeders the sports cars and which ones are carrying a load the SUVs and station wagons. But we can only call methods that are defined On the type car, and of course, all of the things over here must implement that interface or we wouldn't have been allowed to even make the assignment. The compiler would have said, there's no is relationship here, you're not allowed to do that. The compiler has checked it for us. That's the one of the advantages of a strongly typed statically typed language. So best practices summarizing all of this design using Java interfaces. classes, inherit the recording Requirements from interfaces, all references members, local variables parameters should be defined in terms of an interface. What is the type Only refer to classes by name when you're referring to their constructor when using new, or there happens to be a thing on a class called new instance. It's a method. But either way, only refer to classes when necessary because you're needing to use its Constructor. Otherwise, everything should be written to interfaces. polymorphism is important and interfaces are the key to polymorphism in Java. So polymorphism we've spent much of the last several units on This. It's a powerful technique. It works with inheritance of interface in Java. Unless you just think about the single inheritance of class. It allows us to manipulate objects. By what they have in common but in common and Java requires an arrogance, which is why it's so important that we use interfaces.