OOP Intro Flashcards
What is the difference between a class and an object?
A class is the blueprint for an object. An object is an instance of a class.
What does instantiating mean?
“instantiating” is creating an object instance of a class.
What is the difference between stack and heap memory?
Stack is used for storing primitive types (numbers, boolean and character) and variables that store references to objects in the heap. Variables stored in the stack are immediately cleared when they go out of scope (eg when a method finishes execution). Objects stored in the heap get removed later on when they’re no longer references. This is done by Java’s garbage collector.
What are the problems of procedural code
Big classes with several unrelated methods focusing on different concerns and responsibilities.
What is encapsulation?
It suggests that we should bundle the data and operations on the data inside a single unit (class).
Why should we declare fields as private
We want to keep implementation details secret and prevent objects from entering bad state.
What is abstraction?
Suggests that we should reduce complexity by hiding the unnecessary implementation details. Encourages users to work with an interface.
What is coupling?
The level of dependency between software entities (eg classes). The more our classes are dependent on each other, the harder it is to change them.
How does the abstraction principle help reduce coupling?
By hiding the implementation details, we prevent other classes from getting affected when we change these details
What are constructors?
Called when we instantiate our class. We use them to initialize our objects.
What is method overloading?
Declaring a method with the same name but with different signatures. The number, type and order of its parameters will be different.
What are static methods?
These methods are accessible via classes, not objects.
How can we make one class inherit from another?
Through use of the extends keyword.
What does the hashCode() method of an object return?
It returns a numeric value that is calculated based on the address of the object in memory.
What is a default constructor?
A constructor without any parameters. If we don’t create it, the Java compiler will automatically add one to our classes.