150 likes | 262 Views
CS121 Review. Mason Vail. Class. 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.
E N D
CS121 Review Mason Vail
Class • 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
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
Encapsulation • 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 - Visibility • 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
Method • 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
Driver Class • The class containing the main() method, where a program begins • The class invoked by name when launching the Java Runtime Environment (JRE)
Array • 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 • 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 • 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 • 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
Primitive Variablesvs Object References • 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 • 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
Interface • 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
Accessor and Mutator Methods • 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