Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations...
-
Upload
preston-obrien -
Category
Documents
-
view
224 -
download
2
Transcript of Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations...
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
Inheritance – allows us to extend the functionality of an object.
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
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
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
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. }
1. public static void main(String [] args)
2. {
3. IntCell m = new IntCell();
4. System.out.println("Initial Value = "+m.read());
5. }
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. }
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
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
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
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.
static method/data
public static void main() { double x ; A.show1(); // x=A.myValue; // A.show2(); // x=A.yourValue ; // }
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. }
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. }
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.
“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
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
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.
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);
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.
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
How to Use Javadoc.
How to Use Javadoc.
How to Use Javadoc.
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)
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