week02 Foundational OOP Flashcards
What is the role of the main
method in Java?
It’s the starting point of the program. It’s where objects are created and the program begins execution.
What are the requirements for the main
method in Java?
It must be public and static, with a single String array argument(String[] args
).
What are two key characteristics of the main
method?
The main
method must exist for the program to run and should initiate the program’s operations by creating objects and calling methods.
What is a constructor in Java?
A constructor is a special method to create and initialize an object. It has the same name as the class and sets up initial values for object attributes.
What are the key characteristics of a constructor?
A constructor does not have a return type, not even void. It initializes all instance variables with values.
How is a constructor different from a regular method?
Unlike methods, a constructor is called automatically when a new object is created and does not need a return type.
How does a constructor use parameters in Java
It uses parameters passed to it to assign values to the object’s attributes using this
keyword.
What is the equivalent of a constructor in Python?
The \_\_init\_\_
method in Python is like a constructor in Java. It initializes object’s properties.
The instance variables are declared in the constructor.
True or false?
False.
The instance variables are declared in the class.
What are instance variable in Java?
Instance variables are data elements of a class. Each object has its own copy.
Where do you declare instance variables?
Declare them at the top of a class. They should be outside any method or constructor.
How do you assign initial values to instance variables?
Set initial values in the constructor, which is called when creating an object.
What is the scope of instance variables?
The entire class. All methods and constructors in the class can use them.
What else can instance variables be called?
Fields, attributes, or data members. They store information about an object’s state.
Do instance variables have a data type?
Yes, each instance variable must be declared with a data type.
What is the purpose of instance variables?
They represent the state of an object of instance and hold values specific to each object.
What happens to instance variables that are not initialized?
They get a default value from Java: 0 for numbers, false for boolean, null for objects.
How are local variables different from instance variables regarding initialization?
Local variables do not get a default value and must be initialized before use.
What does Java do if you don’t write a constructor for a class?
Java creates a default constructor that sets all instance variables to default values.
What happens to the default constructor when you write your own constructor(s)?
If you write any constructors, Java does not create a default one.
Java class can only have one constructor.
True or false?
False. You can have multiple constructors and they can call each other with this
.
What are the default values for data types in Java?
・byte, short, int, long: 0(or 0L for long)
・float, double: 0.0(or 0.0f for float)
・char: ‘\u0000’(null character)
・boolean: false
・object: null
What is a Java object?
It’s an instance of a class that has its own space in memory.
What determines the state of a Java object?
Its state is determined by the values stored in its instance variables.
How do you access a Java object?
Through a reference, which is a variable that holds the memory address of the object.
What part of an object is visible to other classes?
Only the parts marked as public. Private parts are hidden inside the object.
What is the difference between an object’s public interface and private implementation?
Public interface is what other classes can see and use. Private implementation is internal working that other classes cannot access.
What does encapsulation mean in Java?
Encapsulation is bundling data with methods that operate on the data.
How are constructors related to encapsulation?
Constructors set initial values for an object’s state within the safe boundaries of encapsulation.
What is information hiding in Java?
It’s when an object hides its state and can only be accessed through its public methods, not directly.
What are visibility modifiers in Java?
They are keywords that define an object’s accessibility. Common modifiers are public, private, abd protected.
How do public and private visibility modifiers work?
Public members can be accessed from anywhere. Private members can only be accessed within their own class.
What is the rule of thumb for encapsulationg your classes?
Make variables private and methods public. This protects the integrity of the data.
How can encapsulated objects be visualized?
Think of them as a black box. You can’t see inside, but you can use its features.
What’s the difference between a class’s public interface and private implementation?
The public interface is what you can use from outside the class, like buttons on a machine. Private implementation is the hidden inner workings inside the class, like the machine’s engine.
What are accessors in Java?
Accessors, also know as getters, are methods that let you read private data. They return a value and have no parameters.
How do you write an accessor method?
Start with get
followed by the variable name, make it public, and return the variable value. Example:public int getPrice() { return price;}
.
What are mutators in Java?
Mutators, also know as setters, are methods that let you change private data. They don’t return a value but take a parameter.
How do you wirte a mutator method?
Start with set
followed by the variable name, make it public, and assign the parameter value to the variable. Example:public void setFirstName(String newName) {this.firstName = newName;}
.
Why do we use accessors and mutators?
They enforce encapsulation. Accessors allow read-only access to variables, and mutators enable controlled changes to variables.
Why should the use of mutators be restricted?
Limiting mutability ensures class stability and maintains valid state. Mutators should be used carefully to prevent invalid object states.
What should mutator contain?
Mutators must include logic to check if the new value is valid. They should do nothing if the value is not acceptable.
What are side effect in methods?
Side effects are changes that a method makes outside its main job.
i.g. Changing variables that are not directly part of the method or engaging in activities like reading from or writing to a file.
Accessors should have side effects.
True or False?
False. Accessors must not have any side effects. They should only return values.
What shoud mutator methods change?
Mutotar methods should only change the value of the instance variable(the implicit argument) they are meant to modify, not any other state or object.
What is the purpose of the toString
method in Java?
To provide a text representation of the object’s state, mainly for debugging and logging purposes.
True or False?
“The toString
method should contain the values of some instance variables.”
False. The toString
method should contain the values of all instance variables.
What happens when you pass an object to System.out.println()
?
Java automatically calls the object’s toString()
method to get and print the string representation.
True or false?
These two lines equivalent:System.out.println(myGuppy);
and System.out.println(myGuppy.toString());
True. Both lines of code will result in the toString
method being called and the output being printed.
What is Java class?
A Java class is a data structure that encapsulates data and methods to work on that data. It represents a specific entity or concept.
True or false?
“A Java class should expose all its data to the outside.”
False. A Java class should encapsulates its data, providing controlled access through methods.
What is the purpose of a constructor in a Java class?
A constructor initializes a new object to ensure it has a valid state before being used.
True or false?
“Java will provide a default constructor for a class if none is provided. “
True. If no constructors are explicitly defined, Java will provide a default constructor.
What does this
refer to in Java?
this
refers to the current object whose method or constructor is being executed.
Why do we use this
inside methods?
To clarify that a variable or method belongs to the current object, especially when local variable might overshadow instance variables.
True or false?
“this
can be used to call another constructor within the same class.”
True. this
is used to invoke antoher constructor in the same class.