Flashcards - Object Relationship Concepts
56 Questions
100 Views

Flashcards - Object Relationship Concepts

Created by
@LoyalLanthanum

Questions and Answers

Which of the following statements about the IntCalculator are correct? (Select all that apply)

  • The inner braces are not needed. (correct)
  • The outer braces are not needed. (correct)
  • The new keyword is not needed. (correct)
  • The statement does not end with a semicolon. (correct)
  • What is the term for a variable whose value is never changed, but it isn't declared with the final key word?

  • Effectively final variable (correct)
  • Virtually constant variable
  • Default variable
  • Anonymous inner variable
  • What is the special type of expression used to create an object that implements a functional interface?

  • Beta
  • Alpha
  • Sigma
  • Lambda (correct)
  • Which of the following is an example of a lambda expression?

    <p>IntCalculator multiplier = x -&gt; x * factor;</p> Signup and view all the answers

    When an 'is a' relationship exists between objects, it means that the specialized object has:

    <p>All the characteristics of the general object, plus additional characteristics</p> Signup and view all the answers

    Which of the following statements declares Salaried as a subclass of PayType?

    <p>public class Salaried extends PayType</p> Signup and view all the answers

    If ClassA extends ClassB, then:

    <p>Public members in ClassB are public in ClassA, but private members in ClassB cannot be directly accessed in ClassA</p> Signup and view all the answers

    In an inheritance relationship:

    <p>The superclass constructor always executes before the subclass constructor</p> Signup and view all the answers

    In UML diagrams, inheritance is shown:

    <p>With a line that has an open arrowhead at one end that points to the superclass</p> Signup and view all the answers

    What keyword can you use to call a superclass constructor explicitly?

    <p>super</p> 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); }}

    <p>The call to the method super must be the first statement in the constructor.</p> 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); }}

    <p>It will call the constructor of ClassA that receives an integer as an argument.</p> Signup and view all the answers

    If a subclass constructor does not explicitly call a superclass constructor:

    <p>Java will automatically call the superclass's default or no-arg constructor just before the code in the subclass's constructor executes.</p> Signup and view all the answers

    The super statement that calls the superclass constructor:

    <p>Must be the first statement in the subclass's constructor</p> Signup and view all the answers

    Replacing inadequate superclass methods with more suitable subclass methods is known as what?

    <p>Method overriding</p> Signup and view all the answers

    If two methods have the same name but different signatures, they are:

    <p>Overloaded</p> Signup and view all the answers

    Look at the following code. The method in line ________ will override the method in line ________.

    <p>None of the above</p> Signup and view all the answers

    Look at the following code. Which line will cause a compiler error?

    <p>10</p> Signup and view all the answers

    When a subclass overloads a superclass method:

    <p>Both methods may be called with a subclass object</p> Signup and view all the answers

    A protected member of a class may be directly accessed by:

    <p>All of the above</p> Signup and view all the answers

    When declaring class data members, it is best to declare them as:

    <p>Private members</p> 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.

    <p>package</p> Signup and view all the answers

    When a method is declared with the ________ modifier, it cannot be overridden in a subclass.

    <p>final</p> Signup and view all the answers

    If ClassC extends ClassB, which extends ClassA, this would be an example of:

    <p>A chain of inheritance</p> 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();

    <p>Line 14</p> Signup and view all the answers

    Look at the following code. Which line will cause a compiler error? ClassB item1 = new ClassA(); item1.method1();

    <p>This is an error and will cause the program to crash.</p> Signup and view all the answers

    If a class contains an abstract method:

    <p>All of the above</p> Signup and view all the answers

    Given the following code which of the following is TRUE? public class ClassB implements ClassA{}

    <p>ClassB must override each method in ClassA.</p> Signup and view all the answers

    All fields declared in an interface:

    <p>Are final and static</p> 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){} }

    <p>4</p> 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) { } }

    <p>8</p> 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) { } }

    <p>It does not override methodA.</p> Signup and view all the answers

    When one object is a specialized version of another object, there is this type of relationship between them.

    <p>'Is a'</p> Signup and view all the answers

    A subclass can directly access:

    <p>Only public and protected members of the superclass</p> Signup and view all the answers

    In the following statement, which is the superclass? public class ClassA extends ClassB implements ClassC

    <p>ClassB</p> Signup and view all the answers

    A subclass may call an overridden superclass method by:

    <p>Prefixing its name with the super keyword and a dot (.)</p> Signup and view all the answers

    In the following statement, which is the subclass? public class ClassA extends ClassB implements ClassC

    <p>ClassA</p> Signup and view all the answers

    In the following statement, which is the interface? public class ClassA extends ClassB implements ClassC

    <p>ClassC</p> 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.'); }}

    <p>Nothing is wrong with the code.</p> 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.'); }}

    <p>It will call the constructor of ClassA that receives an integer as an argument.</p> Signup and view all the answers

    If a superclass does not have a default constructor or a no-arg constructor:

    <p>Then a class that inherits from it must call one of the constructors that the superclass does have.</p> Signup and view all the answers

    Look at the following code. The method in line ________ will override the method in line ________.

    <p>10, 4</p> 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){} }

    <p>11</p> Signup and view all the answers

    Protected members are:

    <p>Both A and B</p> Signup and view all the answers

    Protected class members are denoted in a UML diagram with the symbol:

    <h1></h1> Signup and view all the answers

    Which of the following is TRUE about protected access?

    <p>Protected members may be accessed by methods in the same package or in a subclass, even when the subclass is in a different package.</p> Signup and view all the answers

    Like a family tree, a ________ shows the inheritance relationship between classes.

    <p>Class hierarchy</p> Signup and view all the answers

    In a class hierarchy:

    <p>The more general classes are toward the top of the tree and the more specialized are toward the bottom</p> 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();

    <p>Line 9</p> 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();

    <p>This is an error and will cause the program to crash.</p> Signup and view all the answers

    If a class contains an abstract method:

    <p>The method will have only a header, but not a body, and end with a semicolon</p> Signup and view all the answers

    In an interface all methods have:

    <p>Public access</p> Signup and view all the answers

    Which of the following statements correctly specifies three interfaces?

    <p>public class ClassA implements Interface1, Interface2, Interface3</p> Signup and view all the answers

    This annotation tells the Java compiler that a method is meant to override a method in the superclass.

    <p>@Override</p> Signup and view all the answers

    What is required for an interface method that has a body?

    <p>The method header must begin with the keyword default.</p> Signup and view all the answers

    An anonymous inner class must:

    <p>Either A or B</p> 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.

    Quiz Team

    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.

    More Quizzes Like This

    Py4e Chapter 14 Flashcards
    16 questions
    Classes and Objects Flashcards
    30 questions
    Java Classes and Objects Flashcards
    36 questions
    Use Quizgecko on...
    Browser
    Browser