Java Exam II (3/21/20)
What happens in single inheritance?
The subclass is derived from 1 superclass.
What happens in multiple inheritance?
The subclass is derived from more than 1 superclass.
Example: Suppose that supRef is a reference variable of a superclass type. Moreover, suppose that supRef POINTS to an object of a subclass. - We can use an appropriate ____________________ on supRef and make a reference variable of the subclass point to the object. - On the other hand, if supRef does NOT point to a subclass object, and we use the cast operator on supRef to make a reference variable of the sublcass point to the object, then Java will throw a ____________________ indicating that the class cast is NOT allowed.
Cast operator, ClassCastException
Upcasting
Casting an instance of a subclass to a variable of a superclass.
Down casting
Casting an instance of a superclass to a variable of its subclass.
instanceof
Compares an object to a specified type
In early binding, a method's definition is ASSOCIATED with its invocation when the code is ____________________.
Compiled
In ____________________, 1 or more members of a class are OBJECTS of ANOTHER class type.
Composition
A process that combines the data and the operations on that data.
Encapsulation
Composition has what kind of relationship?
"Has-a" relationship
Inheritance has what kind of relationship?
"Is-a" relationship
If A is composition of B, we say that A (composition object) ____________________ or ____________________ B object.
"Owns-a" or "Has-a"
Abstract class
A class that cannot be instantiated.
Java interface
A collection of abstract behavior that can be adopted by ANY class WITHOUT being inherited from a superclass.
Protected member
A member that is accessible ONLY to the methods that belong to the SAME class or to the descendant classes.
Abstract method
A method that has no implementation.
An ____________________ is a class that is declared with the reserved word abstract in its heading.
Abstract class
An ____________________ is a method that has ONLY the heading, not the body.
Abstract method
ClassCastException
An exception thrown when an object variable is cast with an incompatible class.
A reference variable of a class can refer to either ____________________ or ____________________.
An object of its own class, an object of its subclass.
Composition
Another way to relate two classes.
Polymorphism
Assigning multiple meanings to the SAME method name along with the SAME argument list.
Binding
Associating or connecting a method definition with its invocation.
The inheritance mechanism is used to share common ____________________ among the ____________________ classes, where the interface mechanism is used to share common ____________________ among ____________________ classes.
Behavior, unrelated, code, related.
TRUE or FALSE All non-instantiable classes are abstract.
FALSE
Inheritance is an ____________________ relationship
Is-a
Cast operator
Performs an explicit type conversion; it is created by placing the desired result type in parentheses before the expression to be converted.
What implements late binding in Java?
Polymorphism
The term ____________________ means assigning multiple meanings to the same method. Also, it is implemented in Java using ____________________ binding.
Polymorphism, Late
Early binding
Where a method's definition is associated with its invocation when the code is compiled.
Late binding
Where a method's definition is associated with the method's invocation at execution time.
Constructor chaining
Where a whole chain of constructors can be called all the way up to the constructor Object if there is a long line of class descent.
Inheritance mechanism
Where stream classes are implemented in Java.
What happens if you don't use polymorphism?
You have to add new code in your program, and also rewrite existing code to accommodate the change.
In late binding, a method's definition is ASSOCIATED with its invocation at ____________________, i.e. when the method is ____________________.
Execution time, at runtime.
If we define a class and do NOT use the reserved word ____________________ to derive it from an existing class, then the class that we define is AUTOMATICALLY considered to be derived from the class ____________________.
Extends, object
TRUE OR FALSE? An object can be a subclass of another object.
FALSE Inheritance is only between classes.
True or False? Java uses late binding for methods that are private, final, or static.
False
Composition is a ____________________ relationship.
Has-a
____________________ and ____________________ are meaningful ways to relate 2 or more classes.
Inheritance and composition
The following are some facts about abstract classes: -An abstract class can contain ____________________, ____________________, a ____________________, and ____________________. - An abstract class CAN contain abstract ____________________. - If a class contains an abstract method, then the class MUST be ____________________. We can only ____________________ of an abstract class type. - We can instantiate an object of a subclass, BUT only if the subclass ____________________.
Instance variables, constructors, finalizer, nonabstract methods. Methods Declared abstract, declare a reference variable. Overrides the definitions of all the abstract methods of the superclass.
An ____________________ is a CLASS that contains ONLY abstract methods and/or named constants. - Java allows a class to implement more than one ____________________. This is the way to implement ____________________ in Java.
Interface, interface, multiple inheritance
What way does Java implement multiple inheritance?
Java allows a CLASS to implement more than one interface.
Except for a few special cases, Java uses ____________________ binding for all methods.
Late
What type of binding that Java uses for all methods?
Late binding
What is late binding and how is this related to Java's implementation of polymorphism?
Late binding is where a method's definition is associated with the method's invocation at runtime. Method called from reference variable will decide at runtime which version to call, depending on which subclass pointing to reference variable refers to objects of different points in time.
What happens during method overloading?
When the corresponding method in the superclass and the subclass has the same name but different parameters.
In ____________________, a subclass is derived from more than 1 superclass. Java does NOT support multiple inheritance, i.e. in Java a class can ONLY ____________________ the definition of 1 class.
Multiple inheritance, extend.
In a class hierarchy, several methods can have the same ____________________ and the same ____________________.
Name, formal parameter list.
Does Java support multiple inheritance?
No, Java does not support multiple inheritance. Each class is able to extend only on one class, but is able to implement more than one interfaces.
What class does the toString method come from?
Object class
What is the supreme class in Java?
Object class
The classes Scanner, Reader, and Writer are derived from the class ____________________. - The class ____________________ is derived from the class Reader. - The class ____________________ is derived from the class InputStreamReader. - The class ____________________ is derived from the class Writer.
Object, InputStreamReader, FileReader, PrintWriter.
In general, while writing the definitions of the methods of a subclass, in order to specify a call to a public method of a superclass, we do the following: - If the subclass ____________________ a public method of a superclass, then we specify a call to that public method of the superclass by using the reserved word ____________________ followed by ____________________ followed by ____________________ with an appropriate parameter list. - If the subclass does NOT override a public method of the superclass, we can specify a call to that public method by using ____________________.
Overrides, super, dot operator, method name, the name of the method and an appropriate parameter list.
The ____________________ members of a superclass are ____________________ to the superclass. The subclass cannot directly access them.
Private, private.
For a superclass to give a member access to its subclass and still PREVENT its direct access from outside the class, we must declare that member using the modifier ____________________.
Protected
A subclass can directly access this member of a superclass.
Protected member
- When class A contains ____________________ to instances of B & A ____________________ to those instances, we say that A is ____________________ of B.
References, controls all access, composition
Overriding method
Same header, different body
Overloading method
Same name, different arguments
What type of inheritance does Java support?
Single inheritance
Composition (____________________ form of aggregation)
Stronger
In single inheritance, the ____________________ is derived from ONLY one existing class, called the ____________________.
Subclass, superclass.
What keyword is used to call a constructor of the superclass?
Super
While writing the definition of a constructor of a subclass, a call to a constructor of the superclass is specified using the reserved word ____________________. - Moreover, the call to a constructor of the superclass MUST be ____________________.
Super, the first statement.
The class Object directly or indirectly becomes the ____________________ of EVERY class in Java.
Superclass
We CANNOT automatically consider a ____________________ object to be an object of a ____________________. In other words, we CANNOT automatically make a reference variable of a ____________________ type point to an object of a ____________________ type.
Superclass, subclass, subclass, superclass
Java allows us to treat an object of a subclass as an object of a superclass, i.e., a reference variable of a ____________________ type can point to an object of a ____________________ type.
Superclass, subclass.
A subclass can override the methods of a superclass, but this redefinition applies ONLY to the objects of the ____________________.
Superclass.
What happens when we define a class and don't use the keyword "extends" to derive it from an existing class?
The class is automatically derived from the Object class.
True or False? We CANNOT declare a method inside an interface to be either private or protected.
True
Aggregation (____________________ form of composition)
Weaker