X hits on this document





26 / 396

Chapter ‎18   Unsafe code




Access not limited


Access limited to this class and classes derived from this class


Access limited to this program

protected internal

Access limited to this program and classes derived from this class


Access limited to this class

1.6.3 Base classes

A class declaration may specify a base class by following the class name with a colon and the name of the base class. Omitting a base class specification is the same as deriving from type object. In the following example, the base class of Point3D is Point, and the base class of Point is object:

public class Point { public int x, y;

public Point(int x, int y) { this.x = x; this.y = y; } }

public class Point3D: Point { public int z;

public Point3D(int x, int y, int z): Point(x, y) { this.z = z; } }

A class inherits the members of its base class. Inheritance means that a class implicitly contains all members of its base class, except for the constructors of the base class. A derived class can add new members to those it inherits, but it cannot remove the definition of an inherited member. In the previous example, Point3D inherits the x and y fields from Point, and every Point3D instance contains three fields, x, y, and z.

An implicit conversion exists from a class type to any of its base class types. Therefore, a variable of a class type can reference an instance of that class or an instance of any derived class. For example, given the previous class declarations, a variable of type Point can reference either a Point or a Point3D:

Point a = new Point(10, 20); Point b = new Point3D(10, 20, 30);

1.6.4 Fields

A field is a variable that is associated with a class or with an instance of a class.

A field declared with the static modifier defines a static field. A static field identifies exactly one storage location. No matter how many instances of a class are created, there is only ever one copy of a static field.

A field declared without the static modifier defines an instance field. Every instance of a class contains a separate copy of all the instance fields of that class.

Copyright Microsoft Corporation 1999-2003. All Rights Reserved.13

Document info
Document views1286
Page views1286
Page last viewedFri Jan 20 23:00:32 UTC 2017