Chapter 7. Classes


If you are unfamiliar with object-oriented programming, you might find talk about classes and objects rather mysterious . In reality classes are not all that complicated to understand. A class is a named block of code that defines a reference type. The class is the basic building block of all Java programs. The class provides the structure or blueprint for objects. An object is an instance of a class.

Classes will, in general, consist of members (fields, methods , and nested classes and interfaces), initializers (both instance and static), and constructors. The fields define the data structure of the class. The methods will generally be used to access and manipulate the fields. Initializers and constructors are normally used to provide initial values for the fields. None of these elements is required although most classes will define fields and methods. A well-written class should satisfy the object-oriented concept of encapsulation in that it should define all of the methods and fields that it needs.

Languages like C and Fortran are function-based. The basic building block of a C or Fortran program is the function (or subroutine). Java programs are class-based. Every statement will be contained inside a class (or in some cases an interface). When a Java program runs, the execution will largely consist of creating and manipulating objects.

You might still be wondering what sort of classes you will write. The answer is any kind and every kind. If you are a structural engineer, you might write a Beam class that would define variables that describe the properties of a beam and methods to manipulate the variables . A chemist might write a Species class that represents a chemical species. If you are going to perform a finite-difference analysis, you might write a Grid class that would encapsulate a computational grid.

In this chapter will we look at classes, how to declare them, what their features are, and how to create instances of them. Specifically, we will discuss ”

  • Class declaration syntax

  • Access privileges

  • Objects

  • Declaring fields

  • Declaring methods

  • Constructors

  • Static initialization blocks

  • Making copies of objects

  • Nested classes

  • Encapsulation

  • Inheritance

  • The super keyword

  • The this keyword

  • Abstract classes

  • Final classes

  • The garbage collector



Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net