Inheritance Flashcards Chapter 9
30 Questions
100 Views

Inheritance Flashcards Chapter 9

Created by
@FeatureRichHazel

Questions and Answers

What is the general class in an inheritance relationship called?

superclass

What is the specialized class in an inheritance relationship called?

subclass

What key word indicates that a class inherits from another class?

extends

What members of a superclass does a subclass not have access to?

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

What key word refers to an object's superclass?

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

In a subclass constructor, when must a call to the superclass constructor appear?

<p>as the very first statement</p> Signup and view all the answers

What is the following an inexplicit call to the superclass's default constructor? super();

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

What is it called when a method in a subclass has the same signature as a method in the superclass?

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

What is it called when a subclass method has the same name as a method in the superclass but a different signature?

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

What members of a superclass are accessible to subclasses and classes in the same package?

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

When a class implements an interface, what must it provide?

<p>all of the nondefault methods that are listed in the interface, with the exact signatures and return types specified</p> Signup and view all the answers

What are fields in an interface?

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

Abstract methods must be what?

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

What can abstract classes not do?

<p>be instantiated</p> Signup and view all the answers

You can use a lambda expression to instantiate an object that does what?

<p>implements a functional interface</p> Signup and view all the answers

If a subclass constructor does not explicitly call a superclass constructor, Java will not call any of the superclass's constructors.

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

The superclass constructor always executes before the subclass constructor.

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

When a class contains an abstract method, the class cannot be instantiated.

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

What is the difference between a protected class member and a private class member?

<p>A protected member can be accessed by other classes in the same package. A private member can only be accessed in the class where it is created.</p> Signup and view all the answers

What is the superclass and what is the subclass in the following line? public class Pet extends Dog?

<p>Dog is superclass, Pet is subclass</p> Signup and view all the answers

Can a subclass ever directly access the private members of its superclass?

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

What is the difference between overriding a superclass method and overloading a superclass method?

<p>Overriding means that they have the same signatures in both methods. Overloaded methods have the same name, but different parameter lists.</p> Signup and view all the answers

What is an abstract method?

<p>An abstract method expects to be overridden in the class implementing it.</p> Signup and view all the answers

What is an abstract class?

<p>An abstract class is a class designed with implementation gaps for subclasses to fill in and is deliberately incomplete. It cannot be instantiated.</p> Signup and view all the answers

What is the name of the superclass and the subclass in the following line? public class Truck extends Vehicle?

<p>Vehicle is the superclass, Truck is the subclass</p> Signup and view all the answers

Describe the order in which the class's constructors execute when a class B inherits from class A.

<p>Constructor A first then B.</p> Signup and view all the answers

When you create a class, why does it automatically have a toString method and an equals method?

<p>It receives these methods from the Object class.</p> Signup and view all the answers

How is an interface different from an abstract class, or any class?

<p>Many interfaces can apply to many classes whereas an abstract class can only apply to a subclass.</p> Signup and view all the answers

Write the first line of a class named Employee, which implements an interface named Payable and Listable.

<p>public class Employee implements Payable, Listable</p> Signup and view all the answers

When designing a class hierarchy, what should be true of a superclass?

<p>A superclass should contain the data and functionality that are common to all subclasses that inherit from the superclass.</p> Signup and view all the answers

Study Notes

Inheritance Basics

  • Superclass is the general class in an inheritance relationship.
  • Subclass is the specialized class that extends the functionality of the superclass.
  • The keyword "extends" indicates that a class inherits from another class.

Access Modifiers in Inheritance

  • Subclasses do not have access to private members of the superclass.
  • Protected members of the superclass are accessible to subclasses and classes in the same package.
  • Private members can only be accessed within the class where they are declared.

Class Constructors

  • In a subclass constructor, a call to the superclass constructor must appear as the first statement.
  • The call to the superclass's default constructor can be done using super();.
  • The superclass constructor always executes before the subclass constructor.

Method Overriding and Overloading

  • A method in a subclass that has the same signature as a method in the superclass is an example of overriding.
  • A method in a subclass with the same name but a different signature than a method in the superclass is an example of overloading.
  • Overriding means the same signatures are present in both methods, while overloading has different parameter lists.

Abstract Classes and Methods

  • Abstract methods must be overridden in the subclass implementing them.
  • Abstract classes cannot be instantiated and are intentionally left incomplete for subclasses to fill in the gaps.

Interfaces

  • When a class implements an interface, it must provide implementations for all non-default methods with exact signatures and return types.
  • Fields in an interface are both final and static.
  • An interface allows multiple interfaces to be applied across different classes, unlike abstract classes which relate to a single hierarchy.

Class Declaration and Members

  • Constructors of superclass execute before those of the subclass when a subclass object is created.
  • Class objects implicitly receive methods like toString and equals from the Object class.
  • A subclass can never directly access the private members of its superclass.

Understanding Class Relationships

  • In the declaration public class Pet extends Dog, Dog is the superclass and Pet is the subclass.
  • Abstract classes should encompass common functionality across subclasses, serving as a foundation for derived classes.
  • An Employee class can implement multiple interfaces like Payable and Listable through the syntax public class Employee implements Payable, Listable.

Studying That Suits You

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

Quiz Team

Description

Enhance your understanding of inheritance in object-oriented programming with these flashcards. Explore key terms such as 'superclass', 'subclass', and 'extends'. Perfect for anyone looking to solidify their knowledge in Chapter 9 of their programming course.

Use Quizgecko on...
Browser
Browser