Interface, Types, and Typecasting + Polymorphism Flashcards
When a class implements an interface, it promises to provide _______ the behavior published by that interface.
ALL
TRUE OR FALSE:
Interfaces cannot be instantiated
TRUE
Every field declaration in the body of an interface is implicitly ___________________________.
public static final
Methods in an interface are ____________
Abstract methods
multiple interfaces gives the illusion of _____________
multiple inheritance
The type of one object is converted to match the type of another object
reference
Type Casting
T or F
Casts can be applied to both primitive data types and object references
T
Types of Casting
Explicit Casting
char c = (char) i
Implicit Casting
float num = i
Widening vs Narrowing Conversion
Narrowing conversion may cause less of precision and range (float to int / superclass to subclass)
Widening conversion may cause increase in precision and range
(int to float / subclass to superclass)
In type casting objects, the cast must be to ________________________
subclass types, superclass types, or interfaces
determines the operations an object can perform.
Type
determines the implementation of its methods
Class
is a side effect of Inheritance
and Typing
Polymorphism
___________________ for Flexibility: helps us write code
that is easy to modify and extend.
Polymorphism
it is the ability of the programming language to handle objects differently depending on their data type or class.
Polymorphism
Parametric Polymorphism
Java Generics
Java ArrayList Methods
ArrayList <Integer> integers = new ArrayList<Integer>();</Integer></Integer>
integers.add();
integers.remove;
integers.get(index);
ingeters.size();
integers.isEmpty()
integers.addAll(Arrays.asList(1,2));
for (String s : stringList) //Iterate thru the elements
Keyword for using an interface
implements
Types of Polymorphism
True Polymorphism
- Subtype/Inclusion Polymorphism - A subclass of a superclass can be used as a substitute for its superclass.
- Parametric Polymorphism - classes are written so generically that their methods can handle values identically without depending on their types.
Pseudo/Ad hoc Polymorphism
- Method overloading gives the illusion of parametric polymorphism.
Example usage of Iterator Generic
Iterator <String> iter = stringList.iterator();</String>
while(iter.hasNext()){
Sysout(iter.next());
}
Java HashMap
Methods
HashMap <Integer, String> studRec = new HashMap <Integer, String>();
studRec.put(202365908, “Myko”);
studRec.get(202365908)
//Check if key exists
studRec.containsKey(202365908)
//Returns all the keys
studRec.keySet();
Narrowing conversions are possible if and only if the object came from a preceding widening
conversion.
TRUE