Presentation is loading. Please wait.

Presentation is loading. Please wait.

Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.

Similar presentations


Presentation on theme: "Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends."— Presentation transcript:

1 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, InheritanceClient 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.

2 Polymorphism What if you know you want to call a method, 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 instantiate it as an object of the child class. This is called polymorphism. Examples: 1) Vehicle x = new Car(); 2) List z = new ArrayList( ); 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.

3 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.

4 Open: – Animal – Dog – Cat – PolymorphismClient

5 Polymorphism is commonly seen when using ArrayList. ArrayList is a class that implements the List interface. The List interface is a blueprint for its “implementor” classes. There is another implementor of the List interface, called LinkedList, that you do not have to know about, but it helps to be aware of it. However, you do need to be aware of the List interface. Often, you will see ArrayLists declared this way: List x; And then, later on: x = new ArrayList(); Or, a later line of code might want to change x to be a LinkedList. If it had been declared as an ArrayList originally, this would be a problem. Note: we learned previously that an interface cannot be instantiated. In this example, we are not instantiating List; we are actually instantiating ArrayList.

6 Static variables Remember: a static method is a method that can be called from a client without having to create an object. Just use the name of the class itself (examples: Math.random(), SavitchIn.readLine() ). A variable can also be declared with the keyword static. Just like a static method, a static variable is not accessed through an object. It is accessed by using the name of the class itself. Why use a static variable? Sometimes, you want a variable to be shared among all instances of an object, as opposed to each object having its own version of that variable. A static variable is also known as a class variable. Example: see StaticMethodClass & StaticMethodClient

7 Arrays of Objects An array is not limited to holding just ints, or chars, or Strings, etc. An array can hold objects. Remember, when you connect to (instantiate) a class, you create an object, for example: Numbers x = new Numbers(); In this example, x is an object. Demo: Person, ArrayOfObjectsDemo

8 Types of errors There are 3 types of errors: – Compiler error: An error that the compiler will point out to you before you can run it – Run-time error: Will not be noticed by the compiler; will crash the program when it runs – Logic Error: does not cause either of the above errors, but makes your program run in a way you do not want it to

9 Restricting the values in an Arraylist By default, an ArrayList can store any type of Object. You can, if you wish, restrict the type of thing that an ArrayList can hold, by using the symbols. This is called using generics. Examples: ArrayList z = new ArrayList (); ArrayList x = new ArrayList (); List a; a = new ArrayList ();

10 Why use generics? Simply put, using generics will make your code more “stable” by catching more errors at compile time rather than at run-time. Compiler errors are usually easier to fix than runtime errors. When the compiler knows that an ArrayList is restricted to holding Strings, for example, it can check your code before running the program to find any errors that specifically involve using Strings. Another benefit of using generics in an ArrayList is that it eliminates the need for type casting. Example: ArrayList nums = new ArrayList (); nums.add(23); nums.add(11); // no need for “new Integer (11)” etc

11 Another example: public class Sample { private ArrayList ; // more code here } In this class, a private instance variable is created – it is an Arraylist. But note that it will NOT hold objects of the Comparable interface, because that is not valid. An interface cannot be instantiated. Instead, it will hold objects that implement the Comparable interface.

12 (ItchyAndScratchy) – Create an 11-by-11 board which is filled with dashes. – Create a class, ItchyClass, containing one method, DisplayBoard. This method should receive the board as a parameter, display it, and return nothing. – Randomly place an I and an S on the board. – Allow the user to control the I (Itchy) by typing L, R, U, or D, for each of the 4 directions. This will move the I one space on the board. – When Itchy catches Scratchy, the game is over. Display how many moves it took. – Allow the option to play again. – Optional: Allow the user to choose a difficulty level, in which Scratchy (the computer) can move too.


Download ppt "Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends."

Similar presentations


Ads by Google