Mostly Asked Java OOP Interview Questions | LiveCodeProgramming

Mostly Asked Java OOP Interview Questions

Q1. What are the four main principles of OOP?What are the four main principles of OOP?

Encapsulation: Wrapping data and methods into a single unit (class), restricting direct access to some components.
Inheritance: Mechanism where one class acquires the properties and behaviors of another class.
Polymorphism: Ability of an object to take many forms, typically via method overriding or overloading.
Abstraction: Hiding internal details and showing only functionalities.

Encapsulation (एन्कैप्सुलेशन): डेटा और मेथड्स को एक यूनिट (क्लास) में पैक करना, और कुछ हिस्सों को डायरेक्ट एक्सेस से रोकना।
Inheritance (इनहेरिटेंस): एक क्लास दूसरे क्लास की प्रॉपर्टीज़ और बिहेवियर प्राप्त करती है।
Polymorphism (पॉलीमॉर्फिज़्म): किसी ऑब्जेक्ट की कई रूपों में उपस्थिति, आमतौर पर मेथड ओवरराइडिंग या ओवरलोडिंग के द्वारा।
Abstraction (एब्स्ट्रैक्शन): आंतरिक विवरणों को छुपाकर केवल आवश्यक कार्यक्षमता दिखाना।

Q2. What is encapsulation in Java?What is encapsulation in Java?

Encapsulation is the technique of wrapping data (variables) and code (methods) together as a single unit (class). It protects data by making variables private and providing public getter/setter methods to access and update the data.

Encapsulation एक तकनीक है जिसमें डेटा (वेरिएबल्स) और कोड (मेथड्स) को एक यूनिट (क्लास) में बांधा जाता है। यह डेटा को प्रोटेक्ट करता है वेरिएबल्स को private बनाकर और public getter/setter मेथड्स देता है ताकि डेटा को एक्सेस और अपडेट किया जा सके।

public class Employee {
  private String name;
  private int id;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }
}
Q3. Explain inheritance with example.Explain inheritance with example.

Inheritance allows a class (child/subclass) to inherit fields and methods from another class (parent/superclass), promoting code reuse.

Inheritance से एक क्लास (चाइल्ड/सबक्लास) दूसरे क्लास (पेरेंट/सुपरक्लास) के फील्ड्स और मेथड्स प्राप्त कर सकती है, जिससे कोड रिपीटेशन कम होता है।

class Employee {
  String name;
  void display() {
    System.out.println("Name: " + name);
  }
}
class Manager extends Employee {
  int deptId;
  void displayDept() {
    System.out.println("Dept ID: " + deptId);
  }
}
Q4. What is method overloading and overriding?What is method overloading and overriding?

Method Overloading: Same method name with different parameters in the same class.
Method Overriding: Subclass provides a specific implementation of a method already defined in its superclass.

Method Overloading: एक ही मेथड नाम, लेकिन पैरामीटर अलग, एक ही क्लास में।
Method Overriding: सबक्लास में सुपरक्लास के मेथड को नई तरह से परिभाषित करना।

public class Employee {
  void setDetails(String name) {}
  void setDetails(String name, int id) {}
}

class Manager extends Employee {
  @Override
  void setDetails(String name) {
    // overridden method
  }
}
Q5. What is polymorphism?What is polymorphism?

Polymorphism allows objects to be treated as instances of their parent class rather than their actual class. The two types:
1. Compile-time polymorphism (method overloading)
2. Runtime polymorphism (method overriding)

Polymorphism से ऑब्जेक्ट्स को उनके असली क्लास की बजाय उनके पेरेंट क्लास के रूप में ट्रीट किया जा सकता है। दो प्रकार हैं:
1. Compile-time polymorphism (मेथड ओवरलोडिंग)
2. Runtime polymorphism (मेथड ओवरराइडिंग)

Employee e = new Manager();
e.display();  // calls Manager's display if overridden
Q6. What is abstraction?What is abstraction?

Abstraction is hiding the complex implementation details and showing only the necessary features of an object. Achieved using abstract classes and interfaces.

Abstraction जटिल इम्प्लीमेंटेशन को छुपाकर केवल आवश्यक फीचर्स दिखाने की प्रक्रिया है। इसे abstract क्लास और interfaces से हासिल किया जाता है।

abstract class Employee {
  abstract void work();
}

class Manager extends Employee {
  void work() {
    System.out.println("Managing tasks");
  }
}
Q7. Difference between abstract class and interface?Difference between abstract class and interface?

Abstract class can have both abstract and concrete methods; interfaces (Java 7 and before) only abstract methods. Since Java 8, interfaces can have default and static methods.
Class can extend only one abstract class but implement multiple interfaces.

Abstract class में abstract और concrete दोनों मेथड हो सकते हैं; interface (Java 7 तक) में सिर्फ abstract मेथड। Java 8 से interfaces में default और static मेथड भी हो सकते हैं।
क्लास एक ही abstract class को एक्सटेंड कर सकती है, लेकिन कई interfaces को इम्प्लीमेंट कर सकती है।

abstract class Employee {
  abstract void work();
}
interface Payable {
  void calculateSalary();
}
class Manager extends Employee implements Payable {
  void work() {}
  public void calculateSalary() {}
}
Q8. What is the use of the 'this' keyword?What is the use of the 'this' keyword?

'this' refers to the current object instance. It is used to resolve naming conflicts between instance variables and parameters or to pass the current object as a parameter.

'this' वर्तमान ऑब्जेक्ट की तरफ इशारा करता है। इसका उपयोग instance variables और parameters के नाम संघर्ष को हल करने या वर्तमान ऑब्जेक्ट को पैरामीटर के रूप में पास करने के लिए किया जाता है।

public class Employee {
  String name;
  Employee(String name) {
    this.name = name;
  }
}
Q9. Explain final keyword in Java.Explain final keyword in Java.

'final' keyword is used to declare constants, prevent method overriding, and inheritance of classes.

  • final variable: value cannot be changed
  • final method: cannot be overridden
  • final class: cannot be subclassed

'final' कीवर्ड का उपयोग कॉन्स्टैंट्स घोषित करने, मेथड ओवरराइडिंग रोकने, और क्लास के इनहेरिटेंस को रोकने के लिए किया जाता है।

  • final variable: वैल्यू बदली नहीं जा सकती
  • final method: ओवरराइड नहीं हो सकती
  • final class: सबक्लास नहीं बनाई जा सकती

final class Employee {
  final int id = 100;
  final void display() {
    System.out.println("ID: " + id);
  }
}