W2 Flashcards
(14 cards)
What is the difference between the equals method and the == operator?
- equals method determines if the content of the objects are equal/the same
- this will return True if the variables hold the same String value
- the == operator determines if two references are the same
- this is will return True if the two variables reference the same address
What will var1 == var2 evaluate to if var1 and var2 have reference types? have primitive types? Why?
- If var1 and var2 have reference types, we will get True if they reference the same item but False if they reference different items, regardless of the content
- If var1 and var2 have primitive types, we will get True if they hold the same item but False if they hold different items
All Java classes are subclasses of “Object”. We call Java classes “reference types” as opposed to “primitive types”. What is the difference between the two?
- Reference type - variables store references to the objects/values
- Primitive type - variables store the value directly
- A Java variable cannot hold a
String
value directly inside itself; it can only hold a reference to an object of typeString
. But typeint
is aprimitivetype. A Java variable can hold anint
value directly inside itself. - the primitive types all begin with alowercaseletter, and the reference types with anuppercaseletter
How is a constructor different from a method? Include information about what each does as well as the difference in syntax.
Constructor:
- Purpose: A constructor is a special type of block used to initialize an object of a class. Its primary job is to set up the initial state of an object by assigning values to its fields or performing other setup tasks. It is called automatically when an instance (object) of a class is created.
Method:
- Purpose: A method is a block of code that performs a specific task or action. Methods are used to define the behaviors of an object and can be called multiple times during an object’s lifetime.
What does it tell other programmers if you include a getter but not a setter for a variable?
If you also make the variable private, it means you do not want any other class in the program to change the value of the variable.
What is the benefit of making everything as close to private as possible?
Ideally, we want to be able to change or update how values are stored or how methods are implemented without the rest of the program having to be re-written as a result
Why do we consider private variables with public methods to be better encapsulated than public variables and private methods?
Private methods are called “helper” methods because they can only be called from within their own class. So they help the public methods do what they are supposed to do. It is not useful to have a private getter or setter since these are methods intended to be called from outside of the class.
Private variables enable us to hide how the values are stored and/or potentially change how the values are stored. They also allow us to intentionally prevent or allow other class to access the value of a variable and/or change its value.
Consider the variables x and y, where
Person x = new Person(…);
Student y = new Student(…);
Why can we use y in the place of a Person object, but not use x in the place of a Student?
Since y will inherit all of the methods and variables of the Person class, we can call person methods through y. In other words, the Student constructor will call the Person constructor, ensuring y will inherit everything from Person and subsequently Object. But the Person class does not have access to Student-specific methods and variables. So “x.getStudentNumber()” will not work
What does “override” and “shadow” mean?
We override a method when we create an alternative implementation of a method with the same signature in a subclass. A variable is shadowed when there exists two variables of the same type and the same name in two different scopes, like superclass/subclass or class variable/locally defined variable inside a method of the same class.
Give one of the reasons why it is useful to have all classes inherit from the object class.
This guarantees that all classes can be cloned, compared with an equals method, represented by a string (with the toString method) etc.
Consider variable z where
Person z = new Student(…);
If we call z.moogah() but moogah is not a Person method, where will the compilercheck for the moogah method? (In the Object class? In the Student class? etc.)
Since the type of z is “Person”, the compiler will assume that the missing method must be inherited and will look in Object for the implementation of the method
Why do we bother to write Javadoc instead of relying completely on inlinecomments?
Javadoc can be turned into a website that looks like the Oracle website with the “javadoc” command. They have a format that is predictable, easy-to-read, and allows you to fill in the blanks so the required information is included.
How does inheritance work in Java?
Each constructor calls the constructor of its immediate super class, all the way up to class Object. That way, any instance of a class also contains values for all of the variables it inherits and has access to all methods from classes that are higher on the inheritance hierarchy.
What’s different between compiled and interpreted languages?
An interpreter (like Python) translates/executes code one statement at a time, while a compiler (like C) translates the entire program once and executes any number of times