Kevins MCQs Flashcards
What is a direct superclass?
The superclass from which the subclass explicitly inherits.
What is an indirect superclass?
Any class above the direct superclass in the class hierarchy.
What is single inheritance?
Java supports only single inheritance, in which each class is derived from exactly one direct superclass.
Is-a and Has-A: Which represents inheritance and which represents composition?
Is-a represents inheritance - In an is-a relationship, an object of a subclass can also be treated as an object of its superclass – i.e. Lecturer is a Faculty member
Has-a represents composition - In a has-a relationship, an object contains as members references to other objects.
What are the members of a class?
Instance variables & methods.
What is difference between aggregation and composition?
In a Has-A relationship:
Composition is a strong relationship: A Composes B, means A contains and owns B.
Aggregration between A and B means, A contains but not owns B.
What is polymorphism?
When you define a supertype for a group of classes, any subclass of that supertype can be substituted where the supertype is expected.
What has an overloaded method got to do with inheritance and polymorphism?
Nothing.
What are the four access modifier levels?
Public, Protected, Default (Package-Private), Private.
Explain four levels of access modifiers:
Public (all classes can access), Protected(Classes in the same package as well as ‘extends’), Default (Classes in the same package), Private (just the class it is in).
What can interfaces contain (5 things)
Constants, method signatures, default methods, static methods, nested types. (Method bodies exist only for default and static methods).
Can interfaces be instantiated?
No, they can only be implemented by a class or extended by another interface.
What is the difference between a concrete and an abstract class?
An abstract class can not be instantiated. It is declared using the abstract keyword. A concrete class is on one that is not abstract.
How do you create an iterator for an Arraylist called employees with abstract superclass called SalesEmployee?
import java.util.Iterator;
Iterator itr = employees.iterator();
while (itr.hasNext()){
System.out.println(itr.next());
How do you print out toString of objects in an Arraylist called employees of type SalesEmployee, using an enhanced for loop?
for (SalesEmployee temp : employees) {
System.out.println(“Now: “ + temp);