Review static methods A static method can be
Review: static methods • A static method can be called from a client without having to create an object. • It is called using this syntax: Class. Name. method. Name( ); • What static methods have we used? • When writing a static method, use the static keyword after the visibility modifier. Example: public static double get. Pi( ) { return 3. 14; }
Method Overriding • Remember inheritance: when a child class inherits methods, variables, etc from a parent class. • Example: public class Dictionary extends Book • Open Book, Dictionary, Inheritance. Client • Note that a client that creates a Dictionary object can actually call a method that only exists in the Book class. • What if a method exists in both the parent and the child, but has the same exact name? • This is called method overriding. The child class overrides its parent’s method, which allows it to accomplish a similar task, but in its own way. • Note: a static method cannot be overridden.
Rules for Method Overriding • The rules for method overriding are similar to those for method overloading: – It IS valid to have 2 overridden methods with the same name, but different parameters (parameter type, amount, or order) – It is NOT valid to have 2 overridden methods with the same name and parameters, but different return types – Unlike with method overloading, you can override a method and use the exact same name and parameters.
public class Parent { public void say. Hi( ) } { code here } public class Child extends Parent { public void say. Hi( ) { code here } // valid public void say. Hi(int x) { code here } // valid public int say. Hi( ) { code here } // invalid! }
Polymorphism • What if you know you want to call a method that is overridden, but you are not sure yet if you want to call it from the parent or from the child? • In this case, you can create an object of the parent class, but have it hold a reference to an object of any of its child classes. This is called polymorphism. • Examples: 1) Vehicle x = new Car(); 2) List z = new Array. List( ); 3) Vehicle y; y = new Truck(); • Notice that in each of these examples, the order of declarations goes parent child. Never the reverse. • When an overridden method in one of those classes is then called, how does Java know which one to actually call? Java decides this at run-time. This run-time decision by Java is called dynamic binding (or dynamic method binding).
• Dynamic Binding is very different than the process that happens when overloaded methods are called. • When an overloaded method is called, the decision about which method to call is made at compile time. (This is called static binding. ) • When using polymorphism, the compiler only decides if an overridden method can be called, but the actual decision about which method to call cannot be made until runtime.
• Polymorphism can technically be defined as a mechanism for selecting the correct method for an object in the class hierarchy. • What is the point of polymorphism? • Often, it makes sense that subclasses (child classes) of a superclass (parent class) can define their own unique behaviors, and yet share some of the same functionality of the parent class. • The programmer may wish to call the parent’s method, or one child’s method, or even a different child’s method. (Remember, these are all methods with the same name. ) So, polymorphism allows that choice to be made later on.
• Open: – Animal – Dog – Cat – Polymorphism. Client
• So, let’s say we have the following line of code: Animal k = new Dog(); • Or, we have this: Animal k; k = new Dog(); • Imagine that you write 100, 000 lines of code, then decide that from now on, you want k to be a Cat object – because there are methods in Cat that do not exist in Dog. • Or, what if the Cat class did not even exist before? You could create it now, and it would not be a problem. • There is no need to go back and change any code. Simply do this: k = new Cat(); • And now, k is a cat object, with no need to change/fix any code. This is one reason that polymorphism is so useful.
public class Parent { public void say. Bye( ) { code here } } public class Child extends Parent { public void say. Hello( ) { code here } } In a client: Child x = new Child( ); x. say. Bye( ); // this is valid. The child inherited the method. Parent y = new Child( ); // this is valid. Polymorphism. y. say. Hello( ); /* this is NOT valid. y is a Parent object, not a Child object. It can only call methods in Parent. */
• What if we add the following code? What would display? A obj = new A( ); obj. show( );
What is this called?
Assignments • In the 2009 Test, Section I, complete #26 -36, and #38. • ALSO, in Section II (the written code section), complete #1 and #3. • Save all answers in a Word doc called Day 2_2009_Exam_Answers. • Due tonight at midnight by email. • Continue working on Battleship.
- Slides: 14