Java Questions 17 - Class and Object Creation
- What are 3 ways to access a method?
Answer:
- Invoking a method declared in the same class.
- Invoking a method using a reference in the class.
- Invoking an inherited method.
- What do the words 1) construct, 2) create, and 3) instantiate mean when used interchangeably?
Answer:
They all mean, "An object is built on the heap."
- What is the value of a reference that has not had an object assigned to it?
Answer:
The value of the reference is Null.
- How can you determine if a method or variable is static?
Answer:
If you see a method invoked ( or a variable accessed) without the dot operator (.) , it means the method or variable belongs to the class where you see that code.
- What does the reference always refer to?
Answer:
The reference this always refers to the currently executing object.
- Can a private member be inherited from a subclass?
Answer:
When a member is declared private, a subclass cannot inherit it.
- Does a subclass know about the private methods in the superclass?
Answer:
A subclass is unaware of the private mtehods in the superclass.
- Why is default access called package access?
Answer:
A default member may be accessed only if the class accessing the member belongs to the same package,
whereas a protected member can be accessed (through inheritance) by a subclass even if the subclass is in a different package.
- What differentiates default and protected behavior ?
Answer:
Default and protected behavior differ only when we talk about subclasses.
If the protected keyword is used to define a member, any subclass of the class declaring the member can access it through inheritance.
- How would you decribe default access?
Answer:
Default access does not allow a subclass to access a superclass member unless the subclass is in the same package as the superclass.