SlideShare a Scribd company logo
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!

More Related Content

What's hot (20)

PPTX
Java class,object,method introduction
Sohanur63
 
PPTX
Week9 Intro to classes and objects in Java
kjkleindorfer
 
PPT
Java basic
Sonam Sharma
 
PPT
Lect 1-class and object
Fajar Baskoro
 
PPTX
Pi j3.2 polymorphism
mcollison
 
PPT
Classes&objects
M Vishnuvardhan Reddy
 
PDF
Core java complete notes - Contact at +91-814-614-5674
Lokesh Kakkar Mobile No. 814-614-5674
 
PDF
Object Oriented Programming using JAVA Notes
Uzair Salman
 
PDF
Built in classes in java
Mahmoud Ali
 
PPT
11 Using classes and objects
maznabili
 
PPTX
Classes and objects
rajveer_Pannu
 
PPTX
Chapter 05 classes and objects
Praveen M Jigajinni
 
PPTX
Unit3 part3-packages and interfaces
DevaKumari Vijay
 
PDF
CLASS & OBJECT IN JAVA
Riaj Uddin Mahi
 
PPT
Class and object in C++
rprajat007
 
PPTX
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
DevaKumari Vijay
 
PPTX
Classes and objects
Shailendra Veeru
 
PDF
Object Oriented Programming in PHP
Lorna Mitchell
 
PDF
Python Class | Python Programming | Python Tutorial | Edureka
Edureka!
 
Java class,object,method introduction
Sohanur63
 
Week9 Intro to classes and objects in Java
kjkleindorfer
 
Java basic
Sonam Sharma
 
Lect 1-class and object
Fajar Baskoro
 
Pi j3.2 polymorphism
mcollison
 
Classes&objects
M Vishnuvardhan Reddy
 
Core java complete notes - Contact at +91-814-614-5674
Lokesh Kakkar Mobile No. 814-614-5674
 
Object Oriented Programming using JAVA Notes
Uzair Salman
 
Built in classes in java
Mahmoud Ali
 
11 Using classes and objects
maznabili
 
Classes and objects
rajveer_Pannu
 
Chapter 05 classes and objects
Praveen M Jigajinni
 
Unit3 part3-packages and interfaces
DevaKumari Vijay
 
CLASS & OBJECT IN JAVA
Riaj Uddin Mahi
 
Class and object in C++
rprajat007
 
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
DevaKumari Vijay
 
Classes and objects
Shailendra Veeru
 
Object Oriented Programming in PHP
Lorna Mitchell
 
Python Class | Python Programming | Python Tutorial | Edureka
Edureka!
 

Viewers also liked (20)

PPTX
Class object method constructors in java
Raja Sekhar
 
PPTX
class and objects
Payel Guria
 
PPTX
Constructor ppt
Vinod Kumar
 
PPT
Object Oriented Programming Concepts
thinkphp
 
PDF
Classes and Nested Classes in Java
Ravi_Kant_Sahu
 
PPTX
Classes And Objects
rahulsahay19
 
PPS
Java Exception handling
kamal kotecha
 
PPTX
Introduction to java
Veerabadra Badra
 
PPT
06 abstract-classes
Anup Burange
 
PPTX
Modul Kelas Programming : Java swing (session 2)
FgroupIndonesia
 
PPTX
Modul Kelas Programming : Introduction to java
FgroupIndonesia
 
PPT
Character stream classes .52
myrajendra
 
PPT
Byte stream classes.49
myrajendra
 
PPT
Inner classes9 cm604.28
myrajendra
 
DOCX
Nested classes in java
Richa Singh
 
PDF
OOP: Classes and Objects
Atit Patumvan
 
PPTX
Inner classes
DraftKing Zohaib
 
PDF
Files in java
Muthukumaran Subramanian
 
PPT
Inner classes ,annoumous and outer classes in java
Adil Mehmoood
 
PPT
Various io stream classes .47
myrajendra
 
Class object method constructors in java
Raja Sekhar
 
class and objects
Payel Guria
 
Constructor ppt
Vinod Kumar
 
Object Oriented Programming Concepts
thinkphp
 
Classes and Nested Classes in Java
Ravi_Kant_Sahu
 
Classes And Objects
rahulsahay19
 
Java Exception handling
kamal kotecha
 
Introduction to java
Veerabadra Badra
 
06 abstract-classes
Anup Burange
 
Modul Kelas Programming : Java swing (session 2)
FgroupIndonesia
 
Modul Kelas Programming : Introduction to java
FgroupIndonesia
 
Character stream classes .52
myrajendra
 
Byte stream classes.49
myrajendra
 
Inner classes9 cm604.28
myrajendra
 
Nested classes in java
Richa Singh
 
OOP: Classes and Objects
Atit Patumvan
 
Inner classes
DraftKing Zohaib
 
Inner classes ,annoumous and outer classes in java
Adil Mehmoood
 
Various io stream classes .47
myrajendra
 
Ad

Similar to Object and Classes in Java (20)

DOCX
Question and answer Programming
Inocentshuja Ahmad
 
PPTX
4-OOPS.pptx
SatyamMishra237306
 
PPTX
Java basics
Shivanshu Purwar
 
PPTX
Objects and classes in OO Programming concepts
researchveltech
 
PPT
Object -oriented analysis and design.ppt
pierrerj05
 
PPTX
Object Oriented Programming
Manish Pandit
 
PDF
Java pdf
Vinod Panchatcharam
 
PDF
JAVA-PPT'S.pdf
AnmolVerma363503
 
PPT
Md02 - Getting Started part-2
Rakesh Madugula
 
PPTX
Object oriented programming CLASSES-AND-OBJECTS.pptx
DaveEstonilo
 
PPTX
Object Oriented Programming Tutorial.pptx
ethiouniverse
 
PPTX
JAVA-PPT'S-complete-chrome.pptx
KunalYadav65140
 
PPTX
JAVA-PPT'S.pptx
RaazIndia
 
PPTX
UNIT I OOP AND JAVA FUNDAMENTALS CONSTRUCTOR
mohanrajm63
 
PPTX
L03 Software Design
Ólafur Andri Ragnarsson
 
PPT
Object concepts
Aashima Wadhwa
 
PPT
Java oops PPT
kishu0005
 
PDF
Lecture 2 cst 205-281 oop
ktuonlinenotes
 
PPTX
[OOP - Lec 04,05] Basic Building Blocks of OOP
Muhammad Hammad Waseem
 
PPT
025466482929 -OOP with Java Development Kit.ppt
DakshinaPahan
 
Question and answer Programming
Inocentshuja Ahmad
 
4-OOPS.pptx
SatyamMishra237306
 
Java basics
Shivanshu Purwar
 
Objects and classes in OO Programming concepts
researchveltech
 
Object -oriented analysis and design.ppt
pierrerj05
 
Object Oriented Programming
Manish Pandit
 
JAVA-PPT'S.pdf
AnmolVerma363503
 
Md02 - Getting Started part-2
Rakesh Madugula
 
Object oriented programming CLASSES-AND-OBJECTS.pptx
DaveEstonilo
 
Object Oriented Programming Tutorial.pptx
ethiouniverse
 
JAVA-PPT'S-complete-chrome.pptx
KunalYadav65140
 
JAVA-PPT'S.pptx
RaazIndia
 
UNIT I OOP AND JAVA FUNDAMENTALS CONSTRUCTOR
mohanrajm63
 
L03 Software Design
Ólafur Andri Ragnarsson
 
Object concepts
Aashima Wadhwa
 
Java oops PPT
kishu0005
 
Lecture 2 cst 205-281 oop
ktuonlinenotes
 
[OOP - Lec 04,05] Basic Building Blocks of OOP
Muhammad Hammad Waseem
 
025466482929 -OOP with Java Development Kit.ppt
DakshinaPahan
 
Ad

More from backdoor (20)

PPT
Java Database Connectivity
backdoor
 
PPT
Distributed Programming using RMI
backdoor
 
PPT
Programming Server side with Sevlet
backdoor
 
PPT
Distributed Programming using RMI
backdoor
 
PPT
Client Side Programming with Applet
backdoor
 
PPT
Java Network Programming
backdoor
 
PPT
Windows Programming with Swing
backdoor
 
PPT
Windows Programming with AWT
backdoor
 
PPT
Multithreading
backdoor
 
PPT
IO and serialization
backdoor
 
PPT
Exception Handling
backdoor
 
PPT
Java Intro
backdoor
 
PPT
Object Oriented Programming with Java
backdoor
 
PPT
AWT Program output
backdoor
 
PPT
Net Man
backdoor
 
PPT
Data Security
backdoor
 
PPT
Ne Course Part One
backdoor
 
PPT
Ne Course Part Two
backdoor
 
PPT
Net Sec
backdoor
 
PDF
Security Policy Checklist
backdoor
 
Java Database Connectivity
backdoor
 
Distributed Programming using RMI
backdoor
 
Programming Server side with Sevlet
backdoor
 
Distributed Programming using RMI
backdoor
 
Client Side Programming with Applet
backdoor
 
Java Network Programming
backdoor
 
Windows Programming with Swing
backdoor
 
Windows Programming with AWT
backdoor
 
Multithreading
backdoor
 
IO and serialization
backdoor
 
Exception Handling
backdoor
 
Java Intro
backdoor
 
Object Oriented Programming with Java
backdoor
 
AWT Program output
backdoor
 
Net Man
backdoor
 
Data Security
backdoor
 
Ne Course Part One
backdoor
 
Ne Course Part Two
backdoor
 
Net Sec
backdoor
 
Security Policy Checklist
backdoor
 

Recently uploaded (20)

PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
PDF
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PDF
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 

Object and Classes in Java

  • 1. IADCS Diploma course Classes and Objects in Java U Nyein Oo COO/Director(IT) Myanma Computer Co., Ltd
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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)
  • 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
  • 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
  • 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
  • 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)
  • 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 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
  • 26. Name Conflicts in Inner Classes
  • 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
  • 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
  • 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