Untitled Document
Untitled Document
subtopics in a structured format. You can directly use this for your competitive exam
preparation.
Example:
java
Copy code
class Car {
String model;
int year;
Car(String model, int year) {
this.model = model;
this.year = year;
}
}
●
2. Encapsulation
● Definition: The process of bundling data (fields) and methods that operate on the data
into a single unit (class) while restricting direct access to some components using private
access modifiers.
Purpose: Protects the integrity of an object's state and provides controlled access through
public methods (getters and setters).
Example:
java
Copy code
class BankAccount {
private double balance;
3. Inheritance
● Definition: Allows a class (child/subclass) to inherit fields and methods from another
class (parent/superclass).
● Why use it? It promotes code reuse and establishes a natural hierarchy.
● Types of Inheritance:
1. Single Inheritance: One class inherits from one parent class.
2. Multilevel Inheritance: A class inherits from a class, which also inherits from
another class.
3. Hierarchical Inheritance: Multiple classes inherit from the same parent class.
4. Hybrid Inheritance: Combination of different types of inheritance, achieved
using interfaces (Java doesn't support multiple inheritance directly).
Example:
java
Copy code
class Animal {
void eat() {
System.out.println("This animal eats");
}
}
4. Polymorphism
● Definition: Polymorphism allows one entity (method or object) to take many forms.
● Types of Polymorphism:
1. Compile-time Polymorphism (Method Overloading):
■ Same method name but different parameters.
■ Enhances flexibility and code readability.
Example:
java
Copy code
class MathOperations {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}
2.
3. Runtime Polymorphism (Method Overriding):
■ A child class provides its own implementation of a method defined in its
parent class.
■ Achieved through inheritance.
Example:
java
Copy code
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
4.
5. Abstraction
● Definition: Hides implementation details and shows only necessary features.
● Achieved through:
1. Abstract Classes: Cannot be instantiated; can have both abstract and concrete
methods.
2. Interfaces: All methods are abstract by default. Allows multiple inheritance.
●
7. Final Keyword
● Final Variable: Cannot be reassigned (constant).
● Final Method: Cannot be overridden by subclasses.
8. This Keyword
● Refers to the current object within a method or constructor.
Used to distinguish between instance variables and method parameters with the same name.
Example:
java
Copy code
class Car {
String model;
Car(String model) {
this.model = model; // 'this' refers to instance variable
}
}
9. Super Keyword
● Refers to the parent class object.
void printColor() {
System.out.println(color); // prints Black
System.out.println(super.color); // prints White
}
}
Aggregation (Has-a relationship): One class contains another, but both can exist
independently (weak association).
Example:
java
Copy code
class Car {
Engine engine; // Car has an Engine
}
Composition: Strong form of aggregation where one class owns another and both are
dependent. If the parent is destroyed, so is the child.
Example:
java
Copy code
class House {
Room room; // House owns Room
}
These notes should cover all the essential OOP concepts in Java that you need for your
competitive exam. They include clear explanations of each topic, with examples to help you
understand the concepts. Let me know if you need any further clarification or adjustments!