|
| 1 | +## Basics of Object-Oriented Programming (OOPS) |
| 2 | + |
| 3 | +Object-Oriented Programming (OOP) is a fundamental programming paradigm used in software development, defined by its use of classes and objects. |
| 4 | + |
| 5 | +It’s built on four main principles: Inheritance, Polymorphism, Abstraction, and Encapsulation. |
| 6 | + |
| 7 | +These principles not only help in creating structured and reusable code but also make it easier to understand, maintain, and modify. |
| 8 | + |
| 9 | +### Inheritance |
| 10 | +nheritance allows one class to inherit the properties and methods of another class. It's a way to form a hierarchy between classes, promoting code reusability. |
| 11 | +#### Example: |
| 12 | +``` |
| 13 | +class Vehicle { |
| 14 | + public void startEngine() { |
| 15 | + System.out.println("Engine started"); |
| 16 | + } |
| 17 | +} |
| 18 | +
|
| 19 | +class Car extends Vehicle { |
| 20 | + public void openTrunk() { |
| 21 | + System.out.println("Trunk opened"); |
| 22 | + } |
| 23 | +} |
| 24 | +
|
| 25 | +public class Main { |
| 26 | + public static void main(String[] args) { |
| 27 | + Car myCar = new Car(); |
| 28 | + myCar.startEngine(); // Inherited method |
| 29 | + myCar.openTrunk(); // Own method |
| 30 | + } |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +In this Java example, `Car` inherits from `Vehicle`. |
| 35 | + |
| 36 | +Car can use the startEngine method from Vehicle, demonstrating inheritance. |
| 37 | + |
| 38 | +### Polymorphism |
| 39 | +Polymorphism allows objects of different classes to be treated as objects of a common superclass. It’s the ability of multiple object types to implement the same functionality, which can be achieved either by method overloading or method overriding. |
| 40 | + |
| 41 | +#### Example: |
| 42 | +``` |
| 43 | +class Bird { |
| 44 | + public void sing() { |
| 45 | + System.out.println("Bird is singing"); |
| 46 | + } |
| 47 | +} |
| 48 | +
|
| 49 | +class Sparrow extends Bird { |
| 50 | + public void sing() { |
| 51 | + System.out.println("Sparrow is singing"); |
| 52 | + } |
| 53 | +} |
| 54 | +
|
| 55 | +public class Main { |
| 56 | + public static void main(String[] args) { |
| 57 | + Bird myBird = new Sparrow(); |
| 58 | + myBird.sing(); // Outputs: Sparrow is singing |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | +Here, `Sparrow` overrides the sing method of `Bird`. Despite referring to Sparrow with a Bird reference, the overridden method in Sparrow is called. |
| 63 | + |
| 64 | +### Abstraction |
| 65 | +Abstraction is the concept of hiding complex implementation details and showing only the necessary features of an object. It can be achieved using abstract classes and interfaces. |
| 66 | + |
| 67 | +#### Example: |
| 68 | +``` |
| 69 | +abstract class Animal { |
| 70 | + abstract void makeSound(); |
| 71 | +
|
| 72 | + public void eat() { |
| 73 | + System.out.println("Animal is eating"); |
| 74 | + } |
| 75 | +} |
| 76 | +
|
| 77 | +class Dog extends Animal { |
| 78 | + public void makeSound() { |
| 79 | + System.out.println("Bark"); |
| 80 | + } |
| 81 | +} |
| 82 | +
|
| 83 | +public class Main { |
| 84 | + public static void main(String[] args) { |
| 85 | + Animal myDog = new Dog(); |
| 86 | + myDog.makeSound(); // Outputs: Bark |
| 87 | + myDog.eat(); // Inherited method |
| 88 | + } |
| 89 | +} |
| 90 | +``` |
| 91 | +`Animal` is an abstract class that provides a method `makeSound()`. |
| 92 | + |
| 93 | +`Dog` provides the specific implementation of this method. |
| 94 | + |
| 95 | +### Encapsulation |
| 96 | +Encapsulation is the technique of bundling data (variables) and methods that act on the data into a single unit, often called a class, and restricting access to some of the object’s components. |
| 97 | + |
| 98 | +``` |
| 99 | +class BankAccount { |
| 100 | + private double balance; |
| 101 | +
|
| 102 | + public void deposit(double amount) { |
| 103 | + if (amount > 0) { |
| 104 | + balance += amount; |
| 105 | + } |
| 106 | + } |
| 107 | +
|
| 108 | + public void withdraw(double amount) { |
| 109 | + if (amount <= balance) { |
| 110 | + balance -= amount; |
| 111 | + } |
| 112 | + } |
| 113 | +
|
| 114 | + public double getBalance() { |
| 115 | + return balance; |
| 116 | + } |
| 117 | +} |
| 118 | +
|
| 119 | +public class Main { |
| 120 | + public static void main(String[] args) { |
| 121 | + BankAccount account = new BankAccount(); |
| 122 | + account.deposit(1000); |
| 123 | + account.withdraw(500); |
| 124 | + System.out.println("Balance: " + account.getBalance()); |
| 125 | + } |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +In this example, the balance of the `BankAccount` is kept private. It can only be modified through the `deposit` and `withdraw` methods and read through the `getBalance` method, showcasing encapsulation. |
0 commit comments