Podcast Beta
Questions and Answers
Which of the following is a benefit of inheritance?
A class is an instance of an object.
False
What keyword is used in Java to establish an inheritance relationship?
extends
The visibility modifier that is accessible only by the class it belongs to is called ______.
Signup and view all the answers
Which of these terms refers to a new class derived from an existing one?
Signup and view all the answers
Overloading allows a child class to change the definition of an inherited method.
Signup and view all the answers
What does the 'super' keyword do in Java?
Signup and view all the answers
Match the following concepts with their definitions:
Signup and view all the answers
What does the final modifier do in a method?
Signup and view all the answers
Inheritance is transitive, meaning inherited traits are continuously passed down.
Signup and view all the answers
What is an abstract method?
Signup and view all the answers
A class that cannot be instantiated and serves as a placeholder is called an _____ class.
Signup and view all the answers
Match the following concepts with their definitions:
Signup and view all the answers
What is the purpose of overriding the toString() method?
Signup and view all the answers
Siblings in object-oriented programming hold the relationship of inheritance between each other.
Signup and view all the answers
What are the two types of polymorphism?
Signup and view all the answers
What is dynamic binding also known as?
Signup and view all the answers
A search pool is primarily used for sorting items.
Signup and view all the answers
What does the compareTo method do?
Signup and view all the answers
Polymorphism allows an interface reference variable to refer to an object of any class that implements the ______.
Signup and view all the answers
Match the following definitions with their correct terms:
Signup and view all the answers
What does casting do in the context of method invocation?
Signup and view all the answers
Sorting is the process of organizing a list of items in random order.
Signup and view all the answers
Describe the strategy used in selection sort.
Signup and view all the answers
What is the purpose of binary search?
Signup and view all the answers
An array must be sorted for binary search to work.
Signup and view all the answers
What happens if a program does not handle an exception?
Signup and view all the answers
A ___ is an object that defines an unusual or erroneous situation.
Signup and view all the answers
Match the following terms with their definitions:
Signup and view all the answers
Which of the following scenarios may cause an exception to be thrown?
Signup and view all the answers
An error in Java is recoverable and can be caught.
Signup and view all the answers
What does the first line of a call stack trace indicate?
Signup and view all the answers
What is the primary purpose of the try block?
Signup and view all the answers
The catch block is used to define a block of code that executes only if an error occurs in the try block.
Signup and view all the answers
What happens if no exception is thrown in a try-catch statement?
Signup and view all the answers
The ______ is executed no matter how the try block is exited.
Signup and view all the answers
Match each term with its correct description:
Signup and view all the answers
What does the getMessage method do?
Signup and view all the answers
The reserved word throw is used to start exception propagation.
Signup and view all the answers
What is exception propagation?
Signup and view all the answers
Study Notes
What is a class?
- A blueprint for an object.
- Represents the concept of an object.
Inheritance
- A fundamental object-oriented design technique used to create and organize reusable classes.
- Creating new classes from scratch is expensive, inheritance makes it cheaper.
- Process of deriving a new class (child/subclass) from an existing one (parent/superclass/base class).
- Child class automatically "inherits" variables and methods of the original class.
Depicting Inheritance Relationships
- Use a solid arrow pointing to the parent class.
Inheritance Relationships
- Create an "is-a" relationship.
Benefits of Inheritance
- Software reuse: capitalize on already existing software components and saves space.
Visibility Modifiers
- Public: Visible everywhere.
- Protected: Accessible within its own package and by subclasses in other packages. Provides more encapsulation than public, but less than private.
- Private: Member can be accessed only by its own class.
The "super" keyword
- Refer to the parent class.
- Often used to invoke the parent's constructor.
Method Overriding
- A child class can override the definition of an inherited method in favor of its own.
Method Overloading
- Involves multiple methods with the same name, but different signatures, within the same class.
The "final" Modifier
- Prevents a method from being overridden.
Sibling Classes
- Two classes derived from the same parent.
- Have the same characteristics inherited from their parents, but do not have an inheritance relationship between themselves.
Class Hierarchy
- Common features should be established as high up in the hierarchy as possible.
Inheritance is Transitive
- Inherited traits are continuously passed down through the hierarchy.
The toString()
Method
- Typically overridden to provide a custom string representation of an object.
The equals()
Method
- Provided by the
Object
class, determines if two object references refer to the same object.
Java Interfaces
- Collection of abstract methods and constants.
- Establish a set of methods that a class will implement.
Abstract Methods
- Method headers without a method body.
- Declares a contract for a method that will be implemented by concrete subclasses.
Implementing Interfaces
- Use the reserved words
interface
andimplements
in the class header to use interfaces.
Abstract Classes
- Placeholders in a class hierarchy that represent generic concepts.
- Cannot be instantiated.
- Define common behavior for subclasses.
Polymorphism
- "Many forms."
- Allows a message to be displayed in more than one form.
- Defines one interface with multiple implementations.
Polymorphic References
- A reference can refer to different types of objects at different times.
- The method called through a polymorphic reference can vary from one invocation to the next.
Types of Polymorphism
- Compile-time (Static): Determined at compile time.
- Runtime (Dynamic): Determined at runtime.
Binding
- The process of associating a method call with its specific implementation.
Dynamic Binding / Late Binding
- The decision about which implementation to call cannot be made until runtime.
Polymorphism via Interfaces
- Polymorphism works similarly, but an interface reference variable can refer to objects of any class implementing that interface.
Casting
- Cast the appropriate reference.
- Valid if we know the invocation is valid in a particular situation.
Sorting
- The process of arranging a list of items in a particular order.
Selection Sort Strategy
- Find the smallest element in the unsorted part of the array.
- Swap it with the element at the beginning of the unsorted portion.
- Repeat until the entire array is sorted.
Swapping in Selection Sort
- Swap two elements using a temporary variable.
The Comparable
Interface
- Built-in Java interface from
java.lang
package. - Allows objects of a class to be compared to each other, mainly for sorting purposes.
The compareTo()
Method
- Used to compare two objects of the same class with respect to each other.
- Returns:
-
Negative value
if the first input is less than the second. -
0
if they are equal. -
Positive value
if the first input is greater than the second.
-
Insertion Sort Strategy
- Pick an item and insert it into its correct place within a sorted sublist.
- Repeat until all items have been inserted.
Search Pool
- A group of items.
Searching
- The process of finding a target element within a search pool.
Linear Search
- Examine items sequentially, starting from the beginning, until the target is found or the end of the pool is reached.
Binary Search
- Requires a sorted array.
- Start at the middle index of the array.
- If the target is not at the middle index, eliminate half of the array based on the relative order.
Exceptions
- Represent problems or unusual situations in a program.
Error
- Represents an unrecoverable situation, typically not caught.
Exception Scenarios
- Invalid input.
- File not found.
- Array out of bounds.
- Network connectivity issues.
- Division by zero.
Exception Handling
- If an exception isn't handled, the program terminates and displays an error message with the call stack trace.
Call Stack Trace
- Indicates the line where the exception occurred.
- Shows the method call trail leading to the offending line.
Try-Catch Statement
- Identifies a block of code that may throw an exception.
- Contains a try block and catch blocks.
Try Block
- Defines a block of code to be tested for errors during execution.
Catch Block
- Defines a block of code to be executed if an error occurs within the try block.
Catch Clauses and Exception Variables
- Each
catch
clause is a catch handler. - Exception variable can help in analyzing the exception.
The getMessage()
Method
- Returns a string explaining the reason for the exception.
Finally Clause
- Executed regardless of how the try block is exited (exception or not).
Exception Propagation
- If an exception is not caught where it occurs, control goes back to the method that invoked it.
- Continues until caught, handled, or passed out of the main method, which terminates the program.
Defining Custom Exceptions
- Extend the
Exception
class.
The Throwable
Class
- Parent of both
Exception
andError
classes. - Superclass of all errors and exceptions.
- Provides the basis for all descendant classes with the
throw
keyword.
The throw
Keyword
- Used to explicitly throw an exception.
Checked Exceptions
- Must be either caught or listed in the
throws
clause of any method that may throw or propagate it.
Unchecked Exceptions
- Do not need to be explicitly declared or caught.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
This quiz focuses on the fundamentals of inheritance in object-oriented programming. It covers key concepts such as class definitions, inheritance relationships, and visibility modifiers. Test your knowledge about how to create and use subclasses effectively.