Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations...

30
Chapter 3 Objects and Classes

Transcript of Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations...

Page 1: Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.

Chapter 3

Objects and Classes

Page 2: Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.

Objects

Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class

Inheritance – allows us to extend the functionality of an object.

Page 3: Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.

The class construct

Information hiding – no direct access to the data parts of an object

Encapsulation – grouping of data (fields) and operations (methods) with implementation details hidden

Data and operations are accessed using the dot operator

Page 4: Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.

Constructor

controls how an object is initialized

same name as the class – no return type

may be overloaded

if not provided, a default is provided that initializes all data to its default

Page 5: Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.

this reference

a reference to the current object

used to access class members while the class is currently executing

instance variables

methods

pass current object as a parameter

Page 6: Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.

1. class IntCell2. {3. /**4. * Get the stored value5. * @return the stored value6. */7. private int storedValue ;8. 9. public IntCell(int n)10. { storedValue = n; }

11. public int read()12. { return storedValue; }13. public void write(int x)14. { storedValue = x ; }15. }

Page 7: Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.

1. public static void main(String [] args)

2. {

3. IntCell m = new IntCell();

4. System.out.println("Initial Value = "+m.read());

5. }

Page 8: Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.

1. class IntCell2. {3. /**4. * Get the stored value5. * @return the stored value6. */

7. private int storedValue ;

8. public IntCell()9. {10. // this.write(0); 11. this(10);12. }

13. public IntCell(int n)14. { storedValue = n; }15. …………………………..16. }

Page 9: Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.

Accessibility of Class Members

public Protected Package PrivateClass itself Yes Yes Yes Yes

Classes in the same package

Yes Yes Yes No

Subclasses in a different package

Yes Yes No No

Non-subclasses in a different package

Yes No No No

Page 10: Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.

static methods

also called class methods

not associated with an object

used by sending a message to the class

main is an example – used by the interpreter.

other examples include Integer.parseInt and Math.abs

Page 11: Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.

static data

shared by all members of a class

only one copy exists and is accessible to all instances

used for constants – example: MAX_VALUE in the Integer class

initialized when the class is loaded

static initializer – runs during class loading

Page 12: Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.

static method/data

1. class A2. {3. public static double myValue = 0 ;4. public double yourValue;

5. public static void show1()6. {7. System.out.println(“myValue = “+myValue);8. }9. public void show2()10. {11. System.out.println(“yourValue = “+yourValue);12. }13. }

Note that a static method can access only static variables. Non-static method can access both static and non-static variables.

Page 13: Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.

static method/data

public static void main() { double x ; A.show1(); // x=A.myValue; // A.show2(); // x=A.yourValue ; // }

Page 14: Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.

Initialization Block

1. class Square2. {3. private static double [] squareRoots = new double [100];4. private double [] squareRts = new double[100];

5. {6. System.out.println("Hello 1 ");7. for ( int i= 0 ; i< squareRoots.length ; i++ )8. squareRoots[i] = Math.sqrt(i);9. for ( int i= 0 ; i< squareRts.length ; i++ )10. squareRts[i] = Math.sqrt(i);11. }12. public Square()13. {14. System.out.println(“Hello 2 ”);15. }16. }

Page 15: Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.

Static Initialization Block

1. class Square2. {3. private static double [] squareRoots = new double [100];4. private double [] squareRts = new double[100];

5. static6. {7. System.out.println("Hello 1 ");8. for ( int i= 0 ; i< squareRoots.length ; i++ )9. squareRoots[i] = Math.sqrt(i);10. }

11. public Square()12. {13. System.out.println(“Hello 2 ”);14. for ( int i= 0 ; i< squareRoots.length ; i++ )15. squareRoots[i] = Math.sqrt(i);16. }17. }

Page 16: Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.

The instanceof Operator

The instanceof operator performs a run-time test. For example,

if ( obj instanceof Truck) do something;

else do something different;

The instanceof operator is typically used prior to performing a type conversion.

Page 17: Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.

“Standard” Methods

toStringshould format the object data into a String

suitable for printing

equalsthe prototype is always public boolean equals (Object rhs)

default is to compare the reference

locally defined – it should compare the state

Page 18: Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.

Packages

Used to organize similar classes Examples: java.io java.applet

Class C in the package p is specified as p.C.

Java.util.Date today = new java.util.Date()

To avoid using a full package name, use the import directive

Page 19: Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.

Packages

Two forms of import directive:

import java.util.Date;

import java.util.*;

In the first form, Date may be used as a shorthand for a fully qualified class name. In the second form, all classes in the package may be abbreviated with the corresponding class name.

Page 20: Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.

Packages

With the following import directives,

import java.util.Date;import java.io.*;

we may have

Date today = new Date(); FileReader fr = new FileReader(name);

Page 21: Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.

Package Syntax

the package heading

all package classes should be in the same directory

the directory has the same name as the package

compilation is done outside the package.

Page 22: Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.

Final Notes on Packages

The CLASSPATH variable – sets the path to look at for packages that are imported.

friendly access – default alternative to public and private – access to others in the same package

Page 23: Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.

How to Use Javadoc.

Page 24: Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.

How to Use Javadoc.

Page 25: Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.

How to Use Javadoc.

Page 26: Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.
Page 27: Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.
Page 28: Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.
Page 29: Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.

1. import java.io.*;

2. /**3. * This is the solution of the HW3B4. * <p>Title: HW3 Part II </p>5. * <p>Description: Answer for HW3 Part II</p>6. * <p>Copyright: Copyright (c) 2004</p>7. * <p>Company: Valdosta State University</p>8. * @author Jaehoon Seol9. * @version 1.010. */

11. public class CS1302HW3B {12. /**13. * The main starting point14. * @param n hour no input parameter15. * @see Your Textbook16. * @return none no return value17. * @deprecated This method is deprecated due to some reason.18. */19. public void show(int n)20. {21. System.out.println("Test");22. }23. public static void main(String [] args)

Page 30: Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.

All javadoc comments before import statement will be ignored The first string after @param should match the variable name of

the parameter. That is

@param h hour informationpublic void showTime( int h){…………..}

@author paragraph will not be generated automatically. Use:

>javadoc –author filename.java

@version paragraph will not be generated automatically. Use:

>javadoc –version filename.java