Java OOP परिचय
In this page:
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
Encapsulation
Encapsulation एक class का internal data private fields के पीछे छुपाता है, public getter और setter methods के through सिर्फ controlled access expose करते हुए।
इसका मतलब है class internally अपना data store करने का तरीका बदल सकती है बिना इसके public methods के through इसे उपयोग करने वाले किसी भी code को तोड़े।
उदाहरण: Encapsulation
// 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());
}
}
Login to try C/C++/Java/PHP code in the editor
Inheritance
Inheritance एक class को extends के ज़रिए दूसरी के fields और methods acquire करने देता है, related classes में duplicate code से बचते हुए — एक Vehicle से inherit करने वाली Car class अपने आप Vehicle पहले से define करने वाला सब कुछ पाती है, और सिर्फ cars के लिए specific चीज़ जोड़ने की ज़रूरत है।
उदाहरण: Inheritance
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
}
}
Login to try C/C++/Java/PHP code in the editor
Polymorphism
Polymorphism ('कई forms') एक single method call को इस आधार पर अलग behave करने देता है कि यह runtime पर किस असली object पर invoke हुआ है, इसलिए एक general type (जैसे Animal) के खिलाफ लिखा code किसी भी specific subclass (Dog, Cat) के लिए सही behavior सही से call कर सकता है जो असल में pass किया गया।
उदाहरण: Polymorphism
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
}
}
Login to try C/C++/Java/PHP code in the editor
Abstraction
Abstraction implementation complexity को एक simpler interface के पीछे छुपाता है, abstract classes या interfaces उपयोग करके यह define करते हुए कि एक class को क्या करना चाहिए बिना बिल्कुल यह तय किए कैसे — callers simplified contract के साथ interact करते हैं और underlying details जानने की ज़रूरत नहीं।
उदाहरण: Abstraction
// 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());
}
}
Login to try C/C++/Java/PHP code in the editor
- एक class को एक object के साथ confuse करना, जब class blueprint है और objects instances हैं।
- Encapsulation उपयोग करने के बजाय सभी fields को public बनाना।
- Inheritance उपयोग करना जहां composition ज़्यादा fit होता।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: