Lecture 6 and 7 - Constructors , Objects , Key features of OOP Flashcards

1
Q

What is a java constructor?

A

constructor is invoked at the time of object creation. It constructs the values i.e. initialises data for the object.

Constructors could also be overloaded.

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

Must for a constructor:

A

name must be same as its class name

must have no explicit return type

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

What happens if a constructor is omitted in the class?

A

In this case, the compiler automatically create a default no-arg constructor.

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

How do you create an object?

A

Examples

Rectangle rectSmall = new Rectangle();
or:
Rectangle rectLarge;
rectLarge = new Rectangle(10.0,15.0);

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

Steps for creating an object:

A

Declaration
Instantiation
Initialization

Accessing instance members (i.e, attributes and methods):

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

Explain instantiation:

A

The new keyword is used to create the object, i.e., a block of memory space is allocated for this object.

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

Explain initialization:

A

The new keyword is followed by a call to a constructor which initializes the new object.

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

Explain how to access instance members:

A

First, create an instance/object of the class;

Then, use the dot operator (.) to reference the member
attributes or member methods

ex.
Rectangle rect1 = new Rectangle(10,15,2,4);

double w = rect1.width;
rect1.move(2.5,0);

double area = new Rectangle(10,15).getArea();

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

What can instance methods do ?

A

can access instance attributes and instance methods directly.

can access static attributes and static methods directly.

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

What can static methods do and not do ?

A

can access static attributes and static methods directly.

CANNOT access instance attributes or instance methods directly, they must use an object reference.

CANNOT use the this keyword as there is no instance for this to refer to

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

The arguments of a method could be:

A

Primitive Data Type

Reference Data Type (Class, interface, array, enum)

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

Explain passing by value :

A

The method gets a copy of the variable you pass. If you change the copy inside the method, the original value remains the same.

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

What is auto-boxing and unboxing

A

Autoboxing Converting a primitive value (an int, for
example) into an object of the corresponding wrapper class(Integer).

Unboxing Converting an object of a wrapper type (Integer) to its corresponding primitive (int) value.

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

How does java apply auto-boxing and unboxing?

A

Assignment

Method invocation

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