1. Declarations & Access Control Flashcards
Does a supercalss know anything about the subclasses that inherit from it?
No
Is a subclass free to override instance variables and methods define in a superclass?
Yes
Does an interface method have to be implemented?
Yes
Can an _ be used at the beginning of an identifier?
Yes
Is this a legal identifier:
:b
No, identifiers cannot start with a colon
Is this a legal identifier:
-d
No, identifiers cannot begin with a -
Can identifiers begin with a number?
No
Can # be part of an identifier?
No
Where can a public method be accessed from?
Anywhere
Where can a private method be accessed from?
Only within that class in which it is declared.
How many public classes per source file can there be?
1
What must come first, package statements or import statements?
Package statements
How many non-public classes can a source file have?
Any number
What is the syntax for static imports:
import static…
static import…
import static…
Can you use an import statement to search through the java API such as ‘import java.*’
No, this is legal but it will NOT search across packages
What are the 3 access modifiers?
Public
Private
Protected
What are the three nonaccess modifiers you need to know for the OCA exam
strictfp
final
abstract
What is the default access modifier that is not typed?
default
Can you have private classes?
No
Can you have protected classes?
No
What is the visibility of a class with default access?
Within same package only
If a class is public, yet in a different package, do you still need to have an import statement to gain access to that class.
Yes
Should a class ever be marked as both final and abstract?
No
What is strictfp
a non-access modifier that can modifiy a method or a class
What does making a class final mean?
It can never be subclassed ie String class
What is the purpose of an abstract class?
To be extended (hence why it can’t be both final and abstract)
What is the correct syntax for abstract methods located within an abstract class?
public abstract void goFast();
public abstract void goFast() {};
public abstract void goFast();
nb abstract methods end in a semi-colon
If a method is marked abstract, does the class also need to be marked abstract?
Yes
Can abstract classes have non-abstract methods?
Yes, these usually have implementations that shouldn’t change
Can a class be marked as both abstract and final?
No
What has to happen to classes which implement an interface?
They must implement the methods from the interface.
What are all interface methods implicitly?
public and abstract
Do you have to declare interface methods as public and abstract?
No, they are implicitly so.
What must all class methods that are implemented from interfaces be declared as?
public
Can interface methods be static?
No
Does an interface extend or implement other interfaces?
Extend
Can an interface extend a class?
No
Is this legal?
public abstract interface Rollable { }
Yes, but typing abstract is redundant
Is this legal in an interface? void bounce ();
Yes
Is this legal in an interface?
final void bounce ();
No, final and abstract cannot be used together, and abstract is implicit
Can interface methods be protected or private?
No, always public implicitly
Is this legal in an interface?
static void bounce ();
No, interfaces define instance methods not static methods
Can you put constants in an interface?
Yes
What must constants be (implicitly) if declared in an interface?
public static final
interface Woo {
int BAR = 55;
}
Is this BAR public, static and final?
Yes, it is implicitly done so because it is in an interface.
Can the values from constants from an interface be changed?
No.
What are methods and instance variables collectively known as?
Members
What can you modify memebers with - access or non-access modifiers?
Both
How many access modifiers can you use on members?
All four
Zoo z = new Zoo();
How can one get access to a member from Zoo?
z.memberName();
class Moo extends Zoo {…
How can one get access to a member from Zoo?
memberName();
What does the public access modifier mean is applied to members?
Can be access by all other classes regardless of package in which they belong
private String doRooThings(){}
Where can this method be access from?
Only from within the class in which it was declared
Can a subclass inherit this method from its superclass?
private int giveMeNumbers();
No, it is invisible to the subclass.
What happens if both a superclass method and the subclass method are the same, but the superclass method is private?
The subclass method is not overriding the superclass method because it is invisible to the superclass
What is the difference between these two methods?
void testIt(){} protected void testingIt(){}
void testIt(){} - default access, only accessible if class is in same package
protected void testingIt(){} - protected access, can be accessed by classes outside of same package only through inheritance i.e. extending a class