METHODS AND OBJECTS Flashcards

1
Q

What is method overloading?

A

Method overloading is a feature that allows the creation of several methods with the same name which differ from each other in the type of the input and the output of the function.
The methods can be distinguished by their method signatures

public class DataArtist {
 ...
 public void draw(String s) {
 ...
 }
 public void draw(int i) {
 ...
 }
 public void draw(double f) {
 ...
 }
 public void draw(int i, double f) {
 ...
 }
}
 The method signatures: draw(int), draw(double), draw(int,double)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Create a constructor for the following class and use it to form an object:

public class Bicycle{
  int gear;
  int cadence;
  int speed;
}
A
public Bicycle(int startCadence, int startSpeed, int
startGear) {
 gear = startGear;
 cadence = startCadence;
 speed = startSpeed;
}
Bicycle myBike = new Bicycle(30, 0, 8); (object)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How are constructors with similar names differentiated?

A
As with methods, the Java platform differentiates
constructors on the basis of the number of arguments in the list and their types. 
Eg public Bicycle(int startCadence){}
public Bicycle(int startCadence, int startSpeed){}
public Bicycle(){}

Based on the number and type of the arguments that you pass into the constructor, the compiler can determine which constructor to use. Thus the compiler knows that when you write:

new Bicycle( 100, 200);
it should use the constructor that requires two integer argument
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What happens when a class does not have a constructor?

A
• It is not mandatory to provide a constructor for a class.
• However, the compiler automatically provides a noargument, default constructor for any class without
constructors. eg Bicycle (){}
• This default constructor will call the no-argument
constructor of the superclass. Ina scenario where the
superclass doesn't have a no-argument constructor and the class has no explicit superclass, it will call its implicit superclass of Object, which does have a no-argument constructor
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are parameters and arguments and what is the difference?

A

• Parameters: the list of variables in a method declaration.
• Arguments: the actual values that are passed in when the method is invoked.
Parameters are used in the method body and at runtime will take on the values of the arguments that are passed in.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the rules regarding the naming of parameters?

A
  • The name of a parameter must be unique in its scope
  • It cannot be the same as the name of another parameter for the same method or constructor
  • It cannot be the name of a local variable within the method or constructor.
  • However, a parameter can have the same name as one of the class’s fields. In such a case, the parameter is said to shadow the field.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How does one pass an arbitrary number of arguments to a method?

A
• To pass an arbitrary number of arguments to a method, one can use a construct called varargs.
• Usually, this is done when one does not know how many of a particular type of argument will be passed to the method.
• This acts as a shortcut to creating an array manually.
• To use varargs, you follow the type of the last parameter by an ellipsis (three dots, ...), then a space, and the parameter name. 
E.g.:
public polygon(Point... corners) {
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the three sections of creating an object?

A

• Declaration: The first parts of objects are variable declarations that associate a variable name with an object type.
• Instantiation: The new keyword is a Java operator that creates the object.
• Initialization: The new operator is followed by a call to a constructor, which initializes the new object.
E.g.:
Point one = new Point(23, 94);
• This line creates an object one of the Point class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Describe class instantiation

A
  • Class instantiation can also be called object creation.
  • The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory.
  • This operator also invokes the object constructor.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Describe object initialization and give an example

A
• This is the assigning of values/arguments initially declared in the instantiated class from which an object is created.
• These values usually override the initial values allocated.
E.g.
public class Point {
 public int x = 0;
 public int y = 0;
 //constructor
 public Point(int a, int b) {
 x = a;
 y = b;
 }
}
Point one = new Point(23, 94);, assigns 23 and 94 to x and y respectively
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

List some ways in which objects can be utilized

A
  • using the value of one of its fields
  • changing one of its fields
  • calling one of its methods to perform an action
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How can one reference an object’s field both from within the object and outside and give examples

A

• Object fields are accessed by their name:
System.out.println(“Width and height are: “ + width + “,
“ + height);
• In this case, width and height are fields accessed from an object.
• Code that is outside the object’s class must use an object reference or expression, followed by the dot (.) operator, followed by a simple field name, as in:
objectReference.fieldName

System.out.println(“Width of rectOne: “+ rectOne.width);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can one call an object’s method and give examples

A

• One can use an object reference to invoke an object’s
method.
• To do this, simply append the method’s simple name to
the object reference, with an intervening dot operator (.).
• Also, one needs to provide, within enclosing parentheses, any arguments to the method.
• In case of no arguments, empty parentheses are used.
E.g.:
(with arguments)
• objectReference.methodName(argumentList);
(without arguments)
• objectReference.methodName();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

In what instances does a method return to the code that evoked it?

A

When it
• completes all the statements in the method,
• reaches a return statement, or
• throws an exception

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How is this keyword used and give examples

A
• Within an instance method or a constructor, this is a reference to
the current object — the object whose method or constructor is
being called.
• One can refer to any member of the current object from within an
instance method or a constructor by using this:
public class Point {
 public int x = 0;
 public int y = 0;
 (constructor)
 public Point(int x, int y) {
 this.x = x;
 this.y = y;
 }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly