Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
14 views3 pages

Java Opps and Methods

Uploaded by

phonesjunction
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Java Opps and Methods

Uploaded by

phonesjunction
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of

"objects," which can contain data and code that manipulates the data. OOP
principles are designed to help organize and structure code in a more manageable
and scalable way.

### Four Pillars of OOP

1. **Encapsulation**
- **Definition**: Encapsulation is the bundling of data (attributes) and methods
(functions) that operate on the data into a single unit or class. It also involves
controlling access to the data by using access modifiers.
- **Example**: In a `Car` class, the `speed` attribute and methods like
`accelerate()` and `brake()` are encapsulated together.

2. **Inheritance**
- **Definition**: Inheritance is the mechanism by which one class (subclass or
derived class) inherits the attributes and methods from another class (superclass
or base class). It promotes code reuse.
- **Example**: A `SportsCar` class can inherit from the `Car` class, gaining its
attributes and methods while adding its own unique features.

3. **Polymorphism**
- **Definition**: Polymorphism allows methods to do different things based on
the object it is acting upon, even if they share the same name. This can be
achieved through method overloading and method overriding.
- **Example**: The method `draw()` might have different implementations in
different classes like `Circle` and `Rectangle`, but can be called in a unified
way.

4. **Abstraction**
- **Definition**: Abstraction involves hiding the complex implementation details
and showing only the essential features of an object. It simplifies interaction
with objects by exposing only necessary information.
- **Example**: An abstract class `Shape` with an abstract method `draw()` that
must be implemented by any concrete subclass.

### Method Overloading

**Method Overloading** allows multiple methods with the same name but different
parameters (different type, number, or both) in the same class. This is a way to
achieve compile-time polymorphism.

**Real-Time Example**: A `Calculator` class that can perform addition with


different numbers of parameters.

```java
public class Calculator {
// Method to add two integers
public int add(int a, int b) {
return a + b;
}

// Overloaded method to add three integers


public int add(int a, int b, int c) {
return a + b + c;
}

// Overloaded method to add two doubles


public double add(double a, double b) {
return a + b;
}

public static void main(String[] args) {


Calculator calc = new Calculator();
System.out.println("Sum of 5 and 10: " + calc.add(5, 10)); // Calls the
first method
System.out.println("Sum of 1, 2, and 3: " + calc.add(1, 2, 3)); // Calls
the second method
System.out.println("Sum of 5.5 and 3.3: " + calc.add(5.5, 3.3)); // Calls
the third method
}
}
```

### Method Overriding

**Method Overriding** occurs when a subclass provides a specific implementation of


a method that is already defined in its superclass. This is a way to achieve
runtime polymorphism.

**Real-Time Example**: A `Vehicle` class with a method `start()`, and a `Car` class
that overrides this method to provide its own implementation.

```java
// Superclass
class Vehicle {
// Method to be overridden
public void start() {
System.out.println("Vehicle is starting...");
}
}

// Subclass
class Car extends Vehicle {
// Overriding method
@Override
public void start() {
System.out.println("Car is starting with a roar...");
}

public static void main(String[] args) {


Vehicle myVehicle = new Vehicle();
Vehicle myCar = new Car();

myVehicle.start(); // Calls the start() method of Vehicle class


myCar.start(); // Calls the overridden start() method of Car class
}
}
```

### Summary

- **Encapsulation**: Bundles data and methods together and controls access.


- **Inheritance**: Allows one class to inherit from another, promoting code reuse.
- **Polymorphism**: Allows methods to act differently based on the object or
parameters.
- **Abstraction**: Hides complex implementation details and exposes only necessary
aspects.
- **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.

These OOP principles help to design and implement code that is more modular,
reusable, and easier to maintain.

You might also like