Exam Flashcards
(118 cards)
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