Questions and Answers
Which of the following statements about the IntCalculator are correct? (Select all that apply)
What is the term for a variable whose value is never changed, but it isn't declared with the final key word?
What is the special type of expression used to create an object that implements a functional interface?
Which of the following is an example of a lambda expression?
Signup and view all the answers
When an 'is a' relationship exists between objects, it means that the specialized object has:
Signup and view all the answers
Which of the following statements declares Salaried as a subclass of PayType?
Signup and view all the answers
If ClassA extends ClassB, then:
Signup and view all the answers
In an inheritance relationship:
Signup and view all the answers
In UML diagrams, inheritance is shown:
Signup and view all the answers
What keyword can you use to call a superclass constructor explicitly?
Signup and view all the answers
What is wrong with the following code? public class ClassB extends ClassA { public ClassB() { int init = 10; super(40); }}
Signup and view all the answers
Look at the following code and determine what the call to super will do. public class ClassB extends ClassA { public ClassB() { super(10); }}
Signup and view all the answers
If a subclass constructor does not explicitly call a superclass constructor:
Signup and view all the answers
The super statement that calls the superclass constructor:
Signup and view all the answers
Replacing inadequate superclass methods with more suitable subclass methods is known as what?
Signup and view all the answers
If two methods have the same name but different signatures, they are:
Signup and view all the answers
Look at the following code. The method in line ________ will override the method in line ________.
Signup and view all the answers
Look at the following code. Which line will cause a compiler error?
Signup and view all the answers
When a subclass overloads a superclass method:
Signup and view all the answers
A protected member of a class may be directly accessed by:
Signup and view all the answers
When declaring class data members, it is best to declare them as:
Signup and view all the answers
If you do not provide an access specifier for a class member, the class member is given ________ by default.
Signup and view all the answers
When a method is declared with the ________ modifier, it cannot be overridden in a subclass.
Signup and view all the answers
If ClassC extends ClassB, which extends ClassA, this would be an example of:
Signup and view all the answers
Look at the following code. Which method1 will be executed as a result of the following statements? ClassA item1 = new ClassC(); item1.method1();
Signup and view all the answers
Look at the following code. Which line will cause a compiler error? ClassB item1 = new ClassA(); item1.method1();
Signup and view all the answers
If a class contains an abstract method:
Signup and view all the answers
Given the following code which of the following is TRUE? public class ClassB implements ClassA{}
Signup and view all the answers
All fields declared in an interface:
Signup and view all the answers
Look at the following code. Which line has an error? public interface Interface1 { int FIELDA = 55; public int methodA(double){} }
Signup and view all the answers
Look at the following code. Which line in ClassA has an error? public interface MyInterface { int FIELDA = 55; public int methodA(double); } public class ClassA implements MyInterface { FIELDA = 60; public int methodA(double) { } }
Signup and view all the answers
Look at the following code. What is missing from ClassA? public interface MyInterface { int FIELDA = 55; public int methodA(double); } public class ClassA implements MyInterface { FIELDA = 60; public int methodB(double) { } }
Signup and view all the answers
When one object is a specialized version of another object, there is this type of relationship between them.
Signup and view all the answers
A subclass can directly access:
Signup and view all the answers
In the following statement, which is the superclass? public class ClassA extends ClassB implements ClassC
Signup and view all the answers
A subclass may call an overridden superclass method by:
Signup and view all the answers
In the following statement, which is the subclass? public class ClassA extends ClassB implements ClassC
Signup and view all the answers
In the following statement, which is the interface? public class ClassA extends ClassB implements ClassC
Signup and view all the answers
What is wrong with the following code? public class ClassB extends ClassA { public ClassB() { super(40); System.out.println('This is the last statement in the constructor.'); }}
Signup and view all the answers
In the following code, what will the call to super do? public class ClassB extends ClassA { public ClassB() { super(40); System.out.println('This is the last statement in the constructor.'); }}
Signup and view all the answers
If a superclass does not have a default constructor or a no-arg constructor:
Signup and view all the answers
Look at the following code. The method in line ________ will override the method in line ________.
Signup and view all the answers
Look at the following code. Which line will cause a compiler error? public class ClassA { public ClassA() {} public int method1(int a){} public final int method2(double b){} } public class ClassB extends ClassA { public ClassB(){} public int method1(int b){} public int method2(double c){} }
Signup and view all the answers
Protected members are:
Signup and view all the answers
Protected class members are denoted in a UML diagram with the symbol:
Signup and view all the answers
Which of the following is TRUE about protected access?
Signup and view all the answers
Like a family tree, a ________ shows the inheritance relationship between classes.
Signup and view all the answers
In a class hierarchy:
Signup and view all the answers
Look at the following code: Which method1 will be executed when the following statements are executed? ClassA item1 = new ClassB(); item1.method1();
Signup and view all the answers
Look at the following code: Which method will be executed when the following statements are executed? ClassC item1 = new ClassA(); item1.method1();
Signup and view all the answers
If a class contains an abstract method:
Signup and view all the answers
In an interface all methods have:
Signup and view all the answers
Which of the following statements correctly specifies three interfaces?
Signup and view all the answers
This annotation tells the Java compiler that a method is meant to override a method in the superclass.
Signup and view all the answers
What is required for an interface method that has a body?
Signup and view all the answers
An anonymous inner class must:
Signup and view all the answers
Study Notes
Inheritance and Object Relationships
- An "is a" relationship indicates that the specialized object inherits all characteristics of the general object along with additional traits.
- Subclass relationships in Java are established using the
extends
keyword, which creates a hierarchical structure. - In an inheritance relationship, the superclass constructor executes before the subclass constructor to ensure proper initialization.
Constructor Behavior
- Java requires that a call to the superclass constructor using the
super
keyword must be the first statement in the subclass constructor. - If a subclass constructor doesn't explicitly call a superclass constructor, Java automatically invokes the superclass's default or no-arg constructor before executing the subclass constructor.
Method Overriding and Overloading
- Method overriding occurs when a subclass provides a specific implementation for a method already defined in its superclass.
- Method overloading happens when multiple methods in the same class share the same name but differ in parameter types or counts.
- Access levels to superclass methods vary: protected members can be accessed by subclasses, while private members cannot.
Access Modifiers
- Members declared as
protected
can be accessed by the same package classes but also by subclasses in different packages. - By default, if no access modifier is given for a class member, it has package-private visibility.
Abstract Classes and Interfaces
- An abstract class cannot be instantiated, and any subclass must implement its abstract methods.
- Methods in an interface are implicitly public and abstract unless declared otherwise.
- Interfaces in Java can be implemented by classes, which must provide concrete definitions for the interface methods.
UML Diagrams and Class Hierarchies
- In UML, inheritance is represented by a line with an open arrowhead pointing to the superclass, illustrating the parent-child relationship.
- A class hierarchy visually represents the inheritance relationships between classes, with more general classes positioned at the top.
Exception Handling within Classes
- Compiler errors may arise when method signatures in subclasses conflict with final methods in superclasses or due to incorrect method accessibility.
- A class implementing an interface must follow the rules laid out by the interface, specifically regarding method definitions and access.
Lambda Expressions and Anonymous Classes
- A lambda expression is a concise way to represent a function that implements a functional interface, enabling cleaner code.
- Anonymous inner classes can be used to provide implementations of interfaces or extend classes without the need for a separate named class.
Key Terms and Definitions
- The
@Override
annotation indicates that a method is intended to override a method from a superclass. - Variables that remain unchanged after initialization but are not marked as
final
are referred to as effectively final variables.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Test your understanding of object-oriented programming concepts through these flashcards. Focus on the 'is a' relationships and their implications in class hierarchies. Ideal for students learning programming principles.