Mason Vail. A data type definition – “blueprint for objects” Includes properties and/or...

15
CS121 Review Mason Vail

Transcript of Mason Vail. A data type definition – “blueprint for objects” Includes properties and/or...

Page 1: Mason Vail.  A data type definition – “blueprint for objects”  Includes properties and/or methods ◦ “instance” data / methods – specific to one object.

CS121 ReviewMason Vail

Page 2: Mason Vail.  A data type definition – “blueprint for objects”  Includes properties and/or methods ◦ “instance” data / methods – specific to one object.

A data type definition – “blueprint for objects”

Includes properties and/or methods

◦ “instance” data / methods – specific to one object (instance) of the class

◦ “static” or “class” data / methods – belongs to the class itself, shared by all objects of the type

Class

Page 3: Mason Vail.  A data type definition – “blueprint for objects”  Includes properties and/or methods ◦ “instance” data / methods – specific to one object.

One instance of a data type

Looks and acts like other objects of the same type, but has its own identity and state independent of other objects

Object

Page 4: Mason Vail.  A data type definition – “blueprint for objects”  Includes properties and/or methods ◦ “instance” data / methods – specific to one object.

One of the central pillars of object-oriented design

Idea that each object is in control of its own state (instance data) and outsiders are required to interact with the object through its limited public interface (set of methods)

Encapsulation

Page 5: Mason Vail.  A data type definition – “blueprint for objects”  Includes properties and/or methods ◦ “instance” data / methods – specific to one object.

Visibility regulates the exposure of data and methods◦ “public” – accessible to any outside entity with a

reference to the object◦ “protected” – default visibility – accessible to child

objects or any other entity defined in the same package

◦ “private” – accessible only within the class/object

All data / methods should be private until a good reason is given to relax visibility

Encapsulation - Visibility

Page 6: Mason Vail.  A data type definition – “blueprint for objects”  Includes properties and/or methods ◦ “instance” data / methods – specific to one object.

A named set of statements within a class that carry out a specific task

Public methods make up the public interface of an object

Protected/private methods generally serve as supporting utility methods for carrying out intermediate tasks for the object

Method

Page 7: Mason Vail.  A data type definition – “blueprint for objects”  Includes properties and/or methods ◦ “instance” data / methods – specific to one object.

The class containing the main() method, where a program begins

The class invoked by name when launching the Java Runtime Environment (JRE)

Driver Class

Page 8: Mason Vail.  A data type definition – “blueprint for objects”  Includes properties and/or methods ◦ “instance” data / methods – specific to one object.

The most basic linear data structure for storing multiple values of the same type

In some languages, they are a primitive data type, but in Java arrays are objects, though they have special syntax that makes them look a little different than other objects

Array

Page 9: Mason Vail.  A data type definition – “blueprint for objects”  Includes properties and/or methods ◦ “instance” data / methods – specific to one object.

As objects, arrays must be instantiated with a constructor call before they can be used

◦ int[] a = new int[10];//creates an array with room for 10 ints

The size of an array, once instantiated, is immutable

Array

Page 10: Mason Vail.  A data type definition – “blueprint for objects”  Includes properties and/or methods ◦ “instance” data / methods – specific to one object.

Arrays can be multidimensional – each dimension can be of a different size

2D arrays (“tables”) are common

The first dimension of a 2D array is commonly considered to be the row reference

The second dimension of a 2D array is commonly considered to be the column

Array

Page 11: Mason Vail.  A data type definition – “blueprint for objects”  Includes properties and/or methods ◦ “instance” data / methods – specific to one object.

Any multidimensional array can be thought of as an “array of arrays” until the last dimension is accessed and an individual value of the array type is reached

int[][] someArray = new int[5][10];

◦ someArray is an array of int[]s of length 5

◦ someArray[0] is in int[] of length 10

Array

Page 12: Mason Vail.  A data type definition – “blueprint for objects”  Includes properties and/or methods ◦ “instance” data / methods – specific to one object.

A primitive variable is a named memory location containing a value of the primitive type

An object reference variable contains the address of an object somewhere else in memory

Primitive Variablesvs Object References

Page 13: Mason Vail.  A data type definition – “blueprint for objects”  Includes properties and/or methods ◦ “instance” data / methods – specific to one object.

Making a copy of a primitive variable duplicates the value, after which the two variables have no effect on the other

Making a copy of an object reference creates an alias – a duplicate reference to the same object – so changes can be made to the object via any of its aliased references

Copying an object requires instantiating a new, independent object and explicitly duplicating the state of the original object in the new object

Primitive Variablesvs Object References

Page 14: Mason Vail.  A data type definition – “blueprint for objects”  Includes properties and/or methods ◦ “instance” data / methods – specific to one object.

A collection of bodiless, abstract method signatures defined in a .java file

May be implemented by one or more classes

Not a class – may not be instantiated

Interface

Page 15: Mason Vail.  A data type definition – “blueprint for objects”  Includes properties and/or methods ◦ “instance” data / methods – specific to one object.

Accessor (“getter”) methods – methods with the special purpose of returning object information without violating encapsulation◦ Returned values are typically already known – the

method only needs to return an independent copy of instance data

Mutator (“setter”) methods – methods with the special purpose of updating object state without violating encapsulation◦ Changing data takes the form of a request, rather than

a demand – the object has the opportunity to validate input and enforce its own rules about updating its state

Accessor and Mutator Methods