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

Java OOP परिचय

Object-oriented programming code को real-world चीज़ों की तरह organize करता है, हर एक facts और actions के साथ, जैसे एक नाम वाला एक dog जो bark कर सकता है। यह बड़े programs को neat रखता है।

The OOP Concept

Object-Oriented Programming code को procedures की एक flat sequence के बजाय classes (blueprints) और objects (उन blueprints से बने instances) के आस-पास organize करता है, जो real-world entities को model करना और related data और behavior को साथ bundled रखना आसान बनाता है।

उदाहरण: The OOP Concept

java
class Car {
	String model;
}
public class Main {
	public static void main(String[] args) {
		Car myCar = new Car(); // an object built from the Car class blueprint
		myCar.model = "Sedan";
		System.out.println(myCar.model);
	}
}

Encapsulation

Encapsulation एक class का internal data private fields के पीछे छुपाता है, public getter और setter methods के through सिर्फ controlled access expose करते हुए।

इसका मतलब है class internally अपना data store करने का तरीका बदल सकती है बिना इसके public methods के through इसे उपयोग करने वाले किसी भी code को तोड़े।

उदाहरण: Encapsulation

java
// Define the class Account
class Account {
	private double balance;
	public double getBalance() {
		// Send balance back to the caller
		return balance;
	}
	public void deposit(double amount) {
		balance += amount;
	}
}
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new Account object and store it in acc
		Account acc = new Account();
		acc.deposit(100);
		// Print a line to the console
		System.out.println(acc.getBalance());
	}
}

Inheritance

Inheritance एक class को extends के ज़रिए दूसरी के fields और methods acquire करने देता है, related classes में duplicate code से बचते हुए — एक Vehicle से inherit करने वाली Car class अपने आप Vehicle पहले से define करने वाला सब कुछ पाती है, और सिर्फ cars के लिए specific चीज़ जोड़ने की ज़रूरत है।

उदाहरण: Inheritance

java
class Vehicle {
	void move() {
		System.out.println("Moving");
	}
}
class Car extends Vehicle {
}
public class Main {
	public static void main(String[] args) {
		Car myCar = new Car();
		myCar.move(); // inherited from Vehicle
	}
}

Polymorphism

Polymorphism ('कई forms') एक single method call को इस आधार पर अलग behave करने देता है कि यह runtime पर किस असली object पर invoke हुआ है, इसलिए एक general type (जैसे Animal) के खिलाफ लिखा code किसी भी specific subclass (Dog, Cat) के लिए सही behavior सही से call कर सकता है जो असल में pass किया गया।

उदाहरण: Polymorphism

java
class Animal {
	void sound() {
		System.out.println("Some sound");
	}
}
class Dog extends Animal {
	void sound() {
		System.out.println("Bark");
	}
}
public class Main {
	public static void main(String[] args) {
		Animal a = new Dog();
		a.sound(); // Bark, decided at runtime
	}
}

Abstraction

Abstraction implementation complexity को एक simpler interface के पीछे छुपाता है, abstract classes या interfaces उपयोग करके यह define करते हुए कि एक class को क्या करना चाहिए बिना बिल्कुल यह तय किए कैसे — callers simplified contract के साथ interact करते हैं और underlying details जानने की ज़रूरत नहीं।

उदाहरण: Abstraction

java
// Define the class Shape
abstract class Shape {
	abstract double area();
}
// Define the class Circle
class Circle extends Shape {
	// Declare radius and set it to 2
	double radius = 2;
	double area() {
		// Send Math.PI * radius * radius back to the caller
		return Math.PI * radius * radius;
	}
}
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new Circle object and store it in s
		Shape s = new Circle();
		// Print a line to the console
		System.out.println(s.area());
	}
}
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. एक class को एक object के साथ confuse करना, जब class blueprint है और objects instances हैं।
  2. Encapsulation उपयोग करने के बजाय सभी fields को public बनाना।
  3. Inheritance उपयोग करना जहां composition ज़्यादा fit होता।

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.