Final exam Flashcards
4 phases running a Java program
- edit: store program in .java file
- compile: use java compiler (Javac) to create bytecodes from source code to .class file
- load class: read bytecodes in .class
- execute: Java Virtual Machine (JVM) translates bytecodes into machine language
Fields
State or properties of object.
Methods
Behavior or actions of an object. May model concrete things, abstract things or tasks.
Classes
Classification of objects, describes commonality of sets of similar objects.
Object
Instance of a class.
Static fields
Independent of object, exists once per class and shared by all objects of class.
Constructor
Speical method, to create object of class (via new).
Static methods
Independent of any object, cannot access fields or methods. Connected to class.
Void methods
Type of method that returns nothing.
Array
Ordered list of variables of the same type. Size of array is predefined.
Program
Collection of interacting objects (from classes).
How to access fields?
objectName.fieldName
How to invoke methods?
objectName.methodName()
Which types are there + examples?
- primitive type: int, long, short, boolean, double, float, char (support operations +, -, *)
- reference type: classes
String is special type: support concatenation (+)
A variable ___ to an object, and ___ an address.
A variable refers to an object, and contains an address.
Which type has an address?
Reference type, stored as objects.
What does ‘ ==’ check between two variables?
Whether they have the same address.
What does ‘ c1.equals(c2)’ check between two variables?
Check the equivalence in fields.
Aliasing
Two different variables for the same object (address), with ‘ c1 = c2’
Garbage collection
Removes unused addresses on its own.
this Object
Default calling object. Inside class passed implicitly.
What are the Object Oriented fundamentals?
Encapsulation, Separation of Concerns and Loose Coupling.
Encapsulation
Information hiding from user, to keep people from breaking your code.
3 methods for encapsulation + explain
- change state only via behavior (getters and setters)
- hide implementation types (private methods)
- composition = identify what varies and separate that from what stays the same
Composition
Identify what varies and separate that from what stays the same. Create class that contains multiple fields, instead of multiple interfaces/subtypes.
Separation of Concerns
Keep things structured and clear. Classes should do one thing only.
Loose coupling
Achieved by design that promotes single-responsibility and separation of concerns. For example, interfaces.
Advantages of loose coupling
- allow to replace and extend your code
- can be consumed and tested independently of other classes
Getters
Enable read-access to private fields via its behavior (method).
Setters
Update state (field) of object via its behavior (method).
Interfaces
Common means for unrelated objects to communicate with each other. Only behavior, no implementation.
What can be in an interface?
An interface has abstract methods, default methods and constant definitions.
What is not possible with an interface?
An inteface has no constructors and instance variables.
Contracts
Maximum functionality a client can use, minimum functionality a server has to provide.
Goal of interfaces
Minimize dependencies between different parts by separating concern of implementation from concern of user.
How to use interface?
Implement it in a class and override its methods.
“public class <className> implements Interface1, Interface2, ..."</className>
Subtype rule
If method/variable requires reference of type A, then subtype of A may be provided.