8
Composition • When one class contains an instance variable whose type is another class, this is called composition . • Instead of inheritance, which is based on an IS-A relationship, composition is based on a HAS-A relationship. • For example, a car is a vehicle, and has an engine: public class Car extends Vehicle { private Engine x = new Engine(); }

Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based

Embed Size (px)

Citation preview

Page 1: Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based

Composition• When one class contains an instance variable

whose type is another class, this is called composition.

• Instead of inheritance, which is based on an IS-A relationship, composition is based on a HAS-A relationship.

• For example, a car is a vehicle, and has an engine:

public class Car extends Vehicle

{

private Engine x = new Engine();

}

Page 2: Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based

Search algorithms

• We have learned the 4 most common algorithms for sorting an array.

• There are 2 main algorithms for searching an array – looking for an item in an array, and if it is found, determining the index at which it was found.

1) Binary Search2) Sequential Search

Page 3: Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based

Binary Search

• Note: you must begin with a sorted array.• The Binary Search algorithm uses these steps:1) Find the middle index2) Compare the element located there to the thing you

are searching for (the “target”)3) If the target is larger, then ignore the lower half of the

array, and find the middle index of the larger half– If smaller, ignore the larger half, find middle index of smaller

half.

4) Repeat until you find (or do not find) the targetDemo: BinarySearchDemo

Page 4: Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based

Sequential Search

• The Sequential Search algorithm is simpler, but less efficient than Binary Search.

• Simply look at each element in the array, starting at the first element, until you find the target.

Page 5: Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based

null• When you create an Object, such as a String, you must

initialize it. If you do not, then its value is null.

• Then, if you attempt to display it, or call a method on that Object, such as finding the length of a String, you will get an error – either a compiler error, or a runtime error that is called NullPointerException.

• Sometimes, when you do not yet know the value of an Object, you deliberately set it to null, in order to avoid compiler errors. This is called a null reference. You can check for null references using if statements.

• demo: NullDemo

Page 6: Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based

Wrapper Classes

• ints, doubles, and chars are known as primitive types, or built-in types.

• There are no methods associated with these types of variables.• Strings, on the other hand, are not primitive: there is a class

called String that has methods, such as .length( ), .charAt( ), etc.

• So, when you create a String variable, you are actually creating an object of the String class.

• Using primitive types is more efficient, in terms of code and computer memory. But what if you want to use certain methods on an int, a double, or a char?

Page 7: Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based

Wrapper Classes

• A wrapper class allows you to “wrap” or “box” a primitive type into an object, so that you can use methods associated with that object.

• Example:Integer age = new Integer(34);

• The value of age is 34, but you can do more with this than you could with a normal, primitive int.

Page 8: Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based

What is the point of a Wrapper Class?

• The 3 most common uses of a wrapper class are:1) To use in an ArrayList (remember, ArrayLists hold

Objects, not primitive types!) **See ArrayListDemo**2) When you want to use a null reference (to deliberately

set a variable value to null. When would you do this? One example: When you can’t be 100% sure that a method will return a valid integer.)

3) When you want to use an Integer, Double, or Char polymorphically

example: Object num = new Integer(23);