Study your flashcards anywhere!

Download the official Cram app for free >

  • Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

How to study your flashcards.

Right/Left arrow keys: Navigate between flashcards.right arrow keyleft arrow key

Up/Down arrow keys: Flip the card between the front and back.down keyup key

H key: Show hint (3rd side).h key

A key: Read text to speech.a key

image

Play button

image

Play button

image

Progress

1/56

Click to flip

56 Cards in this Set

  • Front
  • Back
What is a key benefit of encapsulation?
the ability to make changes in your implementation details behind a "public" programming interface
What is your code's API?
the set of accessible methods your code makes available for other code to call
How do you make your design include encapsulation?
1. Keep your instance varables protected (with an access modifier, often "private")
2. Make "public" accessor methods, and force calling code to use those methods
3. For the methods, use the JavaBeans naming conventions of set<someProperty> and get<someProperty>
What do you call the methods that others must go through to access your instance variables?
getters and setters
or
accessors and mutators
You express the IS-A relationship in Java through the keyword _____.
extends
Wher does a class' arrow point towards in an inheritance tree?
towards the class it extends
What does it mean to say class A HAS-A B?
if code in class A has a reference to an instance of class B.
_____ can be overloaded or overridden but ______ can be only overloaded.
Methods
constructors
What do overloaded methods and constructors let you do?
Use the same method name (or constructor) but with different argument lists.
What does overriding let you do?
redefine a method in a subclass, when you need a new subclass-specific behavior
What kind of method can you not override?
a final method
What kind of methods are you forced to override?
abstract methods
What does the compiler look at, reference type or instance type?
reference type
______ lets you use a more abstract supertype (including an interface) reference to refer to one of its subtypes (including interface implementers)
polymorphism
Can an overriding method have a more restrictive access modifier than the method overridden?
No
Give the 8 rules for overriding a method.
1. The argument list must exactly match that of the overridden method
2. The return type must exactly match that of the overridden method
3. The access level must no be more restrictive than that of the overridden method
4. The access level can be less restrictive than that of the overridden method
5. The overriding method must not throw new or broader checked exceptions
6. The overriding method can throw narrower or fewer exceptions
7. You cannot override a method marked final.
8. If a method can't be inherited, you cannot override it
How can you take advantage of some of the code in the superclass version of a method, yet still override it to provide some additional specific behavior?
Run the superclass version of the method, then come back and finish with subclass additional method code.
What do overloaded methods let you do?
lets you reuse the same method name in a class, but with different arguments (and optionally a different return type)
What are the 5 rules for overloaded methods?
1. Must change the argument list
2. Can change the return type
3. Can change the access modifier
4. Can declare new or broader checked exceptions
5. A method can be overloaded in the same class or in a subclass
Which overridden method to call is decided at ______ based on object type, but which overloaded version of the method to call is based on the reference type at ____ time.
runtime
compile
Is the choice of which overloaded method to call dynamically decided at runtime?
No, it is based on the reference type passed at compile time
Can a method be both overloaded and overridden?
Yes
You cannot make a new object without invoking _________.
its constructor and the constructor of each of its superclasses
Does every class, including abstract classes, have to have a constructor?
Yes
A constructor has no _____ and its name must exactly match ____.
return type
the class name
What is the typical use of constructors?
to initialize instance variables
Can constructors use any modifier?
Yes, including private
Is it legal to have a method with the same name as the class but it isn't a constructor?
Yes - but stupid
Note: if you see a return type, it's a method rather than a constructor
What happens if you don't type a constructor into your class code?
A default constructor will be automatically generated by the compiler.
What is true about the default constructor?
It is always a no-arg constructor
If you type a constructor with arguments, will you also have a no-arg constructor?
No - not unless you type it in yourself
What must be the first statement of a constructor be?
A call to an overloaded constructor (this()) or a call to the superclass constructo (super()).
What if you type in a constructor and you do not type in the call to super()?
the compiler will insert a no-arg cal to super() for you
If you type in your own no-arg constructor, will it be the default constructor?
No - the default constructor is the one the compiler provides
Can you call an instance method or access an instance variable before the super constructor runs?
No
What are the only types of variables and methods that can be accessed as part of the call to "super()" or "this()"?
only static variables and methods
Do abstract classes have constructors?
Yes - they are always called when a concrete class is instantiated
Do interfaces have constructors?
No - they are not part of an object's inheritance tree
What is the only way a constructor can be invoked?
from within another constructor
How does the default constructor look?
1. it as the same access modifier as the class
2. it has no arguments
3. it includes a no-arg call to the super constructor
What must you do if your superclass does not have a no-arg constructor?
You must type a constructor in your class (the subclass) because you need a place to put in the call to super with the appropriate arguments
Can constructors be overridden?
Can constructors be overloaded?
No - because they are not methods
Yes
What does it mean to overload a constructor?
Typing in multiple versions of the constructor each having a different argument list.
How are overloaded constructors used?
To provide alternate ways for clients to instantiate objects of your class
The first line in a constructor must be a call to ______ or a call to ______.
"super()"
"this()"
Can a constructor have both a call to "super()" and a call to "this()"?
No, because each of these must be the very first statement in a constructor
What is the benefit of having one constructor invoke another constructor?
to avoid code duplication
Can you change the return type for an overloaded method?
Yes - because an overloaded method is a completely different method from any other method with the same name - of course you must change the argument list
Can overriding methods change the return type?
No - they must match the inherited version exactly
Can you return "null" in a method that has an object reference return type?
Yes
Is an array a legal return type?
Yes
In a method with a primitive return type what can you return?
any value or variable that can be implicitly converted to the declared return type
In a method with a primitive return type can you return a value or variable that can be explicitly cast to the declared return type?
Yes
Can you return anything from a method with a "void" return type?
No
In a method with an object reference return type, what can you return?
any object type that can be implicitly cast to the declared return type
What kind of object can be returned from methods that declare an abstract class or interface return type?
an object that passes the IS-A test can be returned