Unit 3 part 2 java as OOP Flashcards

1
Q

Ways of

Argument passing?

A
  1. Pass By value

2. Call by reference

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

Pass By value:

drawbacks too

A

copy of actual args passed to formal args.
Drawbacks:
In efficient storage allocation, copying arrays,objects is expensive.

When you pass a primitive type to a method, it is passed
by value.

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

Call by reference:

A

Formal param receive reference to actual data.
aliasing describes a situation in which a data location in memory can be accessed through different symbolic names in the program.
-Note that when we pass a reference, a new reference variable is created. We cannot change the reference to refer to some other object, as the received reference is a copy of the original reference.

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

object as parameter:

A

One of the most common uses of object parameters
involves constructors. Frequently, you will want to construct
a new object so that it is initially the same as some existing
object.
overloading constructors.

method (classname objvar){}

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

Returning objects:

A
classname method ( )
{

return obj_of_classname;
}

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

worry of going out of scope?

Objects life span?

A

Since all objects are dynamically allocated using new, you
don’t need to worry about an object going out-of-scope
because the method in which it was created terminates. The
object will continue to exist as long as there is a reference to
it somewhere in your program. When there are no references
to it, the object will be reclaimed the next time garbage
collection takes place.

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

Features offered by encapsulation:

A

-encapsulation links data with the code that
manipulates it
-provides access control.
control what parts of a program can access the
members of a class

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

Black Box

A

Inner workings are not open for tampering

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

Access modifier:

name java’s access modifiers

A

Determines access, attached to declaration.

public, private, protected.

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

public:

A

When a
member of a class is modified by public, then that member
can be accessed by any other code.

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

Private:

A

then that member can only be

accessed by other members of its class.

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

main and its access modifier? why?

A

main( ) has always been preceded by the
public modifier. It is called by code that is outside the
program—that is, by the Java run-time system.

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

Default access modifier:

A
When no
access modifier is used, then by default the member of a
class is public within its own package, but cannot be
accessed outside of its package
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

protected:

A

Comes in picture during inheritance

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

static is used for?

example?

A

Allows using a class member independently of any object.

  • When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object
  • methods and variables can be static

main( ) is declared as static because it must be
called before any objects exist.

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

Static variables?

A

Instance variables declares as static are essentially global variables.

  • when object of class is created, copy of these static variables is not created.
  • All instances of class share same static variables.
17
Q

Static methods rules?

A

• They can only directly call other static methods of
their class.
• They can only directly access static variables of
their class.
• They cannot refer to this or super in any way. (The
keyword super relates to inheritance and is described in
the next chapter.)

18
Q

Static block and use of static in class:

A

As soon as class is loaded, all static statements are run.

static BLOCK:

static{ initialise stuff; code; }

If you need to do computation in order to initialize your
static variables, you can declare a static block that gets
executed exactly once, when the class is first loaded.
19
Q

Access static method, var outside class:

A

classname.method( )

specify the name of their
class followed by the dot operator
20
Q

final :

what does it do?

who can be declared as it?

A

A field can be declared as final. Doing so prevents its
contents from being modified, making it, essentially, a
constant. This means that you must initialize a final field
when it is declared. You can do this in one of two ways: First,
you can give it a value when it is declared. Second, you can
assign it a value within a constructor.
uppercase identifiers for final fields

method params, local vars can also be declared as firelds.

Declaring a parameter final
prevents it from being changed within the method. Declaring
a local variable final prevents it from being assigned a value
more than once.

final for methods is different.

21
Q

Nested class:

Scope and lifespan and

A
It is possible to define a class within another class; such
classes are known as nested classes.
The scope of a nested
class is bounded by the scope of its enclosing class.
A nested class has access to the
members, including private members, of the class in which it
is nested. However, the enclosing class does not have access
to the members of the nested class.
22
Q
It is also possible to declare a
nested class that is local to a block.
A

I dont understand

23
Q

explain types of nested class:

A

Static and non static:

Static must access the nonstatic members of its enclosing class through an object. That
is, it cannot refer to non-static members of its enclosing
class directly.
24
Q

Inner class:

A
An inner class is a non-static nested class. It has access to all
of the variables and methods of its outer class and may refer
to them directly in the same way that other non-static
members of the outer class do.
25
Q

Creating Instance of outer class:

A
It is important to realize that an instance of Inner can be
created only in the context of class Outer. The Java compiler
generates an error message otherwise. In general, an inner
class instance is often created by code within its enclosing
scope, as the example does.