APCSA Chapter 7 Test Flashcards
24 Questions
100 Views

APCSA Chapter 7 Test Flashcards

Created by
@SustainableAntigorite1088

Questions and Answers

Describe a relationship between a parent class and a child class.

Most if not all variables and methods of a parent class are inherited by a child class. The child class can't use all of them. The variables and methods that are declared public can be used by the child class, but the ones that are private cannot be used.

How does inheritance support software re-use?

A new class can be derived from an existing class.

What relationship should every pair of parent-child classes represent?

They should all represent an inheritance relationship. The child should also be a version of the parent class.

Why would a child class override methods in its parent class?

<p>The child class can make its own definition of a method instead of using the parent class's definition.</p> Signup and view all the answers

Why is the super reference important to a child class?

<p>This can be used to call the parent's constructor.</p> Signup and view all the answers

What is the relationship of the object class to all other classes?

<p>All of the classes in Java are derived from the object class directly or indirectly.</p> Signup and view all the answers

Superclass is to a subclass as:

<p>Child = Parent This = Super Object = Primitive data Base class = Derived class Instance variable = Local variable</p> Signup and view all the answers

Look back at figure 7.3 in the chapter. Which class in the diagram is both a subclass and a superclass?

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

Suppose a class M inherits from a class P. In the constructor of M, how would you call the default (no arguments) constructor of P?

<p>super()</p> Signup and view all the answers

Which of the following from a parent class is usable in a child class?

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

Look at figure 7.3. Suppose animal is declared to be a reference to an Animal. Which of the following is a valid assignment?

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

In an inheritance relationship, the parent class should be a more specific version of the child class.

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

A child class can use all public data and methods from its parent class.

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

The super reference means the same thing as the this reference, but it is only used in subclasses.

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

In Java, a subclass may inherit from many superclasses.

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

If a child class defines a method with the same signature as a method in its parent class, an error will occur.

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

What statement best characterizes the relationship between the two classes ABC and Xyz?

<p>An Xyz is an Abc</p> Signup and view all the answers

Which members of the class ABC can be used directly by the class Xyz?

<p>II and III only</p> Signup and view all the answers

What is printed by the following statements? Xyz q = new Xyz(); q.z();

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

What is an abstract class?

<p>A kind of ghost class. Can pass along methods and variables but it can't ever be instantiated itself.</p> Signup and view all the answers

Are all members of a parent class usable by the child? Explain.

<p>Yes, but if it is private then it is not.</p> Signup and view all the answers

What is polymorphism?

<p>The ability of a reference variable to refer to different types of objects at different times.</p> Signup and view all the answers

How does inheritance support polymorphism?

<p>A reference variable declared using a parent class can be used to refer to an object of a child class.</p> Signup and view all the answers

How is overriding related to polymorphism?

<p>Overriding is a form of polymorphism which is used in Java to dynamically bind a method from the subclass in response to a method call from a subclass object referenced by a superclass.</p> Signup and view all the answers

Study Notes

Inheritance Basics

  • Child classes inherit most variables and methods from parent classes, with access limited by visibility (public vs. private).
  • Inheritance allows the creation of new classes based on existing ones, promoting code re-use.

Parent-Child Class Relationship

  • Each parent-child class pair exemplifies an inheritance relationship, where the child acts as a specialized version of the parent.
  • Child classes can override parent methods to provide specific implementations, enabling customization.

Importance of Super Reference

  • The super reference is essential for invoking a parent class's constructor within the child class.

Object Class Hierarchy

  • All Java classes are derived from the object class, either directly or indirectly.

Class Relationships and Terminology

  • "Superclass" is synonymous with "base class," while "subclass" aligns with "derived class."
  • A class can have multiple subclasses but can only inherit from one superclass.

Accessibility of Parent Class Members

  • Only public members, methods, and inherited static methods are accessible in child classes.
  • Private members and methods remain inaccessible to child classes.

Method Overriding and Polymorphism

  • Overriding a parent method in a child class does not generate an error; rather, it allows the child to define its behavior.
  • Polymorphism permits a reference of the parent class type to point to objects of a child class, enhancing flexibility in code execution.
  • Overriding is a key aspect of polymorphism, allowing method calls on a superclass reference to resolve to the child class implementation at runtime.

Abstract Classes

  • Abstract classes cannot be instantiated directly but serve as a blueprint for other classes, carrying methods and variables for subclass use.

Summary of True/False Statements

  • Child classes are more specific versions of their parent classes, not vice versa.
  • Child classes can access all public members of the parent class, but private members are restricted.
  • super differs from this as it specifically addresses parent class members.
  • Subclasses can inherit from only one superclass, maintaining Java's single inheritance models.

Example Class Relationships

  • An instance of class Xyz represents an instance of class Abc, demonstrating subclass-parent relations.
  • Members from class Abc accessible in Xyz include public static variables and methods, which can influence behavior and structure.

Method Execution in Classes

  • Invoking methods from subclasses can yield combined outputs, demonstrating inherited and overridden functionalities.
  • For instance, calling z() from an Xyz object runs methods from both Xyz and its parent Abc, showing inheritance in action.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Description

This quiz covers key concepts from Chapter 7 of APCSA, focusing on inheritance in object-oriented programming. You'll learn about the relationship between parent and child classes, as well as how inheritance supports software re-use. Test your understanding with these flashcards!

Use Quizgecko on...
Browser
Browser