Object Oriented Concepts Flashcards
What is an object?
something that can be described by both data attributes (e.g., age, weight) and behavior
(e.g., eat, talk).
Explain this class diagram
The
top compartment contains the name of the thing being modeled…in our case a cat. The next
compartment contains a list of the attributes we are including in our model. And the bottom
compartment contains a list of relevant behaviors.
what is a class?
programming construct used to create objects. It defines the structure and behavior (data and code) shared by a set of objects.
It creates a new data type.
What is encapsulation?
a protective wrapper that controls access to the code and data through a well-defined interface
what does it mean if something is a “member” of a class?
the code and data in the class. Member variables and member methods
whats the difference between a public and private method?
public can be accessed outside of the class. Private methods can only be accessed by code that is a member f hte class.
What is inheritance
process by which one object acquires the properties of another object.
Important because it supports the concept of hierarchical classification (e.g. dog inherits mammal inherits animal)
What is polymorphism?
“one interface multiple methods”
one interface used for a general class of actions
What relationship between a class and an object?
object is instance of a class.
Class is template for an object
what does ‘new’ operator do
dynamically allocates (meaninning allocates at run time) memory for an object
Box mybox = new Box();
Box mybox; // declare reference to object
mybox = new Box(); //allocates a Box object
What is a constructor?
Defines what occurs when an object of a class is created.
class name followed by parentheses specifies the constructor for the class
Explain this situation:
Box b1 = new Box();
Box b2 = b1;
Both point to same object. Changing b2 will update for b1 too.
Whats difference between parameter and argument?
parameter is the variable defined by the method that receives the value.
argument is the value passed to a method when its invoked
How write the constructor method?
same name as the class with capital
Box () {
….
}
explain ‘this’
refers to the current object
this.width = w
what is garbage collection?
releasing of memory of objects no longer needed.
Java does it automatically
What is method overloading
two methods with different parameter declarations. When called, Java executes the one with matching parameters
one way Java supports polymorphism.
for overloading, will java do automatic type conversions (e.g. int to double) to execute a method?
yes
if no test(int) exists, it will elevate the argument to a double to execute test(double)
Explain what it means what objects passed as parameters are call-by-reference?
When primitive types are passed, it creates a new local variable, and updates to that variable in that method do not affect within the invoking method.
In contract, objects pass the reference to that data. So updates in the current method will update the reference, and thus affect the invoking method.
What does recursion mean with respect to a method?
Defining something in terms of itself. A method calling itself.
what is a package?
a group of classes
How is private and public used for access control to instance variables?
public means it can be accessed and edited outside of the class. object.variable = 10 sets the variable to 10.
private means that variable can only be set within that class through a defined method like setNum. object.setNum(10)
public int a;
private int b;
what does static do?
methods and variable declarations that can be used before any instances of the class are created. They run first on their own.
what does ‘final’ do?
prevents its contents from being modified.
Useful for parameters and local variables to prevent it from changing within the methods.
NOT useful for object parameters which are reference variables. Can still edit underlying.
final int FILE_NUM = 1;
common to use uppercase for constants
What’s an inner class?
A class created within another block scope. Could be created within another class, method, or even loop.
It can access all the attributes from the outer class, but the outer does not have access to the inner.
what is a command line argument?
info passed to program when you run it, stored in array args[]
what are variable-length arguments and how to use them and why
if want to create a method that takes in zero or more arguments because you’ll just take in what you can get.
used with “…”
static void vaTest(int … v)
v is declared as an array with variable amount of data.
can call with parameters and variable length parameters but variable needs to be last
int test(int a, int b, double c, int … vals)
What is containment?
using other objects as class members
Class compilation
classes usually maintained as separate files (allows for parallel development)
classes compiled separately
this
not sure really
Can return an object from local method?
Yes Java will copy the reference before method variable destruction.