Chapter 10 Flashcards
What is polymorphism?
Polymorphism indicates that a something can have many forms.
A polymorphic variable means it can have different types of objects at different points in time. (i.e. the type can change during the execution of the program).
What is late binding?
If a method is called on a polymorphic variable, Java will check which method to invoke depending on the type the variable is currently referencing. If the type changes during the execution, Java will change the method that is invoked.
If a declared object is of type X, what other types could this object reference (run-time speaking)?
An object declared with a specific type could potentially reference any of type that is child of the original type declaration.
If X is a parent and Y is a child.
A X object could become a Y object during the program.
Example:
Holiday day;
day = new Christmas();
Suppose this situation:
Holiday day = new Christmas();
day.celebrate();
Of what type is the invoked method? Are there limitations?
The method called depends on the type of the object being referenced, not the reference type.
This means, that although day is declared as a day type, the method invoked is of type Christmas, as this is the type that is being referenced by the object.
CONDITION: The type of the object (not the one being referenced) must also implement the called method. Otherwise a compiler error is shown.
In other words, the celebrate method called in this case will be of type Christmas, but the celebrate method of type Holiday must also exist.
What does the following do?
(day is of Holiday type)
((Christmas)day).getTree();
In which cases is it used?
This statement calls on object day the getTree method of type Christmas. This will NOT raise an exception ONLY IF object day references a Christmas object.
This statement is used when we’re sure that the object will be of the type that implements such method.
If MusicPlayer is the parent of CDPlayer, which of the following assignments are valid?
MusicPlayer mplayer = new CDPlayer();
CDPlayer cdplayer = new MusicPlayer();
The first one is a legal statement, because CDPlayer is-a MusicPlayer. In the sense that mplayer can reference the child class even if it’s declared as a parent (i.e. generic) type.
The second statement is wrong. That’s because a CDPlayer cannot be a MusicPlayer. CDPlayer is a more specific type of MusicPlayer. When referencing a MusicPlayer, some instance data of CDPlayer may not be initiated.
How is overriding related to polymorphism?
When a child class overrides the definition of a parent’s method, TWO definitions of that method exist (one of the parent class and the other of the child class). The version that will be invoked depends on the type of the object that is referenced by the polymorphic reference variable.
If an object is declared with the type of an interface, what limitations does it have?
If an object is declared with the same type of an interface, the object can execute the methods of the type it’s referencing, only on the condition that such method is defined in the interface.
For example: Speaker special = new Philosopher(); //speaker is interface. special.pontificate(); //if pontificate() is not defined in the interface Speaker, a compiler error will occur.
What is selection sort about?
A way would be to find the smallest value in the list and then to swap it with the value in the first position. Then find the next smallest value and so on…
Does swapping of values require more than 2 variables?
To swap 2 values, 3 variables are required. One of which has to keep the value temporarily.
Look at this code:
public boolean equals (Object other)
return (lastName.equals(((Contact)other).getLastName()) …
Why do we need to cast the class Contact?
The class contact must be casted, because the method equals is an override of the method of an interface. To override the method we will have to use the same signature
What is insertion sort?
Its the strateg of putting any item directly into its proper place by analysing the array and eventually shifting the elements to make room for the new one.
What are the effieciencies of the Selection and Insertion Sort algorithms?
They have similar efficiencies, for an array of size n they do approximately n^2 number of comparisons.
What’s the difference between a linear search and a binary search?
A linear search scans every element of an array in turn.
A binary search is used in sorted arrays, it compares an element to understand what part of the two he should check in next.
Example:
If an array has values between 0 and 99. If the method looks for 56 and it finds the value 38, it will check only the numbers on the right of the array. As the value 56 cannot be less than 38 (and be on the left then).
What is a property in JavaFX?
A property is an object that holds a value. By binding properties together a value of one will automatically change the value of the other.