MOOC Week 5 Flashcards
What is OOP?
Object-oriented programming is primarily about isolating concepts into their own entities or, in other words, creating abstractions.
What is an object and how is it created?
An independent entity that contains both data (instance variables) and behavior (methods). It is created using the keyword “new”, this keyword also creats a new memory location for the new object. An object is created on the basis of the class constructor.
How do objects interact with one another?
Through method calls — these method calls are used to both request information from objects and give instructions to them.
What is a class?
A blueprint for creating objects, providing initial values for state (member variables or attributes), and implementations of behavior (member functions or methods).
What keyword is used for a constructor to be called from another constructor? Why is this used?
this() Used for constructor overloading which removes repetitive code, making it more efficient and readable.,
How to overload methods?
Have the same name but different parameters.
public void growOlder() {
this.growOlder(1);
}
public void growOlder(int years) {
this.age = this.age + years;
}
Declaring a primitive variable does what?
Causes the computer to reserve some memory where the value assigned to the variable can be stored.
What is a reference variable?
Any object instanced from a class.
What is the main difference between primitive and reference variables?
The basic difference is that primitive variables store the actual values, whereas reference variables store the addresses of the objects they refer to. Primitive variables are immutable whereas reference variables are a reference to the variable’s data, its object and doesn’t contain the object itself.
If you have created object variables for a class, how can you create constructors, getters, and setters automatically in NetBeans.
Go inside the code block of the class, but outside of all the methods, and simultaneously press ctrl and space.
Contruct a program that prints the current date, what premade java class would you need to import.
import java.time.LocalDate;
public class Example {
public static void main(String[] args) { LocalDate now = LocalDate.now(); int year = now.getYear(); int month = now.getMonthValue(); int day = now.getDayOfMonth(); System.out.println("today is " + day + "." + month + "." + year); } }
What is abstraction in programming?
Through the process of abstraction, a programmer hides all but the relevant data about an object in order to reduce complexity and increase efficiency.
What is the difference between “==” and equals() method?
“ == “ operators is used for reference comparison (address comparison) between reference varaibles while .equals() method for content comparison.
What is type cast?
Type casting is when you assign a value of one primitive data type to another type.
What happens when you type cast a reference variable?
And casting a reference variable doesn’t touch the object it refers to but only labels this object in another way, expanding or narrowing opportunities to work with it. Upcasting narrows the list of methods and properties available to this object, and downcasting can extend it.
A reference is like a remote control to an object. The remote control has more or fewer buttons depending on its type, and the object itself is stored in a heap. When we do casting, we change the type of the remote control but don’t change the object itself.