Week 3 Part 2 Flashcards
When a parameter is passed to a method, it is known as
an argument
Classes that are compiled in the same directory are considered to be in the same _______
package
Most classes in Java programs need to be imported __________, by typing ______ followed by the class name
explicitly, import
think import java.utl
Classes that are in the same package, are they imported explicitly or implicitly?
Implicitly
For classes, what other situation is there where an import declaration is not required?
Not required if the class is referred to with a full qualified method name, including its package and class names.
Describe UML design. How many compartments? Where is the class name? Where are the class attributes? What are attributes? Where are the class operations? What are class operations?
3 compartments, class name in the top one. Class attributes in the second, which correspond to instance variables. Class operations in the third, which correspond to methods and constructors.
How are instance variables represented in UML?
-name:type
How are operations represented in UML?
A setter for example
+setName(name:type)
Apparently everything you learned in semester 1 is wrong. In UML do operations that don’t have a return type have a return value? (void)
Apparently not. If they do have return value it looks like this +getName():type
What happens if a class does not define constructors? What happens to the classes instance variables if this is the case?
The compiler provides a default constructor with no params. The instance vars are initialized to their default values
T/F - A default constructor will still be created if there is a declared constructor
False
T/F - Java requires a constructor call for every object that is created
True
T/F - Constructors specify return types
False
How does UML distinguish a constructor from class operations?
Places the word constructor between Guillemets “«_space;»” pronounced GILL MITTS
«constructor»Account(name:type)
How does a compiler select the appropriate method to call when there’s overloading? What is this called? Explain the mechanisms behind this function
Inspects the:
number of arguments
types of arguments
order of arguments
It’s called the Method signature
test (float, int)
test (int, float)
Will it work?
Yes, they’re in a different order
If 2 method have a different return type but the same arguments, will method overloading occur?
No, that would invalid method overloading
What are 2 advantages on method overloading?
Flexibility, readability
Used to implement an object in different ways
Constructor overloading
Like method overloading, how does the compiler select the correct constructor?
Inspects the methods signature
4 advantages of constructor overloading
- Can create new objects when you have differing amounts of data to pass
- Variety of way to create an object
- When adding a constructor, the compiler will not add a default no-arg constructor
- A constructor eliminates calling the normal or ordinary method implicitly
What is constructor chaining? Can you have constructor chaining with no overloaded constructors?
When a call to a constructor is made from inside another constructor in the same class. This makes constructor chaining w/ no overloaded constructors impossible.
3 benefits of constructor chaining
ACE
- Avoids code duplication
- Consistent Usage
- Ease of maintenance
What term do we use for constructor chaining? What is notable about it?
this(). Has to be the first line in the constructor.
Can every constructor utilize this()? What does it represent?
No, at least one must not have it. this() represents an object being constructed.
Is there is a restricted order to chaining?
No, chaining can be realized in any given order.