Exam Flashcards
Is it legal to assign an object to a variable declared to be of the type of one of its superclasses?
Yes
Is it legal to assign an object to a variable declared to be of the type of one of its subclasses?
No
Are all methods abstract in an abstract class?
No
Can an abstract class include a constructor?
Yes
Can an abstract class be instanstiated?
No
Can an interface be instanstiated?
No
Can an interface include a constructor?
No
Are all methods in an interface abstract?
Yes
Is it legal to assign an object to a variable declared as the type of its superclass if the superclass is abstract?
Yes
Is it legal to suppy as a method’s actual argument an object which is a direct or indirect subclass of the type specified by the formal argument?
Yes
Is it legal to supply as a method’s actual argument an object whose class is a superclass of the formal argument?
No
Explain the concept of substitutability
Where a Java program expects an instance of a particular class, it is legal to substitute an instance of any of its direct or indirect subclasses.
sameColourAs(Amphibion)
Frog kermit = new HoverFrog(); ((HoverFrog) kermit).up();
What is the result?
The HoverFrog referenced by kermit will move up, and will NOT be cast to a HoverFrog reference variable.
Frog kermit = new HoverFrog();
HoverFrog h = (HoverFrog) kermit;
h.up();
What is the result?
The HoverFrog referenced by kermit will be cast and assigned to the HoverFrog reference variable h.
The HoverFrog now referenced by kermit and h will move up.
What is an interface?
An interface specifies a list of public abstract methods that a group of unrelated classes must implement so that their instances can receive a common set of messages.
Does a subclass of a class that implements an interface inherit the implementation?
Yes
Can interface methods return values?
Yes
If varA and varB are two variables, what needs to be true about their types if the statement
varA = varB;
is to compile?
If they are of the same type, or the variable on the right is a subtype of the one on the left, the assignment will work without a cast.
If varA and varB are two variables, under what circumstances can you cast a reference variable of one type to another type in an assignment?
The variable to which the cast is applied (the one on the right) must be of the same type as, or a supertype of, the variable on the left.
Explain the concept of polymorphism
A polymorphic message is a message understood by more than one class. Each class of object can behave in its own way in response to such a message. This occurs via overriding/interfaces.
Give an example of direct interaction between objects
frog1.sameColourAs(frog2)
frog1 sends a message to frog2 within the body of the method, which is then used to alter the state of frog1.
Give an example of indirect interaction
frog1.setPosition(frog2.getPosition());
A third party (e.g. OUWorkspace, coordinating object) is sending a message to frog2 and using the result to alter the state of frog1.
Can you use the keyword this in a class method?
No
Can you use the keyword super in a class method?
No
Give an example of static binding
The class method Collections.sort() can be resolved at compile time because it is not a message and therefore does not have a receiver.
Give an example of dynamic binding
The instance method anAmphibian.right() cannot be resolved at compile time because it could refer to one of several different right() methods depending on the class of the receiver
Are class methods and variables inherited?
Yes
Can an instance method override an inherited class method?
No - compilation error
Can an instance method override an inherited instance method?
Yes
Can a class method override an inherited instance method?
No - compilation error
Can a class method override an inherited class method?
No - it hides the inherited class method, which can still be accessed by qualifiying the method with the superclass name.
Is super(); always implied in a constructor?
Yes, if there is no explicit super() statement.
Is a zero-argument constructor always included by the compiler?
No - only when no other constructor is included.
Where should static variables be initialised?
Upon declaration
Where should final variables be initialised?
Upon declaration or in the constructor
Where should static final variables be initialised?
Upon declaration
What are the implications for declaring primitive data type variables and reference type variables as final?
For primitive data, the value assigned cannot be altered.
For references variables, the reference cannot be altered but the object it references can.
Are local variables given default values?
No
Are instance variables given default values?
Yes
Are static variables given default values?
Yes
True or false:
Methods that may throw a checked exception must either handle the exception or declare that it throws an exception
True
True or false:
Methods that may throw a unchecked exception must either handle the exception or declare that it throws an exception
False
Which classes of exception are checked?
Exception and its subclasses, excluding RuntimeException and its subclasses.
Which classes of exception are unchecked?
Error and its subclasses
RuntimeException and its subclasses
Explain the Design by Contract approach
Preconditions and postconditions are established before writing a method. The method is then written to guarantee that if the precondition is met, the postcondition will be guaranteed.
What does the Design by Contract approach require to happen if a method’s precondition is not met?
Throw an exception
How do you throw an exception?
throw new IllegalArgumentException(“debit “
+ anAmount + is inappropriate. Balance “
+ this.balance);