Classes, Objects, and Methods Flashcards
What is the difference between a class and an object?
A class is a logical abstraction that describes the form and behavior of an object.
An object is a physical instance of the class.
How is a class defined?
A class is defined by using the keyword class.
Inside the class statement, you specify the code and data that comprise the class.
What does each object have its own copy of?
Each object of a class has its own copy of the class’ instance variables.
Using two separate statements, show how to declare an object called counter of a class called MyCounter.
MyCounter counter;
counter = new MyCounter();
Show how a method called myMeth( ) is declared if it has a return type of double and has two int parameters called a and b.
double myMeth(int a, int b) { // …
How must a method return if it returns a value?
A method that returns a value must return via the return statement, passing back the return value in the process.
What name does a constructor have?
A constructor has the same name as its class.
What does new do?
The new operator allocates memory for an object and initializes it using the object’s constructor.
What is garbage collection and how does it work?
Garbage collection is the mechanism that recycles unused objects so that their memory can be reused.
What is this?
The this keyword is a reference to the object on which a method is invoked. It is automatically passed to a method.
Can a constructor have one or more parameters?
Yes.