Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Chapter 3 Objects and Classes

2 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.

3 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

4 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

5 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

6 1. class IntCell 2. { 3. /** 4. * Get the stored value 5. * @return the stored value 6. */ 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. }

7 1. public static void main(String [] args) 2. { 3. IntCell m = new IntCell(); 4. System.out.println("Initial Value = "+m.read()); 5. }

8 1. class IntCell 2. { 3. /** 4. * Get the stored value 5. * @return the stored value 6. */ 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. }

9 Accessibility of Class Members publicProtectedPackagePrivate Class itself Yes Classes in the same package Yes No Subclasses in a different package Yes No Non-subclasses in a different package YesNo

10 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

11 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

12 static method/data 1. class A 2. { 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.

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

14 Initialization Block 1. class Square 2. { 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. }

15 Static Initialization Block 1. class Square 2. { 3. private static double [] squareRoots = new double [100]; 4. private double [] squareRts = new double[100]; 5. static 6. { 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. }

16 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.

17 “Standard” Methods toString should format the object data into a String suitable for printing equals the prototype is always public boolean equals (Object rhs) default is to compare the reference locally defined – it should compare the state

18 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

19 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.

20 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);

21 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.

22 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

23 How to Use Javadoc.

24

25

26

27

28

29 1. import java.io.*; 2. /** 3. * This is the solution of the HW3B 4. * Title: HW3 Part II 5. * Description: Answer for HW3 Part II 6. * Copyright: Copyright (c) 2004 7. * Company: Valdosta State University 8. * @author Jaehoon Seol 9. * @version 1.0 10. */ 11. public class CS1302HW3B { 12. /** 13. * The main starting point 14. * @param n hour no input parameter 15. * @see Your Textbook 16. * @return none no return value 17. * @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)

30 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 information public 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


Download ppt "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."

Similar presentations


Ads by Google