CMSC132 - Final Exam Review
Subclasses - answer- inherits all properties, behaviors, and fields of its superclass
- inherits private fields and methods in its super classes, but it cannot directly access
them
- can have more methods than its superclass
- classes can only extend one superclass
- subclass is a type of a superclass object
- constructor automatically calls its superclass's default constructor (error if it doesn't
have one)
- can explicitly invoke its superclass constructor using super
- execution order: most parent to most sub
- can redefine (override) methods in a superclass, but must have the same name and
signature
- can call an overridden method in a superclass using super( )
- can define a field with the same name as a superclass field
Super classes - answer- can refer to an object of any of its subclasses
object-name instanceof classname - answer- returns true if object is of the type
classname or if is an object of any of its subclasses
object-name.getClass() - answer- returns a reference to something called a class object
(unique object for each class in program), which can be compared using == or equals()
to see if object is the same class as another object
Dynamic binding - answer- the type of a object that a superclass reference refers to
determines which method gets called (when methods are overridden)
- DOES NOT APPLY TO STATIC METHODS
Final - answer- methods cannot be overridden by subclasses
- classes cannot be extended (no subclasses)
The Object class - answer- the (direct/indirect) superclass of all other classes in Java
- doesn't have fields, but it does have methods (equals( ) and toString( ))
Interfaces - answer- A collection of:
~ Constants
~ Method signatures (or prototypes), but no implementations
- a class can only implement one interface at a time
- Restriction of interfaces: CAN'T INSTANTIATE AN INTERFACE
- Reference of an interface type object - an object of any class that implements the
interface
, Composition - answer- using instance variables that refer to other objects in a class
Initialization block - answer- used to initialize fields of a class
- code that is executed once per object
Static initialization block - answer- code that is executed once for each class used while
program is running
When can fields be initialized? - answer- at point of declaration
- in a constructor
- in an initialization block
What is the order of field initialization? - answer1. Declarations and initialization blocks
(in the order they appear)
2. Constructor
Comparable Interface - answer- In a class implementing it, A.compareTo(B) should
return a negative value if A < B, 0 if A == B, and a positive value if A > B
- an array/ArrayList containing elements of a class that implements Comparable can be
sorted using Arrays.sort( ) or Collections.sort( ) (respectively)
Abstract classes - answer- classes with abstract methods must be abstract ("abstract" in
class heading)
- cannot be instantiated, but subclasses can extend an abstract class
- subclasses provide implementation for all abstract methods
- final methods cannot be overridden by subclass methods
- concrete object (of a non-abstract subclass) can be created, and referred to by a
reference of an abstract superclass type
What happens when an abstract class implements an interface? - answer- It's possible
for the abstract class to not implement all of the interface's methods
Types of Errors - answer- Compile-time (syntax) errors
- Runtime errors
- Logic Errors
Compile-time (syntax) Errors - answer- Errors in code construction, such as
typographical, grammatical, or type mismatch problems
Runtime Errors - answer- Operations that are illegal or impossible to execute
- Treated as exceptions in Java
Logic Errors - answer- These are operations leading to an incorrect program state
- They represent a problem in the design or implementation of the algorithm used
Subclasses - answer- inherits all properties, behaviors, and fields of its superclass
- inherits private fields and methods in its super classes, but it cannot directly access
them
- can have more methods than its superclass
- classes can only extend one superclass
- subclass is a type of a superclass object
- constructor automatically calls its superclass's default constructor (error if it doesn't
have one)
- can explicitly invoke its superclass constructor using super
- execution order: most parent to most sub
- can redefine (override) methods in a superclass, but must have the same name and
signature
- can call an overridden method in a superclass using super( )
- can define a field with the same name as a superclass field
Super classes - answer- can refer to an object of any of its subclasses
object-name instanceof classname - answer- returns true if object is of the type
classname or if is an object of any of its subclasses
object-name.getClass() - answer- returns a reference to something called a class object
(unique object for each class in program), which can be compared using == or equals()
to see if object is the same class as another object
Dynamic binding - answer- the type of a object that a superclass reference refers to
determines which method gets called (when methods are overridden)
- DOES NOT APPLY TO STATIC METHODS
Final - answer- methods cannot be overridden by subclasses
- classes cannot be extended (no subclasses)
The Object class - answer- the (direct/indirect) superclass of all other classes in Java
- doesn't have fields, but it does have methods (equals( ) and toString( ))
Interfaces - answer- A collection of:
~ Constants
~ Method signatures (or prototypes), but no implementations
- a class can only implement one interface at a time
- Restriction of interfaces: CAN'T INSTANTIATE AN INTERFACE
- Reference of an interface type object - an object of any class that implements the
interface
, Composition - answer- using instance variables that refer to other objects in a class
Initialization block - answer- used to initialize fields of a class
- code that is executed once per object
Static initialization block - answer- code that is executed once for each class used while
program is running
When can fields be initialized? - answer- at point of declaration
- in a constructor
- in an initialization block
What is the order of field initialization? - answer1. Declarations and initialization blocks
(in the order they appear)
2. Constructor
Comparable Interface - answer- In a class implementing it, A.compareTo(B) should
return a negative value if A < B, 0 if A == B, and a positive value if A > B
- an array/ArrayList containing elements of a class that implements Comparable can be
sorted using Arrays.sort( ) or Collections.sort( ) (respectively)
Abstract classes - answer- classes with abstract methods must be abstract ("abstract" in
class heading)
- cannot be instantiated, but subclasses can extend an abstract class
- subclasses provide implementation for all abstract methods
- final methods cannot be overridden by subclass methods
- concrete object (of a non-abstract subclass) can be created, and referred to by a
reference of an abstract superclass type
What happens when an abstract class implements an interface? - answer- It's possible
for the abstract class to not implement all of the interface's methods
Types of Errors - answer- Compile-time (syntax) errors
- Runtime errors
- Logic Errors
Compile-time (syntax) Errors - answer- Errors in code construction, such as
typographical, grammatical, or type mismatch problems
Runtime Errors - answer- Operations that are illegal or impossible to execute
- Treated as exceptions in Java
Logic Errors - answer- These are operations leading to an incorrect program state
- They represent a problem in the design or implementation of the algorithm used