• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Inheritance » Object Class in Java Example
Next →
← Prev

Object Class in Java Example

By Dinesh Thakur

All the classes in Java that you have defined so far are subclasses by default whether or not you have specified the superclass. The class which all classes in Java are descendent of (directly or indirectly) is java.lang.Object class. So each class inherits the instance methods of Object class. It is important to be familiar with the methods provided by the Object class so that you can use them in your class.

Using Object class methods

There are methods in Object class:

Public String toString (): This method returns a string representation of an object. The default implementation returns the name of the class followed by ‘@’ and the hexadecimal representation for the object. You can override this method in your classes, to return your String object for your class.

// Returns a string that lists the host name.
// @, Returns a hexadecimal representation for the object.
public String toString(){
     return getClass().getName()+‘@’+Integer.toHexString(dataSource.hashCode());
}

It is always recommended to override toString() method,returns the desired String representation of Object. So, override of toString() method refer – Overriding toString() in Java
Note : If you print any Object reference, then internally invokes toString() method.

Teacher t = new Teacher();

// Below two statements are equivalent

System.out.println(t);

System.out.println(t.toString());

public boolean equals (Object obj): It compares two objects for equality and returns true if they are equal and false otherwise. The comparison is case-sensitive. The code below is the equals() method in the Object class. The method is checking whether the current instance is the same as the previously passed Object.

public boolean equals(Object obj) {
    return(this== obj);
}

public final class getClass(): This method returns a class Object (package Java.lang) that is locked by static synchronized methods of the represented class, and that contains information about the object’s type, such as its classname, its superclass and the interfaces it implements. It also is used to get metadata of this class.

public class clObject {
   public
static void main(String[] args) {
      Object obj = new String(“ecomputernotes”);
      Class cl = obj.getClass();
      System.out.println(“The returned Class object is the : “ + c.getName());
   }
}

NOTE: The getclass() methods of the Object class declared final since their implementation are essential for proper behavior of an object, and so those methods cannot be overridden by any class.

Public int hashcode (): A hash table is a storage mechanism that creates a key-and-value pair for each element. For example, suppose you are keeping records of employees within a company. You need to develop a mechanism for retrieving these records based on employees’ social security numbers. An array really won’t work well for this because the nine-digit identification number is too large. (Vectors and arrays work best for an ordered list of sequential values.) In this situation, hash tables are the answer.

Hash tables enable quick location and retrieval of data by using a key. This key is associated with data contained within an object. The key and value can be from any object, but the key object must implement the hashCode() and equals() methods. Once these methods have implemented, the hash table computes a small integer from the key; this is the hash code and the location where the key/value pair exists. Unlike arrays and vectors, the order of values in a hash table is not important-the hash code ID is all that matters. What is important is that the keys are unique: Only one value can store for each key. If you add a value to a key that is already holding another value, then the added value replace the preexisting value.

As with other data structures, the hash table’s performance is affected by the ratio between the number of elements and the capacity of the structure. This ratio is called the load factor and represented as a percentage. Hash tables need to allocate more storage space than is needed because they generate the unique hash code from the identifying key. As the load factor increases,
so does the likelihood that two or more generated hash codes have the same value; this is called a collision. The extra space is used to ensure that all key/value pairs remain unique. Ideally, the load factor is not higher than 50%; that is, the capacity should be double the number of elements contained in the hash table. If the load factor goes over 75%, the hash table automatically doubles the capacity to ensure adequate performance.

Protected Object clone() throws CloneNotSupportedException: This method creates a copy of the object on which it is called.

Protected void finalize() throws Throwable: The finalize method is a particular method, declared in the object class which allows cleaning up of objects which are not in use (not referenced).
When any objects no longer reference an object, Java reclaims the memory space using garbage collection. Java calls a finalize method before garbage collection takes place.
Syntax:
void finalize() {
…..body of finalize method
}

Finalize methods always have a return type of void and override a default destructor in java.object.Object.
A program can call finalize directly just as it would any other method. However, calling finalize not initiate any garbage collection. It treated like any other method called directly. When Java does garbage collection, finalize is still called even if it has already been called directly by the program.
The finalize method can be overloaded also. If Java finds a finalize method with arguments at garbage-collection time, it looks for a finalize method with no arguments. If it does not find one, Java uses the default finalize method instead.
The system only calls finalize when it is ready to reclaim the memory associated with the object and not immediately after an object no longer referenced. That is, finalize() is called only when the system is running short of memory so that it may be called long after the application finishes.

Object Class in Java Example

class Rectangle extends Object 
{
     private double length,breadth;
     Rectangle(double x,double y)
     {
       length = x ;
       breadth = y ;
     }
   public void area()
   {
     System.out.println("Area of Rectangle is = " + breadth);
   }
   public void circumference()
   {
   System.out.println("Circumferen of Rectangle is= "+2*(length+breadth));
   }
}
 class ObjectClass
{
     public static void main(String[] args)
   {
           Rectangle r = new Rectangle(10,20);
           Rectangle rl = new Rectangle(10,20);
           System.out.println("String Representation = " + r.toString());
           System.out.println("Class Name = " + r.getClass());
           System.out.println("Hash Code  = " + r.hashCode());
           System.out.println("r.rquals(rl) = " + r.equals(rl));
    }
}

Object Class in Java Example

You’ll also like:

  1. What is the difference between an Object and a Class
  2. What is an Object and class in PHP?
  3. Write A C++ Program To Assign Class Object.
  4. Write A C++ Program To Illustrate The Concept Of Class Having One Object.
  5. Write A C++ Program To Declare Class Instance (Create Object).
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Java Tutorials

Java Tutorials

  • Java - Home
  • Java - IDE
  • Java - Features
  • Java - History
  • Java - this Keyword
  • Java - Tokens
  • Java - Jump Statements
  • Java - Control Statements
  • Java - Literals
  • Java - Data Types
  • Java - Type Casting
  • Java - Constant
  • Java - Differences
  • Java - Keyword
  • Java - Static Keyword
  • Java - Variable Scope
  • Java - Identifiers
  • Java - Nested For Loop
  • Java - Vector
  • Java - Type Conversion Vs Casting
  • Java - Access Protection
  • Java - Implicit Type Conversion
  • Java - Type Casting
  • Java - Call by Value Vs Reference
  • Java - Collections
  • Java - Garbage Collection
  • Java - Scanner Class
  • Java - this Keyword
  • Java - Final Keyword
  • Java - Access Modifiers
  • Java - Design Patterns in Java

OOPS Concepts

  • Java - OOPS Concepts
  • Java - Characteristics of OOP
  • Java - OOPS Benefits
  • Java - Procedural Vs OOP's
  • Java - Polymorphism
  • Java - Encapsulation
  • Java - Multithreading
  • Java - Serialization

Java Operator & Types

  • Java - Operator
  • Java - Logical Operators
  • Java - Conditional Operator
  • Java - Assignment Operator
  • Java - Shift Operators
  • Java - Bitwise Complement Operator

Java Constructor & Types

  • Java - Constructor
  • Java - Copy Constructor
  • Java - String Constructors
  • Java - Parameterized Constructor

Java Array

  • Java - Array
  • Java - Accessing Array Elements
  • Java - ArrayList
  • Java - Passing Arrays to Methods
  • Java - Wrapper Class
  • Java - Singleton Class
  • Java - Access Specifiers
  • Java - Substring

Java Inheritance & Interfaces

  • Java - Inheritance
  • Java - Multilevel Inheritance
  • Java - Single Inheritance
  • Java - Abstract Class
  • Java - Abstraction
  • Java - Interfaces
  • Java - Extending Interfaces
  • Java - Method Overriding
  • Java - Method Overloading
  • Java - Super Keyword
  • Java - Multiple Inheritance

Exception Handling Tutorials

  • Java - Exception Handling
  • Java - Exception-Handling Advantages
  • Java - Final, Finally and Finalize

Data Structures

  • Java - Data Structures
  • Java - Bubble Sort

Advance Java

  • Java - Applet Life Cycle
  • Java - Applet Explaination
  • Java - Thread Model
  • Java - RMI Architecture
  • Java - Applet
  • Java - Swing Features
  • Java - Choice and list Control
  • Java - JFrame with Multiple JPanels
  • Java - Java Adapter Classes
  • Java - AWT Vs Swing
  • Java - Checkbox
  • Java - Byte Stream Classes
  • Java - Character Stream Classes
  • Java - Change Color of Applet
  • Java - Passing Parameters
  • Java - Html Applet Tag
  • Java - JComboBox
  • Java - CardLayout
  • Java - Keyboard Events
  • Java - Applet Run From CLI
  • Java - Applet Update Method
  • Java - Applet Display Methods
  • Java - Event Handling
  • Java - Scrollbar
  • Java - JFrame ContentPane Layout
  • Java - Class Rectangle
  • Java - Event Handling Model

Java programs

  • Java - Armstrong Number
  • Java - Program Structure
  • Java - Java Programs Types
  • Java - Font Class
  • Java - repaint()
  • Java - Thread Priority
  • Java - 1D Array
  • Java - 3x3 Matrix
  • Java - drawline()
  • Java - Prime Number Program
  • Java - Copy Data
  • Java - Calculate Area of Rectangle
  • Java - Strong Number Program
  • Java - Swap Elements of an Array
  • Java - Parameterized Constructor
  • Java - ActionListener
  • Java - Print Number
  • Java - Find Average Program
  • Java - Simple and Compound Interest
  • Java - Area of Rectangle
  • Java - Default Constructor Program
  • Java - Single Inheritance Program
  • Java - Array of Objects
  • Java - Passing 2D Array
  • Java - Compute the Bill
  • Java - BufferedReader Example
  • Java - Sum of First N Number
  • Java - Check Number
  • Java - Sum of Two 3x3 Matrices
  • Java - Calculate Circumference
  • Java - Perfect Number Program
  • Java - Factorial Program
  • Java - Reverse a String

Other Links

  • Java - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW