ECM2414 Nested Loops Flashcards

1
Q

OOP benefits

A
  • Protected private and public variables in created objects (Encapsulation)
  • (Inheritance)
  • Organises code into classes and objects
  • (Polymorphism)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Issue with encapsulation

A
  • Classes in OOP should usually be specialised, should only include code relating to that type’s specific function
  • With two tightly coupled classes
    ○ Rely heavily on the members in each class
    ○ Lots of work involved in creating functions to access those members
    ○ Occurs more frequently in GUI programming

Nested classes overcome this issue by allowing classes to be defined within classes

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

Static nested class

A

does not have access to instance members

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

inner class

A

does not have access to instance members

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

Instance members

A

refers to variables that are specific to that specific object

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

What modifiers can be used to declare a standard outer class (top-level class)

A

public

package private (the default/implicit)

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

modifiers for nested classes

A

public
private
protected
package private

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

protected modifier

A

an access modifier that limits the visibility of functions and variables to a specific class and its derived classes

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

package private

A

other members of the same package have access to the item. package-private is the default access modifier and does not have a keyword, because package is used to specify the package for a class or interface.

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

Why use nested classes

A

Loosely linked classes located in one place
- Links can be made explicit between classes that are linked to only one other class by making it a nested class

Increases encapsulation on occasion (hiding data for private purposes, you are not able to take access of every data at any time in your code, which is safe)

It makes code more readable (classes pertaining to a top level class are all found under that top level class, looking stately)

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

Nested classes will produce two class files on compilation

Take the example of a top-level class OuterClass and its nested class InnerClass. What would the class files be named?

A

OuterClass.class
OuterClass$InnerClass.class

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

How can static nested classes be accessed?

A

using the enclosing class name
e.g. OuterClass.InnerClass

from within the class however, you can construct the nested class directly with just ‘InnerClass’

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

“this”

A

refers to the inner type instance

OuterClass.this refers to the instance of the surrounding (outer) class, when within the inner class code blocks

In a similar vain to how regular public variables are accessed within a class, not just nested classes

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

Local classes

A

Local classes are defined and exist solely within a block (usually a method)

Local class declaration cannot contain public, protected, private or static access modifiers

Cannot access method local variables (unless passed as arguments) unless they are declared as final

Can access all the members of the outer class instance like standard inner class members

External access to the class cannot be made

explicit finals initialised outside the class within the block can be accessed

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

Anonymous classes

A

Potentially one of the most convoluted aspects of java

Classes that are declared without any class name at all (hence the name anonymous)

Not only may these be defined within a method, they may also be defined within an argument to a method

Uses the ‘new’ keyword, then uses curly bracers to extend the base implementation

When you implement an interface, there is no constructor, so you use an empty pair of parentheses

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

Why not use a 0-6 of a byt instead of enum class?

A

Because:
- Must map between the byte value and the day
- Must check for range errors