Java Object Oriented Programming (OOP) notes provide a comprehensive overview of key concepts such as classes, objects, encapsulation, inheritance, and polymorphism. These notes are ideal for students and professionals looking to strengthen their understanding of OOP principles in Java. The content includes practical examples and code snippets, making it suitable for both beginners and those preparing for exams. Topics covered include method overloading, method overriding, and the importance of constructors in Java programming.

Key Points

  • Explains the four pillars of Object Oriented Programming: class, encapsulation, inheritance, and polymorphism.
  • Includes practical Java code examples demonstrating class and object creation.
  • Covers encapsulation techniques using private data members and getter/setter methods.
  • Describes inheritance in Java with examples of parent and child class relationships.
  • Details method overloading and overriding with code snippets for better understanding.
Manvi Jain
5 pages
Language:Hindi
Type:Study Guide
Manvi Jain
5 pages
Language:Hindi
Type:Study Guide
74
/ 5
🌟 Java Object Oriented Programming (OOP)
OOP kya hota hai? 🤔
OOP ek programming style hai jisme hum real life cheezon ko code ke form mein represent karte hain.
Examples: - Car 🚗 - Student 👨🎓 - Mobile 📱
Java almost pure OOP based language hai.
🔑 OOP ke 4 Pillars (Yaad rakhne layak)
Class & Object
Encapsulation 🔐
Inheritance
Polymorphism 🔄
Ab ek ek karke samajhte hain.
Class & Object 🧱
📦 Class kya hoti hai?
Class ek blueprint / design hoti hai.
Example: - Car ka design = Class
- Asli car = Object
class Student {
String name;
int age;
}
Yahaan: - Student class
- name , age data members
🎯 Object kya hota hai?
Object class ka real instance hota hai.
class Student {
String name;
1
int age;
void display() {
System.out.println(name);
System.out.println(age);
}
public static void main(String[] args) {
Student s1 = new Student(); // object creation
s1.name = "Rahul";
s1.age = 20;
s1.display();
}
}
🧠 Samajhne wali baat:
new Student() memory mein object banta hai
Encapsulation 🔐 (Data Protection)
Encapsulation ka matlab:
Data + Methods ko ek jagah band karna aur direct access se bachana
Use hota hai: - private - Getter & Setter
class BankAccount {
private int balance = 5000;
public int getBalance() {
return balance;
}
public void setBalance(int amt) {
balance = amt;
}
}
Fayde: - Data secure rehta hai 🔒
- Control milta hai ki data kaise use ho
Inheritance (Parent Child)
Inheritance ka matlab:
Ek class doosri class ke features use kare
2
class Animal {
void eat() {
System.out.println("Eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking...");
}
}
class Test {
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
d.bark();
}
}
🧠 Yahaan: - Animal = Parent
- Dog = Child
- extends keyword use hota hai
Polymorphism 🔄 (One thing, many forms)
🔹 Method Overloading (Compile Time)
class MathOp {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
Same method name, different parameters 👍
🔹 Method Overriding (Runtime)
class Parent {
void show() {
System.out.println("Parent class");
3
/ 5
End of Document
74

FAQs

What are the four pillars of Object Oriented Programming in Java?
The four pillars of Object Oriented Programming (OOP) in Java are Class & Object, Encapsulation, Inheritance, and Polymorphism. Class & Object form the foundation where a class serves as a blueprint for creating objects. Encapsulation involves bundling data and methods together while restricting direct access to some of the object's components. Inheritance allows one class to inherit features from another, promoting code reusability. Lastly, Polymorphism enables methods to perform differently based on the object invoking them, which can be achieved through method overloading and overriding.
How does encapsulation enhance data protection in Java?
Encapsulation enhances data protection in Java by restricting direct access to class members and exposing only necessary methods. This is typically achieved using the 'private' access modifier for data members, along with public getter and setter methods. For example, in the 'BankAccount' class, the balance is private, and access is controlled through 'getBalance()' and 'setBalance(int amt)' methods. This ensures that the data remains secure and allows developers to control how the data is accessed and modified.
What is the difference between method overloading and method overriding in Java?
Method overloading and method overriding are two forms of polymorphism in Java. Method overloading occurs at compile time when multiple methods have the same name but different parameters, as seen in the 'MathOp' class with two 'add' methods. In contrast, method overriding takes place at runtime when a subclass provides a specific implementation of a method that is already defined in its superclass, exemplified by the 'Parent' and 'Child' classes where the 'show()' method is overridden. This allows for dynamic method resolution based on the object type.
What role does the 'this' keyword play in Java?
The 'this' keyword in Java is used to refer to the current object within a class. It helps distinguish between class attributes and parameters when they have the same name. For instance, in the 'Test' class constructor, 'this.x = x;' assigns the value of the parameter 'x' to the class attribute 'x'. This usage clarifies which variable is being referenced, ensuring that the correct data is assigned during object instantiation.
Why is Object Oriented Programming important in Java?
Object Oriented Programming (OOP) is important in Java for several reasons. It promotes code reusability, allowing developers to use existing code for new applications, which saves time and effort. OOP also facilitates easier maintenance of code, as changes can be made to individual classes without affecting the entire system. Additionally, OOP models real-life entities, making it easier for developers to conceptualize and design applications. Furthermore, understanding OOP principles is crucial for success in programming interviews and exams.