Object-oriented concepts and definitions
Below, we present concepts and definitions for OO in Java, with references to the course text.
- abstract class
- A class that is declared with modifier abstract. The main feature is that the class cannot be substantiated. Gries/Gries pp. 163–164. Module 2, part 5, item 1.
- abstract method
- A method declared with modifier abstract in an abstract class. Its body is replaced by a ";". Its main feature is that it must be overridden in any non-abstract subclass. Gries/Gries p. 163–164. Module 2, part 5, item 1.
- access modifier
- Java has four levels of access to components. In order of accessibility, they are: public, protected, package, and private. A component declared with public is accessible everywhere. A component in a class C declared with protected is accessible in C, subclasses of C, and classes in the same package as C. A component declared with no access modifier is a package component —it is accessible in all classes in the same package. A component declared with private is accessible only within the class in which it is declared. Gries/Gries pp. 155–156. Module 1 part 6.
- actual parameter
- A term used for argument (of a method call). The term should never be used because it is confusing, especially because the adjective "actual" tends to be omitted and then it is just parameter, which is even more confusing.
- apparent class
- The apparent class of a variable is the class-type with which it is declared. It is a syntactic property. It determines which components can be referenced. Gries/Gries p. 150. Module 2 part 2, item 8.
- applet
- An applet is a Java program that contains a subclass of java.awt.Applet or javax.swing.JApplet. It can appear on web pages. Gries/Gries pp. 438–444.
- application
- A Java application is a program that has a class that contains a
static procedure defined like this:
public static void main(String[] pars) {...}
An application can be executed without using an IDE, as a stand-alone program. The text explains how to create this stand-alone program, in what is called a jar file. Gries/Gries pp. 435–437.
- argument
- An expression that occurs within the parentheses of a method call.
The following call has two arguments: x+y and y+w:
min(x+y, y+w)
The number of arguments must equal the number of parameters of the method that is being called, and the type of each argument must be the same as or narrower than the type of the corresponding parameter. Gries/Gries pp. 59, 62.
- assertion
- A true-false statement about the variables in a program that is placed as a comment at a particular place in the program to indicate that the true-false statement is true at that place. See also precondition and postcondition. Gries/Gries pp. 75, 506–507.
- boolean
- A primitive type whose values are true and false. Gries/Gries pp. 226–229.
- Boolean, class
- A wrapper class for type boolean. Gries/Gries pp. 174–175.
- bottom-up rule
- The rule that indicates that, when looking for a variable or method in an object, start from the bottom and look upward. This rule ensures that an overriding method will be called (if present) rather than an inherited one. Gries/Gries, pp. 143–144.
- byte
- A primitive type whose values are integers in the range –2^8 .. 2^8 – 1. Gries/Gries pp. 218–219.
- Byte, class
- A wrapper class for type byte. Gries/Gries pp. 174–175.
- cast
- A cast is an operation that converts a value from one type to another. For example, the cast (double) (5+6) converts the value of the expression, which is 11, to double format. A widening cast may be done implicitly; a narrowing cast must be done explicitly, because it may lose information or may be illegal. See narrower type.
- char
- A primitive type whose values are single characters, like 'h', '$', and '\n'. Gries/Gries pp. 224–225.
- Character, class
- A wrapper class for type char. Gries/Gries pp. 174–175.
- class
- A class definition defines the format of objects (or
instances or manila folders) of the class —it defines the methods and fields that
go in each object of the class. In our analogy,
a class is a file-drawer that contains all the manila folders of
the class that have been created. For the basic syntax of a class
definition, see Gries/Gries p. 107 and (for a subclass definition)
p. 142.
The class definition also defines the static variables (class variables) and static methods (class methods), which also are placed in the file drawer.
A class can be used as a type. If C is (the name of) a class, then C v; declares a variable v that contains either null or the name (on the tab) of an object of class C. - class, apparent
- See apparent class.
- class as a type
- A class name can be used as a type. Its values are the names of objects of that class. For example, the declaration JFrame j indicates that variable j can contain values that are the names of objects of class JFrame.
- class invariant
- The collection of meanings and constraints on the fields of a class that describe the valid states of all instances of the class before and after each method is called. The class invariant should appear as comments on the declarations of the fields. A constructor should truthify the class invariant. Each method body can assume that the class invariant is true and should terminate with the class invariant true. Gries/Gries pp. 146–147.
- class method
- See static method.
- class, real
- See real class.
- class type
- A type that is the name of a class. Its values are the names of objects of that class. Gries/Gries, pp. 115–116.
- class variable
- Same as a static variable.
- component
- A component of a class is a variable or method that is declared in the class or is inherited from the superclass. Gries/Gries, p. 39, 82.
- Conditional expression
- An expression of the form <boolean-expression> ? <expression-1> : <expression-2>. To evaluate it, evaluate the <boolean-expression>; if it is true, use the value of <expression-1> as the value of the conditional expression; otherwise, use the value of <expression-2> as the value of the conditional expression. Here is a discussion of the conditional expression.
- constructor
- A method that is declared with the basic form:
- <access modifier> <class-name> ( <parameters> )
{ ... }
The purpose of a constructor is to initialize some or all of the fields of a new instance of class <class-name> when the instance is created. See also constructor call. Gries/Gries, pp. 110–112, 147–148. - constructor call
- A call of a constructor. There are three
ways to call a constructor —the last two ways may appear only as
the first statement of a constructor body:
1. In a new-expression.
2. As a call of another constructor in the same class: this(<arguments>);.
3. As a call of a constructor of the superclass: super(<arguments>);.
Gries/Gries, pp. 112, 116, 147. - constructor call, default
- If (and only if) the body of a constructor does not contain a constructor call as its first statement, Java automatically inserts this one: super();. Gries/Gries, pp. 148.
- constructor, default
- If (and only if) a class definition does not contain a constructor, the following
one is automatically inserted in the class:
public <class-name>() {super();}
Gries/Gries, p. 111. This page has a mistake, in that the default constructor is shown with an empty body, whereas it contains the call super();.
- default values
- Fields (both static and non-static) have default values (if they are not initialized), as follows: numeric-type fields: 0; boolean fields: false; char fields: '\u0000'; and class-type fields: null. Local variables do not have initial default values and are expected to be assigned before referenced. Gries/Gries, p. 107.
- deprecate
- To deprecate means to lessen in value. Some classes and methods within classes in the Java API package have been deprecated because they have been superceded by newer ones that do a better job. You can still use the deprecated entities, but it is better if you don't. Gries/Gries, p. 495.
- double
- A primitive type whose values are scientific numbers, e.g. 3.46E–4. Each value occupies 64 bits. Gries/Gries pp. 221–224.
- Double, class
- A wrapper class for type double. Gries/Gries pp. 174–175.
- Escape character \
- Character \, the escape character, is used to write characters that cannot appear directly in a String. Here are examples; there are others:
\n —new-line character
\\ —backslash character
\" —double quote character
\' —single quote character (you can use ' in a String, but as a character, write '\''
- extends clause
- A clause, extends C, put on a class definition to indicate that the defined class is a subclass of class C and that C is a superclass of the defined class. The subclass inherits all components defined in and inherited by the superclass. Gries/Gries, pp. 142-143.
- field
- A variable belonging to a class (static field) or object (non-static field). Gries/Gries p. 107.
- file drawer
- In order to provide an easy-to-understand introduction to classes and objects, we view a class as a file drawer, which holds (1) the objects, i.e. manila folders, and (2) the static variables and methods. The Java class definition defines the format of the content manila folders. See class.
- float
- A primitive type whose values are scientific numbers, e.g. 3.46E–4. Each value occupies 32 bits. Gries/Gries pp. 224.
- Float, class
- A wrapper class for type float. Gries/Gries pp. 174–175.
- folder, manila
- Same as object or instance.
- formal parameter
- We use the term parameter (of a method) instead of formal parameter and argument (of a method call) instead of actual parameter. The terms formal parameter and actual parameter should never be used because they are confusing, especially because the adjectives tend to be forgotten and then there is no difference in the terms.
- frame for a call
- When a method is called, a frame is created for the call. This frame
contains
(1) the name of the method,
(2) a program counter, which indicates which statement to execute next,
(3) a scope box, which contains the name of the class or object in which the method resides,
(4) the parameters of the method, and
(5) the local variables of the method.
Gries/Gries, pp. 93–94. - function
- A method that is declared with the basic form:
- <access modifier> <return-type> <function-name> ( <parameters> ) { ... }
- Execution of the body of the function must terminate by with executing a return statement, giving an expression whose value is to be returned. A call on a function is an expression (and thus yields a value). Gries/Gries, pp. 55–58.
- function call
- A function call is an expression, so it can be placed wherever an
expression (of the appropriate type) can be placed. Its form is one
of:
1. for a static function: <class-name> . <function-name> ( <arguments> )
2. for a non-static function: <expression> . <function-name> ( <arguments> )
where the <expression> evaluates to the name of an object that contains the function to be called. Both "<class-name>." and "<expression>." can be omitted if the call appears in a place in which the function can be called without them. Gries/Gries, p. 62. - getter method
- A non-static function whose purpose is to retrieve a value from the object in which the method appears —the value is usually in a field of the object. If the field to be retrieved is named xab, by convention, the name of the getter method is getXab. Gries/Gries, p. 35, 109.
- header, of a method
- The part of a declaration of a method that gives the access modifiers, prefixes like static, the return type or void, the method name, and the parameter declarations delimited by parentheses. Thus, it is the complete method declaration except for the method body. Gries/Gries, p. 57.
- implements clause
- The implements clause, implements <interface-name>, on a class indicates that the class overrides all abstract methods declared in the interface. Gries/Gries, pp. 336–337.
- import statement
- The import statement has one of the forms:
import <path>.<class-name>; // import a particular class
import <path>.*; // import all classes in the package
where the <path> is a path of the API package (e.g. javax.swing) or a path to a directory on your hard drive that represents a package of classes. It is placed as the first statement of a file that contains a class definition in order to indicate that the class may refer to the classes in the directory given by the <path>. Gries/Gries, pp. 31–32, 330–331. - inheritance
- A subclass inherits all the instance fields and methods available in its superclass. This means that each instance of the subclass contains not only the instance fields and methods defined in it but also the instance fields and methods defined in an inherited by the superclass. Gries/Gries, pp. 143–144.
- inside-out rule
- A rule, used in most programming languages, that indicates how to determine what a reference to a variable or a method means (which declaration it refers to). Gries/Gries, pp. 83, 109–110.
- instance
- An instance of a class is an object or manila folder of a class; instance, object, and manila folder are synonyms. Gries/Gries, p. 31.
- instance method
- A method that is defined without modifier static in a class. A copy of the method resides in each instance of the class. Gries/Gries, p. 3, 107.
- instanceof
- Operation <object-expression> instanceof <class-name> yields true iff the object whose name is given by <object-expression> is an instance of class <class-name> or of a subclass of <class-name>. Gries/Gries, p. 150, 152–153.
- instance variable
- A variable that is defined without modifier static in a class. Also called a field. A copy of the variable resides in each instance of the class. Gries/Gries, p. 31, 45–46.
- instantiate
- To create an instance of. So, evaluation of a new-expression new C(...) instantiates class C, producing a new instance of it.
- int
- A primitive type whose values are integers in the range –2^31 .. 2^31 – 1. Gries/Gries pp. 216–218.
- Integer, class
- A wrapper class for type int. Gries/Gries pp. 172–174.
- integral types
- The Java primitive types whose values are integers: byte, short, int, long, and char. Gries/Gries, pp. 215–221.
- interface
- An interface provides a way to ensure syntactically (at compiletime) that a class implements certain methods. The interface defines the methods abstractly (without bodies), and the class definition then includes an implements clause to indicate that it will define the methods. Gries/Gries, pp. 335–47.
- Literal
- A Java denotation of a value. Here are examples of literals for some of the primitive types and type String:
Type int: 354
Type float: 3.56f
Type long: 3.2E-6
Type boolean: true
Type char: 'c'
Type String: "I want peace. Throw away the I (ego) and want (desires), and I have it."
- local variable
- A variable that is declared within the body of a method. Gries/Gries, pp. 76–78.
- long
- A primitive type whose values are integers in the range –2^63 .. 2^63 – 1. Gries/Gries pp. 219–220.
- Long, class
- A wrapper class for type long. Gries/Gries pp. 174–175.
- manila folder
- Same as object or instance.
- Math, class
- Class Math, in package java.lang, contains a bunch of mathematical functions —e.g. sin(...), tan(...), ceil(...)— as well as the constants PI and E. Gries/Gries, pp. 23–25.
- method
- A set of instructions to be carried out. A method is analogous to a recipe in a cookbook. Java has three kinds of method: function, procedure, and constructor. Gries/Gries, pp. 55–99.
- method body
- A method consists of a method header followed by the method body, which has the form: { <sequence of statements/declarations> }. When the method is called, its body is executed. Gries/Gries, pp. 64–71.
- method call
- An expression or statement that ends up executing the method body.
See function call, procedure
call, and
constructor call. It is important
to understand how a method call is executed. Its execution is performed in four steps:
1. Draw a frame for the call;
2. Assign argument values to the parameters;
3. Execute the method body;
4. Erase the frame for the call (and, if a function, return the function value).
Gries/Gries, pp. 59–62, 162–163. - method header
- See header, of a method.
- method name
- The name of the method, defined in the method header.
- method main
- A Java program becomes an application when a static procedure main is defined that has one parameter, of type String[]. The application can be started without having to have an IDE around. To start execution, this method main is called. Gries/Gries, pp. 435–437.
- method signature
- The method name together with the types of its parameters. For example, one method in class Math has signature min(int, int). Gries/Gries, pp. 57.
- method specification
- The specification of a method defines what the method does. It is used to understand how to write calls on the method. It must be clear, precise, and thorough, and it should mention all parameters, saying what they are used for. It is a contract between the programmer who wrote the method and users of the method. It may be given in terms of a precondition and postcondition. The reference gives details on specs for the different kinds of methods: Gries/Gries, pp. 376–379.
- narrower type
- Below, each line gives a series of types, beginning with the narrowest
type and proceeding to the widest type:
byte --> short --> int --> long --> float --> double
char --> int --> long --> float --> double
subclass --> superclass
A cast from a narrower type to a wider type will be done automatically, but a cast from a wider type to a narrower type must be explicitly asked for in a Java program. Gries/Gries, pp. 225–226, 151–152. - new-expression
- The new expression has the form new <class-name> (
<arguments> ). It is evaluated in three steps:
1. Create an instance of class <class-name>;
2. Execute the constructor call <class-name>(<arguments>);
3. Yield as value the name of the newly created instance.
Gries/Gries, pp. 116–117. - non-static variable
- See instance.
- non-static method
- Same as instance method.
- null
- A value that means "the absence of an object name". If a reference like null.b or a method call like null.m(...) is attempted at runtime, a "null pointer exception" happens. Gries/Gries, p. 38.
- object
- An instance of a class, also called a manila folder. Each object of a class contains the non-static fields and methods defined in and inherited by the class. See class.
- Object, class
- The superest class of them all: any class that does not explicitly extend another class automatically extends class Object. Gries/Gries, p. 154.
- object type
- Same as class type.
- overloaded
- A method name is overloaded if there are two methods with the same name but different signatures. For example, in class Math, there are functions with signatures min(int, int) and min(double, double). Gries/Gries, p. 23.
- override
- A method that is inherited from the superclass can be overridden by redeclaring it in the subclass. The bottom-up rule ensures that the overriding method will be called, rather than the inherited one.
- package
- A package consists of the classes that are contained in a specific directory on your hard drive (or the API packages, like javax.swing and java.lang). The package statement itself is beyond the scope of this course, although it is discussed in the text. Gries/Gries, pp. 329–334.
- parameter
- A variable that is declared within the parentheses of the header of a method. Gries/Gries, pp. 56–58.
- parameter declaration
- A parameter declaration occurs within the parentheses of the header of a method. It has the form <type> <variable-name>. Gries/Gries, pp. 57-58.
- postcondition
- An assertion that indicates what is to be true at the end of execution of a method body —or, more generally, of any sequence of statements. Gries/Gries, pp. 93, 506.
- Precedence of operators
- Standard mathematics gives precedence to multiplication * over addition +, so that the expression 2 + 3 * 5 is evaluated as if it were parenthesized like this: 2 + (3 * 5). The precedences used in Java for all operators are given on this page.
- precondition
- An assertion that indicates what is to be true at the beginning of execution of a method body —or, more generally, of any sequence of statements. Gries/Gries, pp. 93, 506.
- primitive type
- A type that is fully integrated into Java, i.e. one that is not defined by a class definition. The primitive types of Java are: char, byte, short, int, long, float, double, and boolean. Gries/Gries pp. 215–216.
- private
- An access modifier. A variable or method that is declared with this modifier can be referenced only within the class in which the declaration occurs. Gries/Gries, pp. 155–156.
- procedure
- A method that is declared with the basic form:
- <access modifier> void <function-name> ( <parameters> ) { ... }
- A call of a procedure is a statement. Gries/Gries, pp. 55–58.
- procedure call
- A procedure call is a statement. Its form is one
of:
1. for a static procedure: <class-name> . <function-name> ( <arguments> ) ;
2. for a non-static procedure: <expression> . <function-name> ( <arguments> ) ;
where the <expression> evaluates to the name of an object that contains the procedure to be called. Both "<class-name>." and "<expression>." can be omitted if the call appears in a place in which the procedure can be called without them. Gries/Gries, p. 59–60. - promoting a value
- Same as casting it to a wider type, done automatically in Java when necessary. See narrower type. Gries/Gries, p. 220.
- public
- An access modifier: the variable or method that is declared with this modifier can be referenced everywhere. Gries/Gries, pp. 155–156.
- real class
- The real class of a variable is the class of the object whose name is in the variable. The real class can change at runtime, when a new object (i.e. object name) is stored in the variable. Gries/Gries p. 150.
- return statement
- In a procedure, a return statement has the form
- return;
- In a function, a return statement has the form
- return <expression>;
- Execution of the return statement terminates execution of the method body in which it occurs and, in the case of a function, returns the value of the <expression> as the value of the function call. Gries/Gries p. 150.
- scope
- The scope of a variable is the set of statements in which it can be referenced. For a parameter, it is the body of the method in which the parameter is declared. For a local variable, it is from its declaration to the end of the block in which the declaration occurs. Gries/Gries, pp. 65, 76–78.
- scope box
- A box within the frame for a method call that contains the name of the class (for a static method) or object (for an instance method) in which the method resides. Gries/Gries, pp. 93–94.
- setter method
- A non-static procedure whose purpose is to store a value in a field of the object in which the method resides. If the field is named xab, by convention, the name of the setter procedure is setXab. Gries/Gries, p. 35, 109.
- short
- A primitive type whose values are integers in the range –2^15 .. 2^15 – 1. Gries/Gries pp. 218–219.
- Short, class
- A wrapper class for type short. Gries/Gries pp. 174–175.
- signature
- See method signature.
- specification, of a class
- A description of what a class is for. Generally speaking, it indicates what an instance of the class represents.
- specification, of a method
- See method specification.
- static
- See static method and static variable.
- static field
- Same as static variable.
- static method
- A method that is declared with attribute static. There is only one copy of the method, and it resides in the class itself (in the class file drawer). Gries/Gries, p. 124.
- static variable
- A variable declared in a class definition with attribute static. There is only one copy of the variable. It is created when execution of the program starts, and it resides in the class itself (in the class file drawer). Gries/Gries, pp. 122–124.
- String, class
- The values of class String are objects that contain sequences of characters. Gries/Gries, pp. 175–181.
- subclass
- Class SC is a subclass of class C, and C is the superclass of SC, if the definition of class SC has an extends clause extends C. This means that every instance of class SC inherits all the components of class C. Gries/Gries, pp. 141–162.
- super
- A keyword that is used in two ways. First, to call an inherited function instead of an overriding one. Second, to call a constructor of a superclass. Gries/Gries, pp. 144–146, 147–148.
- superclass
- See subclass.
- this
- A keyword that is used in two ways. First, as a name for the object in which it appears. Second, to call another constructor of a class. Gries/Gries, pp. 109–110, 112.
- toString function
- A Java convention is to write an instance function toString() that yields a description of the object in which it resides. Gries/Gries, pp. 112–113.
- type
- A set of values together with operations on them. Gries/Gries, pp. 17.
- variable
- A name with an associated value. Sometimes viewed as a named box with the value in the box. In Java, a variable has a type, which defines the set of values that can go in its box. In Java, there are four kinds of variable: parameter, local variable, instance variable (or field), and class variable (or static variable). Gries/Gries, pp. 26–28.
- Variable declaration
- The basic form of a variable declaration is <type> <variable-name>. Depending on the kind of variable being declared, there may be modifiers. See parameter, local variable, instance variable, and static variable. Gries/Gries, pp. 28.
- wider type
- See narrower type.
- Wrapper class
- Each of the primitive types of Java has an associated wrapper class. For example, the wrapper class for type int is Integer. Each instance of the wrapper class contains, or wraps, one value of the primitive type, thus allowing a primitive type value to be viewed as an object. In addition, the wrapper class provides functions for manipulating values of the primitive type and may also include a few constants. Gries/Gries, pp. 172–175.