Concepts+Techniques+and+Models+of+Computer+Programming_Part21

Concepts+Techniques+and+Models+of+Computer+Programming_Part21

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 558 Object-Oriented Programming declared as final , which means that the variable can be assigned exactly once. Final variables must be assigned before they are used. Java introduces its own terminology for some concepts. Classes contain fields (attributes, in our terminology), methods, other classes, or interfaces, which are known collectively as class members. Variables are either fields, local variables (declared in code blocks local to methods), or method parameters. Variables are declared by giving their type, identifier, and an optional set of modifiers (e.g., final ). The self concept is called this . Interfaces Java has an elegant solution to the problems of multiple inheritance (Sections 7.4.4 and 7.4.5). Java introduces the concept of interface , which syntactically looks like a class with only method declarations. An interface has no implementation. A class can implement an interface, which simply means that it defines all the meth- ods in the interface. Java supports single inheritance for classes, thus avoiding the problems of multiple inheritance. But, to preserve the advantages of multiple inheritance, Java supports multiple inheritance for interfaces. Java supports higher-order programming in a trivial way by means of the encoding given in Section 7.5.2. In addition to this, Java has more direct support for higher-order programming through inner classes. An inner class is a class definition that is nested inside another class or inside a code block (such as a method body). An instance of an inner class can be passed outside of the method body or code block. An inner class can have external references, but there is a restriction if it is nested in a code block: in that case it cannot reference non-final variables. We could say that an instance of an inner class is almost a procedure value. The restriction likely exists because the language designers wanted non- final variables in code blocks to be implementable on a stack, which would be popped when exiting the method. Without the restriction, this might create dangling references. 7.7.2 Introduction to Java programming We give a brief introduction to programming in Java. We explain how to write a simple program, how to define classes, how to use inheritance, and how to write concurrent programs with locks and monitors. We situate the Java style with respect to the computation models of this book. This section only scratches the surface of what is possible in Java. For more information, we refer the reader to one of the many good books on Java program- ming. We especially recommend [10] (on the language) and [111] (on concurrent programming). Copyright c 2001-3 by P. Van Roy and S. Haridi. All rights reserved. 7.7 The Java language (sequential part) 559 A simple program We would like to calculate the factorial function. In Java, functions are defined as methods that return a result: class Factorial { public long fact(long n) { long f=1; for (int i=1; i<=n; i++) f=f*i; return f; } }...
View Full Document

This document was uploaded on 08/10/2011.

Page1 / 30

Concepts+Techniques+and+Models+of+Computer+Programming_Part21

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online