← Back to Core Java Course | Chapter 7: OOP Core | Lesson 11 of 11

Java की Object Class

Object हर Java class का ancestor है, इसलिए हर object को कुछ basic abilities मिलती हैं जैसे खुद को describe करना और खुद को compare करना। यह हर कोई share करने वाले एक family trait जैसा है।
Syntax
java
@Override
public String toString() { ... }
@Override
public boolean equals(Object obj) { ... }
@Override
public int hashCode() { ... }

What is the Object Class?

Java में हर class implicitly Object extend करती है अगर यह explicitly कुछ और extend नहीं करती, यही कारण है कि हर object में toString(), equals(), और hashCode() जैसे methods अपने आप available हैं भले ही आपने कभी उन्हें खुद नहीं लिखा।

उदाहरण: What is the Object Class?

java
class Dog {}
public class Main {
	public static void main(String[] args) {
		Dog dog = new Dog();
		System.out.println(dog.toString()); // inherited from Object
	}
}

The toString() Method

Object से inherited default toString() class name plus एक hash code print करता है, जो debugging के लिए शायद ही कभी उपयोगी है — meaningful field values return करने के लिए इसे override करना आपकी अपनी classes पर आप करने वाले सबसे आम customizations में से एक है।

उदाहरण: The toString() Method

java
// Define the class Dog
class Dog {
	// Declare name and set it to "Rex"
	String name = "Rex";
	// Override the method inherited from the parent type
	@Override
	public String toString() {
		// Send "Dog: " + name back to the caller
		return "Dog: " + name;
	}
}
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println(new Dog());
	}
}

The equals() Method

Object से default equals() बस memory references compare करता है (== के identical), इसलिए अलग addresses वाली दो logically-equal objects unequal मानी जाएंगी जब तक आप असल field values compare करने के लिए equals() override न करें।

उदाहरण: The equals() Method

java
// Define the class Point
class Point {
	int x;
	Point(int x) { this.x = x; }
	// Override the method inherited from the parent type
	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof Point)) return false;
		// Send ((Point) obj).x == this.x back to the caller
		return ((Point) obj).x == this.x;
	}
}
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new Point object and store it in p1
		Point p1 = new Point(5);
		// Create a new Point object and store it in p2
		Point p2 = new Point(5);
		// Print a line to the console
		System.out.println(p1.equals(p2));
	}
}

The hashCode() Method

hashCode() एक integer return करता है जो HashMap और HashSet जैसी hash-based collections objects को efficiently bucket करने के लिए उपयोग करती हैं; Java का contract मांगता है कि equals() से equal माने गए दो objects को वही hash code return करना चाहिए, नहीं तो वे collections गलत behave करेंगी।

उदाहरण: The hashCode() Method

java
// Define the class Point
class Point {
	int x;
	Point(int x) { this.x = x; }
	// Override the method inherited from the parent type
	@Override
	public int hashCode() {
		// Send Integer.hashCode(x) back to the caller
		return Integer.hashCode(x);
	}
}
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println(new Point(5).hashCode());
	}
}

The getClass() Method

getClass() एक instance के असली type को describe करता runtime Class object return करता है, जो reflection के लिए, concrete class name log करने के लिए, या अकेले instanceof checks काफी precise न होने पर subclasses के बीच distinguish करने के लिए उपयोगी है।

उदाहरण: The getClass() Method

java
// Define the class Dog
class Dog {}
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new Dog object and store it in dog
		Dog dog = new Dog();
		// Print a line to the console
		System.out.println(dog.getClass().getName());
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. hashCode() के बिना equals() override करना, जो HashSet और HashMap को तोड़ता है।
  2. equals() चाहिए होने पर == उपयोग करना।
  3. toString() override न करना, इसलिए एक object print करना Person@1b6d3586 जैसा कुछ दिखाता है।
चैप्टर सारांश
  • Classes blueprints हैं और objects instances हैं, constructors के साथ created और initialized, this current object को refer करते हुए।
  • Access modifiers, non-access modifiers, और getters और setters visibility control करते हैं और data encapsulate करते हैं।
  • static, final, instanceof, और Object class round out करते हैं कि objects कैसे behave करते हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.