Object and Classes in Java

3,717 views

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,717
On SlideShare
0
From Embeds
0
Number of Embeds
473
Actions
Shares
0
Downloads
196
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Object and Classes in Java

  1. 1. IADCS Diploma course Classes and Objects in Java U Nyein Oo COO/Director(IT) Myanma Computer Co., Ltd
  2. 2. Classes and Object-Oriented Programming <ul><li>Classes should be the implementation of a design or object model </li></ul><ul><li>Classes reflect the types of real-world objects and relationships between objects </li></ul><ul><li>Attributes become fields of classes, and messages become methods of classes </li></ul><ul><li>Design the interface between classes </li></ul>
  3. 3. Using Constructors and Finalizers <ul><li>Constructors: methods that prepare newly created objects for use </li></ul><ul><li>Finalizers: are methods that perform any actions, such as releasing memory buffers or closing network connections, that should be completed before the objects are discarded </li></ul>
  4. 4. Constructors <ul><li>Rules to define constructors are: </li></ul><ul><ul><li>Give the constructor the same name as the class name </li></ul></ul><ul><ul><li>Do not specify a return type, not even void </li></ul></ul><ul><ul><li>You can specify as many arguments as you like </li></ul></ul>
  5. 5. Finalizers <ul><li>Use finalizers only for necessary cleanup tasks (example: releasing memory buffers allocated by the object or closing network connections) </li></ul><ul><li>A finalizer cannot have private or package access because it overrides a protected method of Object </li></ul>
  6. 6. Finalizers (Cont.) <ul><li>The finalize method cannot take arguments or return a value </li></ul><ul><li>Include the clause throws Throwable to allow for a subclass to include a finalizer that does throw an exception </li></ul>
  7. 7. Reference Objects and Garbage Collector <ul><li>Create a reference object by instantiating the classes SoftReference, WeakReference, or PhantomReference and passing an object reference to an object used by your program as an argument of the constructor </li></ul><ul><li>When the garbage collector operates on objects, it changes the reachability level of the object </li></ul><ul><li>Use the reference classes to determine the status of an object </li></ul>
  8. 8. Reachability Levels of Objects
  9. 9. Cloning Objects <ul><li>For the primitive types, you can copy with the assignment operator </li></ul><ul><li>To copy objects of reference type, including arrays, use the clone method </li></ul><ul><li>By default, the clone method performs a shallow copy </li></ul>
  10. 10. Cloning Objects (Cont.) <ul><li>A shallow copy makes no attempt to make duplicates of the referenced objects </li></ul><ul><li>A deep copy duplicates contained objects, creates new object references for the duplicates, and inserts the new object references into the copy of the containing object </li></ul>
  11. 11. Shallow and Deep Copies
  12. 12. Making Objects Cloneable <ul><li>To be cloneable, an object must be an instance of a class that implements the interface Cloneable </li></ul><ul><li>The clone method throws an exception when called for an instance of a class that is not cloneable </li></ul><ul><li>The Cloneable interface is an example of a marker interface </li></ul><ul><li>The purpose of a marker interface is to attach a piece of type information to a class </li></ul>
  13. 13. Overriding the Default Clone Method <ul><li>If your classes contain fields that have reference types, override the clone method to perform a deep copy </li></ul><ul><li>Start the implementation of clone with the statement super.clone( ) </li></ul><ul><li>Define the clone method to copy objects for all fields that are reference objects </li></ul>
  14. 14. Defining Cloneable Classes <ul><li>Override or inherit clone </li></ul><ul><li>Optionally define your class to implement Cloneable </li></ul><ul><li>Optionally throw the exception CloneNotSupportedException </li></ul><ul><li>Optionally catch the exception CloneNotSupportedException </li></ul><ul><li>List or omit the exception in the throws clause of the clone method </li></ul>
  15. 15. Run-Time Type Information <ul><li>Run-time type information (RTTI) is the type information about the objects in use within a program cannot be known until the program is running </li></ul><ul><li>Use RTTI to access members of classes that you cannot know about when you develop your class </li></ul><ul><li>RTTI is helpful in debugging the code </li></ul>
  16. 16. Determining the Type of Objects <ul><li>Use of the instanceof operator </li></ul><ul><ul><li>To ask whether an object is an instance of a particular class or any of its subclasses </li></ul></ul><ul><ul><li>Returns a boolean value </li></ul></ul><ul><ul><ul><li>True if the object on the left is an instance of the class or a subclass of the class on the right </li></ul></ul></ul><ul><ul><li>Does not work for the primitive types </li></ul></ul>
  17. 17. Accessing Information about Classes at Runtime <ul><li>The Java platform creates the Class object automatically and stores several facts about the class (accessible at runtime) </li></ul><ul><li>You can call a method to get the Class object for a class or an instance of a class </li></ul><ul><li>After you have a reference to a Class object, you can send messages to it to ask questions about the class that the object describes </li></ul>
  18. 18. Casting Between Types <ul><li>Casting forces an object or primitive type to behave as a different type of object or primitive type </li></ul><ul><li>If a cast is definitely unsafe, the compiler outputs an error message </li></ul><ul><li>If cast might be unsafe, the compiler assumes the code is correct and leaves the decision to the JVM (runtime checking) </li></ul>
  19. 19. Using the Reflection API <ul><li>Reflection API: </li></ul><ul><ul><li>The classes in the package java.lang.reflect </li></ul></ul><ul><ul><li>Supports introspection, which essentially asks a class to describe itself </li></ul></ul><ul><ul><li>Gives an object the ability to reflect upon itself and discover its contents </li></ul></ul><ul><li>Three classes represent the building blocks of classes: </li></ul><ul><ul><li>Constructor </li></ul></ul><ul><ul><li>Method </li></ul></ul><ul><ul><li>Field </li></ul></ul>
  20. 20. Calling Methods with the Reflection API <ul><li>Call the method by name or create an object with the new keyword </li></ul><ul><li>Use a Method object or a Constructor object when you must use the Reflection API to discover which methods are available, or to determine the type of an object </li></ul>
  21. 21. Nested Classes and Interfaces <ul><li>Nested class: </li></ul><ul><ul><li>Defined inside the definition of another class </li></ul></ul><ul><ul><li>Are top-level classes </li></ul></ul><ul><ul><li>Nested top-level classes are declared with the keyword static </li></ul></ul><ul><li>Rules for accessing a nested class resemble the rules for accessing members of the enclosing class </li></ul>
  22. 22. Nested Classes and Interfaces (Cont.) <ul><li>The names of nested classes consist of: </li></ul><ul><ul><li>package name </li></ul></ul><ul><ul><li>enclosing class names </li></ul></ul><ul><ul><li>the simple name of the nested class (separated by dots) </li></ul></ul>
  23. 23. Inner Classes <ul><li>Three kinds of inner classes are possible: </li></ul><ul><ul><li>Member inner classes: defined inside another class, at the same level as fields and methods </li></ul></ul><ul><ul><ul><li>They are members of the enclosing class </li></ul></ul></ul><ul><ul><li>Local inner classes: defined inside blocks of code </li></ul></ul><ul><ul><ul><li>They are local to the enclosing method or block </li></ul></ul></ul><ul><ul><li>Anonymous inner classes: local inner classes that have no name. </li></ul></ul>
  24. 24. Inner Classes (Cont.) <ul><li>Use inner classes to create adapter classes within an existing class </li></ul><ul><li>Adapter classes </li></ul><ul><ul><li>Programming design pattern </li></ul></ul><ul><ul><li>Create a wrapper for the data of a class to present it within a different interface that the class does not implement </li></ul></ul><ul><li>An inner class implements an interface when the interface defines only one method </li></ul>
  25. 25. Defining Member Inner Classes <ul><li>Inner classes </li></ul><ul><ul><li>Instance members of their enclosing classes </li></ul></ul><ul><ul><li>Can be declared public, protected, or private </li></ul></ul><ul><ul><li>Cannot be instantiated without an instance of the outer class </li></ul></ul><ul><li>To create the inner class object in an instance method of the enclosing class, the object reference is implied </li></ul>
  26. 26. Name Conflicts in Inner Classes
  27. 27. Local Inner Classes <ul><li>Local inner classes </li></ul><ul><ul><li>Classes declared inside methods </li></ul></ul><ul><ul><li>Private to the blocks in which they are defined </li></ul></ul><ul><ul><li>The names of the classes cannot be used anywhere except in the method in which they are defined </li></ul></ul><ul><li>Objects of a local inner class and all the objects within their extended states live beyond the scopes in which they are created </li></ul>
  28. 28. Anonymous Local Inner Classes <ul><li>Anonymous inner classes </li></ul><ul><ul><li>Local inner classes that have no name </li></ul></ul><ul><ul><li>Have neither names nor constructors </li></ul></ul><ul><li>To initialize an object, an instance initializer is used </li></ul><ul><li>An instance initializer has the same syntax as a static initializer, but without the keyword static </li></ul>
  29. 29. Class Files for Nested and Inner Classes <ul><li>Each nested top-level class or inner class is stored in its own .class file </li></ul><ul><li>The filename generated for .class files consists of </li></ul><ul><ul><li>The enclosing class name </li></ul></ul><ul><ul><li>A dollar sign character ($) </li></ul></ul><ul><ul><li>The enclosed class name (for every level of nesting) </li></ul></ul><ul><li>An anonymous class is identified by a number </li></ul>
  30. 30. Thanks You!

×