Java Inheritance Basics
33 Questions
100 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

An object of a derived class has the type of the derived class, and it also has the type of the base class, and more generally, has the type of every one of its ___________ classes.

ancestor

Overriding is when a derived class redefines a method from the base class.

True

If an instance variable is not modified by public, protected or private then it is said to have:

  • Package access
  • Default access
  • Friendly access
  • All of the above (correct)
  • Inheritance is the process by which a new class - known as a _________ - is created from another class, called the _____________.

    <p>derived class, base class</p> Signup and view all the answers

    When a class both extends and implements, by convention the ____ clause is last in the class header.

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

    A constructor for a derived class begins with an invocation of a constructor for the base class.

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

    Inheritance refers to a very specialized form of a class.

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

    A(n) ____ is not an object, but it points to a memory address.

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

    A derived class contains only public instance variables and public methods from the base class.

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

    The call to the base class constructor (super) must always be the last action taken in a constructor definition.

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

    When you define a class, if you do not explicitly extend another class, your class is an extension of the ____ class.

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

    A derived class is a class defined by adding instance variables and methods to an existing class.

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

    In using the keyword this in place of super(), the invocation of this must be the ___________ action taken by the constructor.

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

    You may substitute the keyword this for super() to call a constructor of the derived class.

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

    When you assign a variable or constant of one type to a variable of another type, the behavior is called ____.

    <p>implicit conversion</p> Signup and view all the answers

    A(n) ____ class is one from which you cannot create any concrete objects, but from which you can inherit.

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

    If the final modifier is added to the definition of a method, this means:

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

    A good toString() method can be very useful in debugging a program.

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

    The equals method for a class should have _________ as the type of its one parameter.

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

    If Java did not allow you to ____ classes, you would need to create every part of a program from scratch.

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

    If you do not specify a package for a class, it is placed in an unnamed ____ package.

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

    The following statement creates an array of three Animal references.

    <p>Animal[] ref = new Animal[3];</p> Signup and view all the answers

    Private methods of the base class are not available for use by derived classes.

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

    The special syntax for invoking a constructor of the base class is:

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

    When a class both extends and implements, by convention the ____ clause is last in the class header.

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

    An instance variable (or method) that is private in a base class is accessible by name in the definition of a method in any other class.

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

    Inheritance promotes code ___________.

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

    A derived class is also called a ___ class.

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

    The class __________ is an ancestor class of all Java classes.

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

    An application's ability to select the correct subclass method is known as ____.

    <p>dynamic method binding</p> Signup and view all the answers

    The Object class contains the method:

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

    A base class is synonymous with a:

    <p>Parent class</p> Signup and view all the answers

    A super class is also called a:

    <p>base class</p> Signup and view all the answers

    Study Notes

    Inheritance Basics

    • An object of a derived class has types of the derived class, base class, and all ancestor classes.
    • Inheritance creates a new class, known as a derived class, from an existing class called the base class.

    Method Overriding

    • Overriding occurs when a derived class redefines a method from its base class.
    • Derived classes can also add instance variables and methods to enhance the base class functionality.

    Access Modifiers

    • If an instance variable lacks public, protected, or private modifiers, it has package access, also termed default or friendly access.

    Class Relationships

    • A derived class is referred to as a subclass, while a base class is synonymous with the parent class or superclass.
    • The Object class is the ancestor of all Java classes, automatically extending from it if no other superclass is specified.

    Constructor Behavior

    • A constructor in a derived class starts by invoking a constructor of its base class, although this invocation doesn't have to be the last action taken.

    Object References

    • A reference is not an object but points to a memory address where an object is stored.

    Abstract and Final Classes

    • An abstract class cannot instantiate objects but can serve as a base for other classes.
    • A method defined with the final modifier cannot be redefined in any derived class.

    Debugging and Utility Methods

    • Implementing a robust toString() method aids significantly in debugging Java programs.
    • The equals method in a class should have Object as the parameter type.

    Dynamic Binding

    • The capability of selecting the appropriate subclass method at runtime is termed dynamic method binding.

    Accessing Methods and Variables

    • Private methods in a base class are inaccessible to derived classes.
    • An instance variable or method declared private is not accessible by name in other class methods.

    Code Reusability

    • Inheritance encourages code reuse, allowing shared functionality without rewriting code.

    Array and Package Defaults

    • Creating an array of Animal references can be done as: Animal[] ref = new Animal;.
    • Classes without a specified package belong to the unnamed default package.

    Calling Superclass Constructors

    • Superclass constructors are invoked using the syntax super(), which allows for proper initialization in inheritance hierarchies.

    Studying That Suits You

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

    Quiz Team

    Description

    Test your knowledge on the foundational concepts of inheritance in Java, including method overriding, class relationships, and access modifiers. Understand how derived classes and base classes interact and the significance of constructors in class hierarchies.

    More Like This

    Java Inheritance Study Questions
    14 questions
    Ereditarietà in Java
    12 questions

    Ereditarietà in Java

    PortableMoldavite3157 avatar
    PortableMoldavite3157
    Use Quizgecko on...
    Browser
    Browser