Thanks to visit codestin.com
Credit goes to www.slideshare.net

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

Object and Classes in Java

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