Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.

Similar presentations


Presentation on theme: "Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming."— Presentation transcript:

1 Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming

2 Introduction to Java Chapter 7 - Classes & Object-oriented Programming2 Classes A class is the basic unit of the Java language. It is the “blueprint” for the objects created from that class. Each class contains some data definitions (called fields), together with methods to manipulate that data. –When the object is instantiated from the class, an instance variable is created for each field in the class. The methods serve as an interface to isolate the data in the class from the outside world.

3 Introduction to Java Chapter 7 - Classes & Object-oriented Programming3 Class Hierarchy All classes form a part of a class hierarchy. –Classes below a given class are subclasses of the class. –Classes above a given class are superclasses of the class. The class immediately above a given class is known as its immediate superclass. A class inherits both instance variables and methods from it’s immediate superclass. It can add additional variables and methods, and it can override (change) the inherited methods.

4 Introduction to Java Chapter 7 - Classes & Object-oriented Programming4 Structure of a Class The major components (members) of a class are: –Fields - define the instance variables to be created when an object is instantiated from the class. –Constructors - special methods the define how to initialize variables when an object is instantiated. –Methods - implement the behaviors of a class. –Finalizer - a special method to perform cleanup before an object is destroyed.

5 Introduction to Java Chapter 7 - Classes & Object-oriented Programming5 The Member Access Operator (.) The members of a class (instance variables and methods) are accessed using the member access operator, or dot operator (.) –To access a member, the user names a reference to a object, followed by the dot operator, and followed by the member name (with no spaces) Examples: Obj.a Access instance variable a Obj.methodA Access method methodA

6 Introduction to Java Chapter 7 - Classes & Object-oriented Programming6 Example: Timer Class Method resetTimer Class definition Instance variable definition Constructor Method elapsedTime Note: This method does not have a finalizer.

7 Introduction to Java Chapter 7 - Classes & Object-oriented Programming7 Scope Instance variables have class scope, meaning that they are visible everywhere within a class, including within methods –Example: variable savedTime in class Timer. Local variables within a method have block scope, meaning that they are only visible within the block in which they are defined. –Example: variable eTime in method elapsedTime.

8 Introduction to Java Chapter 7 - Classes & Object-oriented Programming8 Hidden Instance Variables It is possible for a local variable to have the same name as an instance variable, hiding it within a block. In this case, the hidden instance variable can be accessed using the this reference. Instance variables Local variables Reference hidden instance variables using this

9 Introduction to Java Chapter 7 - Classes & Object-oriented Programming9 Common Types of Methods Found in a Class Certain type of methods are common to many classes –“ set methods” are used to set the values of instance variables –“ get methods” are used to read the values of instance variables –predicate methods return a true / false result based on some test. –toString method creates a string representation of the contents of the object

10 Introduction to Java Chapter 7 - Classes & Object-oriented Programming10 Packages A package is a group of classes and methods that share some related purpose. All standard Java classes are grouped in packages, such as java.lang, java.io, etc. To use the contents of any package except java.lang, it must first be imported into a program with an import statement. The import statements must be the first non- comment lines in a program!

11 Introduction to Java Chapter 7 - Classes & Object-oriented Programming11 Creating Your Own Packages You can create packages for your own methods: –First, add a package statement to each class specifying the package that it belongs to. Place the statement before the class definition: package chapman.testpackage; –Then, compile using the -d option to specify the root directory of the package structure: javac -d d:\packages MyClass.java; –These commands will place file MyClass.class in directory d:\packages\chapman\testpackage.

12 Introduction to Java Chapter 7 - Classes & Object-oriented Programming12 Example Creating MyClass package statement Compilation statement The class file will now be in directory d:\packages\chapman\testpackage

13 Introduction to Java Chapter 7 - Classes & Object-oriented Programming13 Using Your Own Packages To use the classes in your packages, you must add an import statement to each class wanting to access the packages. In addition, you must add the root directory of the package structure to the CLASSPATH environment variable. Example: If class chapman.testpackage.MyClass is in directory d:\package\chapman\testpackage, then the root directory of the package structure is d:\package, and it must appear in a CLASSPATH statement.

14 Introduction to Java Chapter 7 - Classes & Object-oriented Programming14 Example Using MyClass Set class path in environment import statement

15 Introduction to Java Chapter 7 - Classes & Object-oriented Programming15 Member Access Modifiers Member access modifiers control where a class member can be accessed from: –public : Members can be accessed from any class –private : Members can only be accessed from within the class that they are defined in – : With no modifier, members can be accessed from any class in the same package as the class that they are defined in. –protected : Access from the same package or from any subclass of the class that they are defined in

16 Introduction to Java Chapter 7 - Classes & Object-oriented Programming16 Finalizers A finalizer is a special method named finalize, which performs any necessary clean-up (releasing resources, etc.) before an object is destroyed. It is automatically called just before an object is destroyed. Most classes do not need a finalizer.

17 Introduction to Java Chapter 7 - Classes & Object-oriented Programming17 Garbage Collection When an object is no longer needed, it is automatically destroyed by a low-priority thread called the garbage collector. Destroying old objects returns their resources to the system for re-use. Any object that no longer has a reference pointing to it is a candidate for garbage collection. The garbage collector calls a class’s finalizer before destroying it.

18 Introduction to Java Chapter 7 - Classes & Object-oriented Programming18 Static Variables A static variable is a special type of variable that is shared by all objects instantiated from a class. Static variables are useful for keeping track of global information such as the number of objects instantiated from a class, etc. They are declared with the static keyword: private static int created; // Static! Static variables are also useful for declaring a single shared copy of a constant: static final double C = 2.99792458e8;

19 Introduction to Java Chapter 7 - Classes & Object-oriented Programming19 Example Class A definition with two instance variables and one static variable x an y are unique in each object, while s is common to both Two objects a1 and a2 instantiated from class A

20 Introduction to Java Chapter 7 - Classes & Object-oriented Programming20 Static Methods Static methods are methods that can be executed without first instantiating an object of the class containing the method. Static methods can access the static variables in a class, but they cannot access instance variables. Static methods are normally used for utility calculations that are independent of the data in a class, such as sin(), cos(), tan(), etc.

21 Introduction to Java Chapter 7 - Classes & Object-oriented Programming21 Example: Extended Math Class static methods static variable


Download ppt "Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming."

Similar presentations


Ads by Google