Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5 Classes.

Similar presentations


Presentation on theme: "Chapter 5 Classes."— Presentation transcript:

1 Chapter 5 Classes

2 Class and member scope A top-level class (i.e., a class not nested inside another) has either package or public scope: class C { /*...*/ } public class Q { /*...*/ } Package scope is the default. A class must be declared as public to have public scope.

3 Class and member scope A class with package scope is accessible in the same package, whether the package be default or explicitly named. A class with public scope is accessible outside of its containing package as well as inside of its containing package.

4 Class and member scope Class members can have private, package, protected, or public scope. Package scope is the default for class members just as package scope is the default for classes. To have nonpackage scope, a member must be declared explicitly as private, protected, or public.

5 Class and member scope The class declaration
public class C { private int n1; // private int n2; // package protected int n3; // protected public int n4; // public } illustrates possible member scopes.

6 Class and member scope A member with private scope is accessible only within its encapsulating class. A member with package scope is accessible only within its package. A member with protected scope is accessible in its containing package and in subclasses of its encapsulating class.

7 Class and member scope A member with public scope is accessible wherever its encapsulating class is accessible. If a member with public scope is encapsulated in a class with package scope, the public member is accessible only in the containing package.

8 Class and member scope Information hiding dictates that a class and its members should have the narrowest possible scope consistent with class services. Public members are typically constructors and high-level methods.

9 Constructors Constructors are used with the new operator to construct class instances. A concrete class is a class for which instances can be constructed. An abstract class cannot have instances. A constructor must have the name of its encapsulating class and no return type or void in place of a return type.

10 Constructors Constructors can have any member scope.
There are two ways for a class to have a public no-argument constructor: The class defines no constructors at all. In this case, the compiler in effect provides a public no-argument constructor. The class explicitly defines a public no-argument constructor.

11 Constructors If a class defines any constructors at all, the compiler does not provide a public no-argument constructor. Constructors can be used to restrict and to guide object construction. For instance, if the Emp class’s only constructor expects employee’s name as a string, then only named Employees can be constructed.

12 Constructors Unreferenced objects can be constructed. The code segment
System.out.println( new Date() ); illustrates. Constructors are typically overloaded; that is, a class typically has several constructors.

13 Methods Methods can have private, package, protected, or public scope.
Methods must have a return type or void in place of a return type, if the method does not return a value. Methods can be overridden so long as the methods differ in name or number and order of argument types.

14 Methods A return type cannot be used to distinguish two methods. The code segment class C { int m() { /*...*/ } float m() { /*...*/ } //** ERROR } illustrates.

15 Methods Related pairs of get/set methods can be used to define class properties. The code segment class Window { void setBorder( Color c ){/*...*/} Color getBorder() {/*...*/} //... } illustrates with a color border property.

16 Methods A method can construct an object of its encapsulating class’s type and return a reference to the object. Such methods are known as factory methods and are an alternative to explicit constructor invocations. A static method can access only other static members.

17 Fields A field can have private, package, protected, or public scope. Because fields often provide low-level implementation support, they are often private. A field has a default value if not initialized in its declaration. Zero is the default value for numbers and false for booleans. Field object references default to null.

18 Class libraries Related classes can be collected into a package, which then serves as a software library. The standard packages provide many rich examples. For instance, the java.net package provides a class library for network programming.

19 Class libraries A good way to gain fluency in a class or class library is to write a test client, that is, a program that explores and tests a class’s or a class library’s services. A programmer-defined class should be accompanied by a test client that illustrates typical client use of the class.


Download ppt "Chapter 5 Classes."

Similar presentations


Ads by Google