Questions and Answers
Is the statement Apple p = new McIntosh() legal?
True
Is fruit instanceof Orange true?
False
Is fruit instanceof Macintosh true?
False
Can fruit invoke the method makeApple?
Signup and view all the answers
Can orange invoke the method makeOrangeJuice?
Signup and view all the answers
Is the statement Orange p = new Apple() legal?
Signup and view all the answers
What causes a runtime ClassCastingException?
Signup and view all the answers
Does every object have a toString method and an equals method?
Signup and view all the answers
Is mistyping the equals method signature a common mistake?
Signup and view all the answers
What will the output be for circle1.equals(circle2) using method (a)?
Signup and view all the answers
What will the output be for circle1.equals(circle2) using method (b)?
Signup and view all the answers
What happens when you call list.remove('Dallas') one time?
Signup and view all the answers
Can you add a Date object to a list of Strings?
Signup and view all the answers
Is it correct to run System.out.println(java.util.Collections.max(array)) on a primitive array?
Signup and view all the answers
What access does the protected keyword provide?
Signup and view all the answers
Can final classes be extended?
Signup and view all the answers
Can a class with default modifier be accessed by a class in a different package?
Signup and view all the answers
What should the modifier be so that a class in the same package can access it, but a class in a different package cannot?
Signup and view all the answers
How do you prevent a method from being overridden?
Signup and view all the answers
Is a protected datum accessible by any class in different packages?
Signup and view all the answers
What does inheritance allow in programming?
Signup and view all the answers
What is a superclass?
Signup and view all the answers
What is a child class?
Signup and view all the answers
What keyword indicates that a class is extending another superclass?
Signup and view all the answers
True or false? Java allows multiple inheritance directly from multiple classes.
Signup and view all the answers
What is single inheritance?
Signup and view all the answers
True or false? A subclass is a subset of a superclass.
Signup and view all the answers
What keyword is used to define a subclass?
Signup and view all the answers
What is the output of running class C in a given example?
Signup and view all the answers
How does a subclass invoke its superclass's constructor?
Signup and view all the answers
True or false? When invoking a constructor from a subclass, its superclass's no-arg constructor is always invoked.
Signup and view all the answers
What is overriding a method?
Signup and view all the answers
True or false? You can override a private method defined in a superclass.
Signup and view all the answers
True or false? You can override a static method defined in a superclass.
Signup and view all the answers
What is the benefit of using the @Override annotation?
Signup and view all the answers
What is the ultimate parent or superclass in Java?
Signup and view all the answers
What is polymorphism?
Signup and view all the answers
What are the three pillars of object-oriented programming?
Signup and view all the answers
What is dynamic binding?
Signup and view all the answers
True or false? Casting an object reference generates a new object.
Signup and view all the answers
True or false? You can always successfully cast an instance of a superclass to a subclass.
Signup and view all the answers
Study Notes
Inheritance
- Inheritance allows creating a superclass (general class) and extending it to subclasses (specialized classes) that inherit properties and methods.
- Subclasses usually contain more methods than the superclass.
- Private data fields in a superclass are inaccessible to subclasses, accessed only through getters and setters.
- The "is-a" relationship must be relevant; not all relationships are suitable for inheritance.
- Java supports single inheritance directly but can implement multiple inheritance through interfaces.
Class Types
- Superclass/Parent/Base Class: The class from which a subclass derives.
- Child Class/Extended Class/Derived Class: The class that inherits properties and methods from the superclass.
Inheritance Concepts
- The
extends
keyword is used to define a subclass. - Single inheritance permits a subclass to inherit from only one superclass.
- Java does not allow multiple inheritance but permits a superclass with an interface.
Constructors and Method Overriding
- The
super
keyword invokes a superclass's methods and constructors. - Superclass constructors are invoked using
super()
as the first statement in the subclass constructor. - Constructor chaining occurs when a subclass constructor calls its superclass constructor before executing its own tasks.
- Overriding a method must match the original signature and return type and is annotated with
@Override
.
Static and Private Methods
- Private and static methods cannot be overridden.
- Access modifiers and return types must align for successful method overriding.
Polymorphism and Dynamic Binding
- Polymorphism enables superclass variable types to refer to subclass objects.
- Dynamic binding allows the JVM to choose the method implementation at runtime based on the object type.
- Declared type of a variable determines method matching at compile time while actual type determines method binding at runtime.
Casting
- Implicit casting allows subclass variables to be used as superclass references.
- Explicit casting is required when downcasting objects, which can result in a
ClassCastException
if improperly handled. - The
instanceof
keyword checks if an object of a certain type can reference a specific subclass.
Object Class Methods
- Every object in Java has a
toString()
andequals()
method defined in theObject
class, typically overridden for customized behavior.### Equals Method in Java - Incorrectly overriding the
equals
method can lead to errors; it should takeObject
as a parameter instead of the specific class (e.g.,equals(Object o)
). - Using an incorrectly overridden
equals
method results in always returning false when comparing instances, as signatures do not match during the compile-time check. - Correct overriding allows proper comparison leading to a true output when checking instances with the correct type.
- Use of
instanceof
in theequals
method is crucial for type safety and avoiding runtime errors when comparing different object types.
ArrayList Operations
- Create an ArrayList for double values with
ArrayList<Double> list = new ArrayList<>();
. - Append an object with
list.add(object);
. - Insert at the beginning with
list.add(0, object);
. - Get the count of objects using
list.size();
. - Remove a specific object with
list.remove(object);
. - Remove the last object with
list.remove(list.size() - 1);
. - Check if an object exists using
list.contains(object);
. - Retrieve an object at a specific index using
list.get(index);
.
Code Error Identification
- Adding incompatible types (e.g., Date to a String ArrayList) leads to compilation errors.
- Attempting to access an index that does not exist results in
IndexOutOfBoundsException
.
List Modifications
- Removing an item from a list only removes the first instance; subsequent calls require a loop or a different approach.
- Use a while loop with
i--
after a successful removal to correctly iterate and check against the new list size.
Integer and Object Management in Lists
- Java's type system requires care when handling primitive types; using
Double
rather thanint
directly avoids issues in lists. - For collections, convert primitive arrays to wrapper class arrays for methods like
Arrays.asList()
.
Access Modifiers in Java
- Protected: Members are accessible in the same package and by subclasses. More open than private but controlled compared to public.
- Private: Members are rigidly private, only accessible within the same class.
- Public: Members are fully accessible by any class, enabling broader functionality.
- Default (package-private): Members are accessible only within the same package.
Final Keyword
- Declaring a class or method as
final
prevents inheritance or overriding, ensuring that the intended behavior remains unchanged. - Final classes can still be instantiated, but they cannot be derived from.
Access Modifier Statements Evaluation
- True: A protected method or variable can be accessed in the same package.
- False: A protected method cannot be accessed by all classes in different packages, only by subclasses.
- True: Subclasses in any package have access to protected members.
- True: Final classes can have instances created.
- False: Final classes cannot be extended.
- False: Final methods cannot be overridden.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Explore key concepts of Inheritance and Polymorphism in Java programming through interactive flashcards. This quiz will help reinforce your understanding of how subclasses inherit properties and methods from superclasses, as well as the accessibility of private data fields. Perfect for students looking to solidify their grasp of object-oriented programming.