Main Features of OOP (Object-Oriented Programming):
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
- Method Overriding
- Method Overloading
- Classes and Objects
1. Encapsulation
Encapsulation means binding the data and methods that operate on that data into a single unit, and restricting outside access to it.
<code>javaCopyEdit<code>class Person {
private String name; // private variable
// Getter method
public String getName() {
return name;
}
// Setter method
public void setName(String name) {
this.name = name;
}
}
public class Main {
public static void main(String[] args) {
Person person = new Person();
person.setName("John");
System.out.println(person.getName()); // Output: John
}
}
2. Abstraction
Abstraction is the process of hiding implementation details and showing only the essential features.
javaCopyEditabstract class Animal {
abstract void makeSound(); // Abstract method
}
class Dog extends Animal {
void makeSound() {
System.out.println("Bark");
}
}
public class Main {
public static void main(String[] args) {
Animal dog = new Dog();
dog.makeSound(); // Output: Bark
}
}
3. Inheritance
Inheritance allows a class (child) to acquire properties and behavior (methods) of another class (parent).
javaCopyEditclass Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Output: This animal eats food.
dog.bark(); // Output: The dog barks.
}
}
4. Polymorphism
Polymorphism allows one interface to be used for a general class of actions.
javaCopyEditclass Animal {
void makeSound() {
System.out.println("Some sound");
}
}
class Dog extends Animal {
void makeSound() {
System.out.println("Bark");
}
}
class Cat extends Animal {
void makeSound() {
System.out.println("Meow");
}
}
public class Main {
public static void main(String[] args) {
Animal myDog = new Dog();
Animal myCat = new Cat();
myDog.makeSound(); // Output: Bark
myCat.makeSound(); // Output: Meow
}
}
5. Method Overriding
Method overriding means redefining a method in the subclass that already exists in the parent class.
javaCopyEditclass Animal {
void makeSound() {
System.out.println("Some sound");
}
}
class Dog extends Animal {
@Override
void makeSound() {
System.out.println("Bark");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Dog();
animal.makeSound(); // Output: Bark
}
}
6. Method Overloading
Method overloading allows multiple methods with the same name but different parameters in a class.
javaCopyEditclass MathOperations {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
public class Main {
public static void main(String[] args) {
MathOperations math = new MathOperations();
System.out.println(math.add(2, 3)); // Output: 5
System.out.println(math.add(2, 3, 4)); // Output: 9
}
}
7. Classes and Objects
A class is a blueprint for objects. An object is an instance of a class.
javaCopyEditclass Car {
String model;
int year;
void displayInfo() {
System.out.println("Model: " + model + ", Year: " + year);
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.model = "Toyota";
car.year = 2020;
car.displayInfo(); // Output: Model: Toyota, Year: 2020
}
}