Classes & Objects Flashcards

1
Q

Instance of a class

A

An object whose type is that class.

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

Coupling

A

The measure of the degree of interdependence between the various classes in your project. A good project will have low coupling.

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

Cohesion

A

The degree to which a class deals with related tasks. Similar tasks should be contained within a single class. A class is cohesive if its features support a single abstraction.

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

DRY

A

This stands for Don’t Repeat Yourself! It means that whenever you find yourself writing the same piece of code again, you should create a single method to perform that function and call it wherever that action needs to be performed. Note that DRY is the opposite of WET, or Write Everything Twice, in computer science.

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

CRC card

A

An index card representing a class that lists its responsibilities and collaborating classes.

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

Principle of least surprise

A

It is important to follow existing conventions and practices to improve the readability of your code and make it easiest to understand.

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

Abstraction

A

One of the key concepts of object-oriented languages like Java. Its main goal is to handle complexity by hiding unnecessary details (making them abstract) from the user.

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

Unified Modeling Language (UML)

A

A notation for specifying, visualizing, constructing, and documenting the artifacts of software systems.

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

Immutable class

A

A class without a mutator method.

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

Side effect

A

An effect of a method other than returning a value.

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

Static variable

A

A variable defined in a class that has only one value for the whole class, and which can be accessed and changed by any method of that class.

Static variables are most commonly used for declaring constants and keeping count of how many instances of a class have been generated. For example, given a bankAccount class, the following static variable is used to assign a unique account number to every new back account that’s created:

private static int accountNumberGenerator = 100;
public BankAccount(String name, int balance)

{
this.name = name;

this.balance = balance;

accountNumber = accountNumberGenerator;

accountNumberGenerator++;
}

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

Static method

A
A method with no implicit parameter. That is, they are not invoked on an object. A typical example of a static method in Java is the sqrt( ) method in the Math class which is invoked as:
Math.sqrt(x);
\* Note, when calling a static method you suppy the name of the class that contains it. 
\*\* Also note, the main method in Java is always static since when a program starts, there aren't any objects.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Package

A

A collection of related classes. The import statement is used to access one or more classes in a package.

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

Instance variables

A

A variable defined in a class for which every object of the class has its own value. An instance variable consists of the following parts:

  • An access specifier (these should always be made private)
  • The data type such as String or double
  • A programmer given name such as bankAccountBalance
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Access specifier / modifier

A

A reserved word that indicates the accessibility of a feature, such as private or public. As a result, access modifiers ensure encapsultation - one of the fundamental aspects of object oriented programming.

As a general rule, you should:

  • Make all instance variables private
  • Make all methods public
    • A valid exception here would be “helper” methods that you don’t want other classes to access. They can be made private as well.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Return value

A

The value returned by a method through a return statement.

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

Encapsulation

A

The hiding of implementation details.

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

Association

A

Association is the relation between two different classes that is established via their objects. Association can be in many forms:

  • One-to-One
  • One-to-Many
  • Many-to-One
  • Many-to-Many
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Body

A

All statements of a method or block.

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

Constructor / Construction argument

A

A constructor is like method to instantiate and initialize an object. Yes, even if the constructor doesn’t accept any arguments, it should initialize the instance variables. For example, the constructor of a BankAccount Class may not have any parameters, but will still initialize the balance to zero when a new BankAccount object is created.

There are a few differences between a constructor and a method:

  1. The name of the constructor is always the same as the Class
  2. There can be more than one constructor with the same name
  3. Constructors do not have a return type

Note that if you don’t provide a constructor for your class, then Java provides one for you by default. The syntax is:
ClassName reference = new ClassName( );
However, once you supply a constructor, it is no longer available!

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

No-argument constructor

A

A constructor that takes no arguments.

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

Public interface

A

The content of a class declaration that any programmer can view and use. It consists of the public constructors and methods of the class.

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

Parameter variable

A

A variable of a method that is initialized with a value when the method is called.

24
Q

Local variable

A

A variable whose scope is a block of code. When a variable is declared inside a method, it must be initialized before it’s used or an error will be thrown.

25
Q

Garbage collector

A

Automatic reclamation of memory occupied by objects that are no longer referenced.

26
Q

Initialize

A

Set a variable to a well-defined value when it is created.

27
Q

Implicit parameter

A

The object on which a method is invoked. For example, in the call x.f(y), the object x is the implicit parameter of the method f.

28
Q

Explicit parameters

A

A parameter of a method other than the object on which the method is invoked.

29
Q

Private interface

A

The implementation details that are hidden (encapsulated). It consists of instance variables, and the bodies of constructors and methods.

30
Q

If instance variables are private, then how can programs read of set their values?

A

With “getters” and “setters”. These are important methods that can be provided in your programs to maintain encapsulation. For example, a “getter” method, such as getColor(), gives a program the ability to retrieve an object’s color without being able to modify the value as would be possible if the variable was public.

31
Q

What is the difference between an instance field and a static field?

A

Each instance of a class has its own copy of the class’s instance fields. In contrast, a static field does not belong to any single instance of the class. There is only one copy of a static field in memory, regardless of the number of instances of the class, and it is shared by all of the instances.

32
Q

What action is possible with a static method that isn’t possible with an instance method?

A

It can be used directly from the class without creating an instance of the class.

33
Q

Do static methods have any limitations?

A

Yes. They cannot refer to non-static members of the class. This means that any method called from a static method must also be static. It also means that if the method uses any of the class’s fields, they must also be static.

34
Q

What is an object’s “state”?

A

An object’s “state” is simply the data that is stored in it’s fields at any given moment.

35
Q

What is a toString() method and why are they so popular?

A

Creating a string that represents that the state of an object is such a common task that many programmers equip their classes with methods to do this which are named toString() methods. If you write a toString() method for a class, Java will automatically call it when the object is passed as argument to print() or println(). Java also implicitly calls an object’s toString() method any time you concatenate an object of the class with a string.

Note that every class automatically has a toString() method that returns a string containing the object’s class name, followed by the @ symbol, followed by an integer that is usually based on the object’s memory address. This is because every class directly or indirectly extends the Object class, so every class inherits its members, which includes the toString() method. This method is called if you haven’t provided your own toString() method.

36
Q

What happens if a programmer does not provide a toString() method for a class?

A

Every class automatically has a toString() method that returns a string containing the object’s class name, followed by the @ symbol, followed by an integer that is usually based on the object’s memory address.

37
Q

What methods does every class in Java automatically have?

A
  1. toString() 2. Equals()
38
Q

Why can’t you use the == operator to compare two objects?

A

Because, like with Strings, that will compare the memory address of both objects and therefore always return false. If you want to compare the contents of two objects (i.e. that their field values are the same) then you need to create you own “equals” method.

39
Q

What is a reference copy?

A

An assignment operation that only copies the address of an object, not the actual object itself. For example: Stock company1 = new Stock(“ABC”, 9.5); Stock company2 = company1; To copy the object itself, you must create a new object and then set the new object’s fields to the same values of the object being copied.

40
Q

If you can’t use the == operator to copy an object, then how do you do it?

A

You can create a copy method or use a copy constructor, which is a constructor that accepts an object of the same class as an argument. For example:

public Stock(Stock object2)

{

symbol = object2.symbol;

sharePrice = object2.sharePrice;

}

41
Q

What is aggregation?

A

An instance of a class being used as a field in another class. In other words, it’s when an object is made up of other objects. For example, a house class may contain door objects, window objects, etc.

42
Q

What is object aggregation?

A

Making an instance of one class a field in another class

43
Q

What is an aggregate class?

A

A class that is made of constituent objects

44
Q

What is a deep copy?

A

A copy of an aggregate object that includes copies of the objects it references. This is important to prevent security holes. For example:

public Course(String name, Instructor instr, Textbook text)

{

courseName = name;

instructor = new Instructor(instr);

textBook = new TextBook(text);
}
45
Q

What is a shallow copy?

A

A copy of an aggregate object that only makes a reference copy of the objects it references (don’t do this!!!!). For example: public Course(String name, Instructor instr, Textbook text)

{

courseName = name;

instructor = instr;

textBook = text;

}

46
Q

Immutable

A

Unable to be changed (i.e. String objects)

47
Q

What is the “this” reference variable?

A

A reference variable that an object can use to refer to itself. It is available to all non-static methods. One of the most common uses of the “this” reference variable is to overcome shadowing. For example: public Stock(String symbol, double sharePrice) { this.symbol = symbol; this.sharePrice = sharePrice; }

48
Q

What is “shadowing”?

A

When a method’s parameter has the same name as a field in the same class.

49
Q

What is an enumerated data type?

A

A custom data type that holds a specific set of legal values.

50
Q

What is a “fully qualified name”?

A

Notation used for enumerated data types. For example, for: enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }

The fully qualified name for WEDNESDAY is Day.WEDNESDAY and under most circumstances, you must use the fully qualified name of an enum constant.

51
Q

When should a method be made static?

A

If you do not need to create an instance of the class to use it. For example, a convertMpgToKpl method in a Car class, should be made static because an instance of a car isn’t necessary to use that method. That is, the formula to convert miles per gallon to kilometers per litre exists independent of a car.

52
Q

Aggregation

A

Aggregation is a special form of Association which represents the Has-A relationship. It is an uni-directional association where both the entries can survive individually.

53
Q

Composition

A

Composition is a more restrictive form of aggregation that makes two entities highly dependent on each other. It represents the part-of relationship where the composed object cannot exist without the other entity.

54
Q

Dependency

A

The uses relationship between classes in which one class needs services provided by another class.

55
Q

Collaborator

A

A class on which another class depends.