Chapter 3 - Polymorphism/Abstract

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/17

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 4:07 AM on 2/22/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

18 Terms

1
New cards

What is a final method?

A method that cannot be overridden by a subclass/cannot be changed by children. Once a method is marked as final, its behavior is fixed for all subclasses.

2
New cards

What is a final class?

A final class is a class that cannot be extended. Meaning no subclasses can be created from it.

3
New cards

Why would we ever use a final method?

We would use a final method whenever we feel like a parent class needs to protect its own internal data/needs to protect the way something is done, especially when that method controls important internal data.

We would also use a final method in order to prevent subclasses from changing rules that the parent class depends on and to maintain consistent behavior across subclasses.

4
New cards

When would we use a final class?

We would use a final class whenever we need to prevent a class’ behavior or data from being modified. We would also use a final class when we want objects of that class to be immutable, so that they cannot be changed after instantiation.

Final classes can also improve performance, because it allows the JVM to make certain optimizations.

Final classes also allow you to modify a class without having it break any of the subclasses.

5
New cards

What does the default equals() method check for?

It checks to see if two variables are referring to the same object in memory.

6
New cards

Why do we also need to override hashCode when we override equals()?

Becuase java uses hashCode in sets like HashMap and HashSet, so if two objects are equal, they must produce the same hashCode to make sure that these structures work correctly.

7
New cards

What is an abstract method?

An abstract method is a method without a body. It usually only declares the name of the method and the parameters.

Abstract methods are methods that need to be implemented by any classes that choose to extend another class containing abstract methods.

8
New cards

What is an abstract class?

An abstract class is a class that cannot be instantiated. It can only contain abstract methods and occasionally, concrete methods.

Note that none of the concrete methods in an abstract class use the default keyword, that only applies to interfaces.

It only represents a general idea, not an entire object.

9
New cards

Why would we use an abstract method?

Whenever a parent class knows that every subclass must provide a specific behavior, but the parent class doesn’t know how to define that behavior itself for each individual subclass.

10
New cards

Why would we use an abstract class?

We would use an abstract class whenever we need to define a shared structure and behavior for all classes of a related subtype. This is true for defining common fields/features that all subclasses share (like how all animals have an age, can eat, etc).

11
New cards

If a class contains even one abstract method…

THE CLASS MUST BE DECLARED ABSTRACT.

12
New cards

Why can’t we instantiate an abstract class?

Because abstract classes are only a concept, not a full implementation, so it is missing the necessary pieces necessary to instantiate objects from it.

13
New cards

If a class extends an abstract class, what must it do?

It must implement all abstract methods from its parent/grandparent.
If it does not do this, then it must be declared abstract.

14
New cards

What type of class cannot be made abstract?

Final classes cannot be made abstract.

15
New cards

Can a class be abstract even with no abstract methods?

Yes, a class is abstract so long as it is declared with the abstract keyword.

This means an abstract class can have no abstract methods, only concrete methods and fields/constants, but still be considered abstract.

16
New cards

What can an abstract class contain?

  • Abstract methods

  • Concrete methods

  • Instance variables

  • Static variables

  • No methods

  • Constants

  • Constructors

17
New cards

What do we commonly use abstract classes for?

We use them as containers for static utility methods, because those never need to be instantiated as objects anyways.

18
New cards