Basic Objects Flashcards
What is OOP?
Object Oriented Programming: a programming model based on the construction and use of objects- a program built under this model can be seen as a set of objects interacting together
What makes up a class?
When you create a class, you’re actually defining a new type of data (i.e. an object)
Like any other type, a class has:
- a name (starts with an uppercase by convention
- some kind of data that is stored: a collection of variables
- some kind of actions that can be performed on the data: a collection of methods
What are instance variables?
The object’s data: each instance will have its own specific set of values for the instance variables
What do variables of an object type contain?
A reference, not the object itself: just like we have seen for arrays and Strings, because they are objects as well
What are instance methods?
methods that can only be used on instances. These methods have access to the instance variables of that specific instance (static keyword is omitted)
Why do we use the ‘this’ keyword?
In an instance method, if you have a local variable with the same name as an instance variable, the local one will be used. You use the keyword to represent the current instance of the object.
What is the toString() method?
public String toString()
This method is automatically called by Java to get a String representation of your object (called when printing or concatenation is needed with your object)
What are constructors?
A special method that is used to instantiate an object. We normally use it to initialize the instance variables. They have no return type at all (not even void) and must have the exact same name as the class. You can define multiple constructors so long as they have different signatures
What is the purpose of the public keyword?
makes the method/variable accessible outside of the class
What is the default constructor?
Provided by Java in case no constructor is defined in a class. Does not do anything except instantiate the object. It disappears as soon as one constructor is defined in the class. This means that you cannot use a constructor with no parameters if you declared only one constructor with parameters
What is the distinction between functions and methods?
functions are static, methods work with instance variables because they are not static
What are the access modifiers?
public, private, protected-private
What do public and private mean?
public: means any code, anywhere, can access or use it
private: means only methods in this same class can access or use it
use private for instance variables and public for most instance methods
What is encapsulation?
The idea that you can restrict access to some of the object’s fields. It protects the internals, preventing other classes from misusing the object.
What are fancy names for get/set methods?
accessor/mutator