Computer Science 1: Class Relationships

Lesson 16

The classes in a piece of software relate to each other in specific ways. 3 of these ways are dependency, aggregation, and inheritance.

Dependency

The dependency relationship is a “uses” relationship; where one class uses the other. This often happens when once class uses the methods of another.In general it’s best to have less dependancy- that way errors have less of a chance to move down the chain like dominos. An object can also depend on itself, or it’s objects can be dependant on one another.

Aggregation

Aggregation is a has-a relationship. An aggregate object id an object that contains references to another object as instance data. The more complex an object, the more likely it will need to be broken down into aggregate objects- smaller, more workable pieces.

“this”

“this” is a reserved word in java that allows an object to reference itself.This is often used to distinguish the parameters of the constructor from the instance variables of the object.

int variable1, variable2;

public Sum(variable1, variable2) {

this.variable1 = variable1;
this.variable2 = variable2;

}

Method Design

Every method has an algorithm that determines how it will work. An algorithm is a step-by-step process for solving a problem- like directions or a recipe. Algorithms often use psudocode- a mix of english and simple code statements to plan and show how the code will operate. When a method is too complex the process of method decomposition can be used to break the method into smaller, more manageable private methods to handle certain tasks.

Published by Bethany Hull

Currently working on a BS in Computer Science from Boise State. BA in Dance from BYU-I. Yoga teacher. Choreographer. Backpacker. Foodie.

Leave a comment

Design a site like this with WordPress.com
Get started