30
IADCS Diploma course IADCS Diploma course Classes and Objects in Classes and Objects in Java Java U Nyein Oo COO/Director(IT) Myanma Computer Co., Ltd

Object and Classes in Java

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Object and Classes in Java

IADCS Diploma courseIADCS Diploma courseClasses and Objects in JavaClasses and Objects in Java

U Nyein Oo

COO/Director(IT)

Myanma Computer Co., Ltd

Page 2: Object and Classes in Java

Copyright : MCC Classes & Objects in Java 2

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

Page 3: Object and Classes in Java

Copyright : MCC Classes & Objects in Java 3

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

Page 4: Object and Classes in Java

Copyright : MCC Classes & Objects in Java 4

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

Page 5: Object and Classes in Java

Copyright : MCC Classes & Objects in Java 5

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

Page 6: Object and Classes in Java

Copyright : MCC Classes & Objects in Java 6

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

Page 7: Object and Classes in Java

Copyright : MCC Classes & Objects in Java 7

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

Page 8: Object and Classes in Java

Copyright : MCC Classes & Objects in Java 8

Reachability Levels of Objects

Page 9: Object and Classes in Java

Copyright : MCC Classes & Objects in Java 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

Page 10: Object and Classes in Java

Copyright : MCC Classes & Objects in Java 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

Page 11: Object and Classes in Java

Copyright : MCC Classes & Objects in Java 11

Shallow and Deep Copies

Page 12: Object and Classes in Java

Copyright : MCC Classes & Objects in Java 12

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

Page 13: Object and Classes in Java

Copyright : MCC Classes & Objects in Java 13

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

Page 14: Object and Classes in Java

Copyright : MCC Classes & Objects in Java 14

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

Page 15: Object and Classes in Java

Copyright : MCC Classes & Objects in Java 15

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

Page 16: Object and Classes in Java

Copyright : MCC Classes & Objects in Java 16

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

Page 17: Object and Classes in Java

Copyright : MCC Classes & Objects in Java 17

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

Page 18: Object and Classes in Java

Copyright : MCC Classes & Objects in Java 18

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)

Page 19: Object and Classes in Java

Copyright : MCC Classes & Objects in Java 19

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

Page 20: Object and Classes in Java

Copyright : MCC Classes & Objects in Java 20

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

Page 21: Object and Classes in Java

Copyright : MCC Classes & Objects in Java 21

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

Page 22: Object and Classes in Java

Copyright : MCC Classes & Objects in Java 22

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)

Page 23: Object and Classes in Java

Copyright : MCC Classes & Objects in Java 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.

Page 24: Object and Classes in Java

Copyright : MCC Classes & Objects in Java 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

Page 25: Object and Classes in Java

Copyright : MCC Classes & Objects in Java 25

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

Page 26: Object and Classes in Java

Copyright : MCC Classes & Objects in Java 26

Name Conflicts in Inner ClassesName Conflicts in Inner Classes

Page 27: Object and Classes in Java

Copyright : MCC Classes & Objects in Java 27

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

Page 28: Object and Classes in Java

Copyright : MCC Classes & Objects in Java 28

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

Page 29: Object and Classes in Java

Copyright : MCC Classes & Objects in Java 29

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

Page 30: Object and Classes in Java

Copyright : MCC Classes & Objects in Java 30

Thanks You!Thanks You!