Object-Oriented Programming Concepts in Java
Define Class and Objects:
- A class is a blueprint or prototype for creating objects. It defines properties and behaviors using
fields (variables) and methods.
- An object is an instance of a class, representing a specific entity with state and behavior.
Four Pillars of Object-Oriented Programming System:
1. **Encapsulation**: Wrapping data (variables) and methods into a single unit called a class.
Access to the data is controlled using access modifiers.
2. **Inheritance**: Mechanism by which one class acquires properties and behaviors of another
class, promoting code reuse.
3. **Polymorphism**: Ability of a single entity to take multiple forms. It includes method overloading
(compile-time polymorphism) and method overriding (runtime polymorphism).
4. **Abstraction**: Hiding implementation details and showing only essential features of an object.
Constructor and Types of Constructors:
- A constructor is a special method used to initialize objects. It has the same name as the class and
does not have a return type.
Types of Constructors:
1. **Default Constructor**: A constructor with no parameters, provided by Java if no constructors are
explicitly defined.
Example: `public ClassName() {}`
2. **Parameterized Constructor**: A constructor with parameters to initialize object properties.
Example: `public ClassName(int x) { this.x = x; }`
3. **Copy Constructor**: A constructor that initializes an object using another object of the same
class.
Method Overloading:
- Method overloading is the ability to define multiple methods with the same name but different
parameter lists within the same class.
Ways to Overload Methods in Java:
1. By changing the number of parameters.
Example:
`void add(int a, int b) {}`
`void add(int a, int b, int c) {}`
2. By changing the data type of parameters.
Example:
`void add(int a, int b) {}`
`void add(double a, double b) {}`
3. By changing the sequence of parameters.
Example:
`void print(String s, int n) {}`
`void print(int n, String s) {}`