Classes & Objects Flashcards
Instance of a class
An object whose type is that class.
Coupling
The measure of the degree of interdependence between the various classes in your project. A good project will have low coupling.
Cohesion
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.
DRY
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.
CRC card
An index card representing a class that lists its responsibilities and collaborating classes.
Principle of least surprise
It is important to follow existing conventions and practices to improve the readability of your code and make it easiest to understand.
Abstraction
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.
Unified Modeling Language (UML)
A notation for specifying, visualizing, constructing, and documenting the artifacts of software systems.
Immutable class
A class without a mutator method.
Side effect
An effect of a method other than returning a value.
Static variable
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++;
}
Static method
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.
Package
A collection of related classes. The import statement is used to access one or more classes in a package.
Instance variables
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
Access specifier / modifier
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.
Return value
The value returned by a method through a return statement.
Encapsulation
The hiding of implementation details.
Association
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
Body
All statements of a method or block.
Constructor / Construction argument
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:
- The name of the constructor is always the same as the Class
- There can be more than one constructor with the same name
- 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!
No-argument constructor
A constructor that takes no arguments.
Public interface
The content of a class declaration that any programmer can view and use. It consists of the public constructors and methods of the class.