• 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

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/41

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

41 Cards in this Set

  • Front
  • Back
  • 3rd side (hint)
Instance variables can have which access controls?
public, private, protected, or default?
All of the above
Instance variables can have any access control and can be marked final or transient
Instance variables can be marked transient or _______. (non-access modifier)
Final
Instance variables can be marked final or transient
Instance variables can NOT be synchronized, native, strictfp, or _______

extra credit: why?
abstract
abstract keyword can only be applied to methods and classes
Shadowing is when the name of a local variable matches the name of an instance variable. Is it legal to do that?
Yes
but it's very bad and can create some hard to find bugs
Besides strictfp, what are the other non-access modifier keywords you can use for your classes?

extra: when would you use each?
final and abstract
final when you don't want the class to be subclassed

abstract when you want to force the class to be subclassed by anyone who wants to use it. It also can't be instantiated.
What is wrong with this:
public final abstract class Foo{ }
A class can't be final. You would be saying that you have to subclass the class that can't be subclassed. compiler error ensues
Which kind of class can't be subclassed?
final
which class can't be instantiated?
abstract
What is the effect of a class having at least one final method?
nothing, this is a trick question. if you said that the whole class must be marked final, shame on you
seriously, this is the kind of tricky stuff that will get you on the exam
what is the effect of a class that has at least one abstract method?
it must be marked abstract or you will get a compiler error
make sure you know how to spot a real abstract method:
public foo();
and one that only looks abstract because it seems like it doesn't have a body:
public foo(){}
curly braces = method body despite its emptiness

Why is this legal:
abstract class Foo(){
void bar();
void baz(){}
}
Because abstract classes can have both abstract and non-abstract methods
if you said both methods were abstract, note the curly braces on method baz, that means it has a body and is not abstract.
if class Bar extends abstract class Foo, Bar must do what to be considered a concrete class?
It must implement all of Foos abstract methods or else Bar is a de facto abstract class
If Bar doesn't implement the methods or doesn't get marked abstract you will get a compiler error.
Interfaces are contracts for what a class CAN do, but doesn't tell you what?
Interfaces don't say HOW a class must do something
example: an interface with an addUser method only says that a class that implements the interface will addUser but doesn't say how it will do it. Will it add it to a list and return the list, or will it add it to a string builder and print the string? you'll just have to find out.
Interface Bouncy has methods for a class to bounce.

So in your code you see class Ball (which implements Bouncy) that is a subclass of class Toy which implements Bouncy.

you also see class Tire (which also implements Bouncy) but is a subclass of class AutoPart

Is this Madness???
No! This. IS. JAVA!!!!!!! *kick*
Even though ball and tire are from different inheritance trees, they can still implement the interface
interfaces can be implemented by any class from any inheritance tree
Interfaces are really just purely abstract classes. What does that even mean? Also can you use the interface and abstract keyword together?
It means that interfaces are 100% abtract classes since none of their methods are allowed to be concrete. Of course you can use interface and abstract keyword together.
Not only is it legal to use the abstract keyword when declaring an interface, but if you don't do it, the compiler will
Interfaces can only have one kind of method. For this reason they have two keywords declared by default (implicit, you know added by compiler if you don't)
They can only be abstract. So they are always declared public and abstract.
whenever you see an interface method imagine (public abstract) in front of it, so if you see:
final void foo();
or
private void foo();

you will see them as
(public abstract) final void foo();
and
(public abstract) private void foo();

That makes the error more obvious when you see the confilcting keywords.
Interface constants are always (read: implicitly)
1)
2)
3)
declarations are option in any order
1)public
2)static
3)final

extra credit: any programmer using constants in an interface should be considered bad at software design
static imports should always be considered as a replacement for interface constants.

Placing constants in an interface was a popular technique in the early days of Java, but now many consider it a distasteful use of interfaces, since interfaces should deal with the services provided by an object, not its data. As well, the constants used by a class are typically an implementation detail, but placing them in an interface promotes them to the public API of the class.
When a concrete class implements an interface there are about 5 things to look for to ensure it is legal. Name as many as you can and imagine how each of those will look in code.
1) it must have concrete implementations of ALL abstract methods

2)follow legal override rules for those methods

3)Avoid declaring any new checked exceptions for those methods (non-interface methods are fine though)

4)Avoid declaring exceptions that are broader than the exceptions in the interface

5)Maintain the signature and return type of the methods it implements, any other combo is an overloaded method and doesn't have any of the above restrictions.
exam gotchas:
1) the code will have the class implement a method without code between the braces so you think its abstract.

2) watch for any code that violates override rules for arg list, return type, access level, final and static keywords, unchecked exceptions, and constructors

3)code will either have a concrete method with unchecked exceptions (which is fine) or have an overloaded method (meaning this isn't an interface method) with checked exceptions.

4)the code will declare exceptions that are more narrow to trick you into thinking it would generate an error

5) the code will have a huge list of methods, some are implementations and some are overloaded so you have to keep track of slight variations in the return type or a type in the argument list.

Also, a different variable name in the argument list does not count as a different argument list.

public void foo(String u){} is the same as
public void foo(String s){} is the same as
public void foo(String a){} mkay?
Local variables may have wihich access modifiers?
none. ANY local variable declared public, private, or protected is illegal
Seriously, think about the life span of a local variable and think about what access modifiers do. Now you see why it would be silly to try to make a local variable public.
What's the only modifier that a local variable can have.

extra credit: why is it good?
final
extra credit: why is it good to mark a local variable final? it expresses intent and can prevent some sly bugs in your code. google "java final local variables" to find a lively discussion on the subject.
What is wrong with this?
public int foo(int x){
int i;
i = x*2;
return i;
}
Local variable was not initialized.

not to mention very bad method design.
Local variables don't get default values when declared, so declaration and initialization must be inline.
The "this" keyword (as is this.name or this.whatnot) refers to what?
The current object.
class OneNumber {
int n;
void setValue (int n) {
this.n=n;
};
}
The only code that can see or access private members is...
the code in its class
Are private members invisible to subclasses?
Yes, so they can't be inherited.
What's the difference between default and protected keyword?
default = package level
protected = package level plus subclasses regardless of package
NOTE: subclasses can NOT see protected members of its super class and can NOT see protected members of other subclasses
Encapsulation helps hide _______ behind an interface or API
implementation
Encapsulated code has two features:
1) Instance variable are ______
2)_____ and _____ methods provide access to instance variables
1) protected
2) Getter and Setter

note:
Although, if your getter is public, how protected is your class from exposing its implementation from other classes?
IS-A refers to _________ or __________
inheritance or implementation

note:
This means that if a class extends another class or implements an interface it has an IS-A relationship
IS-A is expressed with the keyword _____
extends
True/False

"inherits from" and "is a subtype of " are all equal expressions
True!
HAS-A means that an instance of one class "has a" ______ to an instance of another class or the same class
reference
Inheritance allows a class to be a subclass of a superclass. What does it inherit and what are their access modifiers?
public and protected
variables and methods
________ is a key concept that underlies IS-A, polymorphism, overriding, overloading, and casting.
Inheritance
ALL classes (except class Object), are a subclass of type Object. So EVERY class inherits what from Object?
Object's methods.

note:
extra credit if you can name them. Not now, though...later.
Polymorphism means _____ ______

(hint: not the concept, but what is the literal Greek translation)
πολλές μορφές = polus morphē =
many forms
A reference variable is ALWAYS of a single, unchangeable type, but it CAN refer to what?

(what type of object)
a subtype object
A single object can be referred to by reference variables of many different types as long as they are the same what?

(types)
type or supertype
Does the reference variable's type determine which methods can be called or is it the object's type?
The reference variables

example:
class X{void doXStuff(){}}
class Y extends X {void doYStuff}}

X x1 = new Y();
x1.doYStuff();

won't compile. x1 is a Y object but the reference type is an X.

You'd have to cast it.
And THIS is NOT a proper cast: (Y)x1.doYStuff(); //generates compiler error

PROPER CASTING:
((Y)x1).doYStuff();
Polymorphic method invocations apply ONLY to overridden ______ methods.
instance
Methods can be overridden or overloaded, but constructors can only be...
overloaded.


note:
why not overridden?

constructors aren't inherited, so they have to provide their own constructors. Even the default, no-arg constructor the compiler provides still has to call the superclass's constructor manually.

so:
class A{
public A(){}
}
class B extends A{
public B(){
}
}

B's constructor isn't overriding A's constructor, since
edit later
okay