Inheritance in Java Flashcards
26 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

Are private fields and methods of the superclass inherited in the subclass?

False

If a subclass constructor does not explicitly call a superclass constructor, what will Java automatically do?

Java will automatically call the superclass's default constructor.

Can a method be overloaded and simultaneously be overriding a method in a superclass?

True

"has a" is a part of what concept?

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

"is a" is a part of what concept?

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

When a subclass inherits fields and methods from a superclass, are they included in the subclass?

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

What keyword indicates that a subclass inherits from a superclass?

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

In a UML diagram, how do we indicate that a subclass extends a superclass?

<p>We use an open arrow head pointing from the subclass to the superclass.</p> Signup and view all the answers

We can say that a subclass is a specialized version of the superclass.

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

Given two classes, Auto() and SportsCar() which extends Auto(), when a new SportsCar object is created, what object is created first?

<p>An object of the superclass Auto() is created first.</p> Signup and view all the answers

Can private members of the superclass be accessed by the subclass?

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

Given a superclass with a private field numHours, is the statement 'SubClass child = new SubClass(); println("My child studies " + child.numHours + " hours a day.")' syntactically correct?

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

SuperClass constructors are inherited.

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

When a subclass is instantiated, the superclass _____________ constructor is executed first.

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

When a subclass is instantiated, what is instantiated first?

<p>The superclass default constructor.</p> Signup and view all the answers

What does the super keyword refer to?

<p>The superclass.</p> Signup and view all the answers

What happens if you have written your own constructor in the superclass and omit the default constructor?

<p>You must add a no-arg constructor or call the superclass constructor from the subclass.</p> Signup and view all the answers

Must the call to the superclass constructor be the first Java statement in the subclass constructor?

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

If the superclass does not have a default no-arg constructor, what must happen?

<p>Every subclass must explicitly invoke the superclass constructor using the keyword SUPER.</p> Signup and view all the answers

How can a subclass method invoke the overridden method found in the superclass?

<p>By using the super keyword.</p> Signup and view all the answers

What is a protected member?

<p>Protected members may be accessed by methods in a subclass and by methods in the same package.</p> Signup and view all the answers

In a UML diagram, how is a protected member designated?

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

How do we prevent a method from being overridden in the superclass by a subclass?

<p>By using the final modifier.</p> Signup and view all the answers

What is the Object class?

<p>The Java API has a class named Object, which all other classes directly or indirectly inherit from.</p> Signup and view all the answers

Why does every class have a toString or equals method?

<p>Because every class inherits from the Object class.</p> Signup and view all the answers

Can static members be overridden?

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

Study Notes

Inheritance Principles

  • Private fields and methods of the superclass are not inherited by the subclass.
  • If a subclass constructor does not call a superclass constructor, Java automatically invokes the superclass's default (no-arg) constructor.
  • Methods can be both overloaded and overridden in subclasses—overloading occurs when methods share the same name but differ in parameters.

Concepts in Object-Oriented Design

  • The term "has a" relates to the concept of aggregation, indicating a whole-part relationship.
  • The term "is a" relates to inheritance, illustrating a hierarchical relationship between classes.
  • When a subclass inherits fields and methods from a superclass, they are not physically included in the subclass's memory.

Relation Indicators

  • The keyword extends signifies that a subclass inherits from a superclass.
  • In UML diagrams, inheritance is represented with an open arrowhead pointing from the subclass to the superclass.

Object Creation and Constructors

  • A subclass can be seen as a specialized version of the superclass, inheriting its properties and behaviors.
  • When a new object of a subclass is created, the superclass's constructor is executed before the subclass's constructor.
  • If a superclass has a private field (e.g., numHours), it cannot be accessed directly by the subclass.
  • Superclass constructors are not inherited by subclasses; each subclass must invoke the superclass constructor if needed.

Default Constructor and Overriding

  • The superclass's default constructor is executed first when a subclass is instantiated.
  • If a superclass lacks a default no-arg constructor, all subclass constructors must explicitly call a constructor from the superclass.
  • The super keyword is essential for invoking the superclass's constructor and accessing its members.

Access Modifiers and Method Overriding

  • Protected members are accessible by methods in the subclass and methods within the same package, ensuring controlled access.
  • In UML, a protected member is denoted with a #.
  • To prevent a method from being overridden in a subclass, use the final modifier in the superclass method definition.

Object Class and Static Members

  • All classes in Java inherit from the Object class, which provides fundamental methods like toString and equals.
  • Every class has a toString and equals method because they inherit from the Object class.
  • Static members belong to a class and cannot be overridden by subclasses, as they are not tied to instances.

Studying That Suits You

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

Quiz Team

Description

Test your knowledge of inheritance in Java with these flashcards. Covering key concepts such as visibility of fields and automatic constructor calls, these flashcards are perfect for anyone needing a refresher on Java inheritance principles.

Use Quizgecko on...
Browser
Browser