1/17
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
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.
What is a final class?
A final class is a class that cannot be extended. Meaning no subclasses can be created from it.
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.
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.
What does the default equals() method check for?
It checks to see if two variables are referring to the same object in memory.
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.
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.
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.
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.
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).
If a class contains even one abstract method…
THE CLASS MUST BE DECLARED ABSTRACT.
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.
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.
What type of class cannot be made abstract?
Final classes cannot be made abstract.
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.
What can an abstract class contain?
Abstract methods
Concrete methods
Instance variables
Static variables
No methods
Constants
Constructors
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.