Java Object-Oriented Programming (OOP) Summary
Object-Oriented Programming is a paradigm based on the concept of "objects," which can contain data (fields/attributes) and code (methods). Java is a class-based, object-oriented language.
1. The Four Pillars of OOP
Abstraction is the process of hiding implementation details and showing only the essential features of an object.
Abstract Classes: Can have both abstract methods (no body) and concrete methods.
Interfaces: A blueprint of a class that usually contains only abstract methods (until Java 8).
B. Encapsulation
Encapsulation is the wrapping of data (variables) and code (methods) together as a single unit.
Access Modifiers: private, protected, public, and default.
Data Hiding: Achieved by making variables private and providing public getter and setter methods.
C. Inheritance
Inheritance allows one class (child/subclass) to acquire the properties and behaviors of another (parent/superclass).
Keywords: extends for classes, implements for interfaces.
Benefit: Code reusability.
D. Polymorphism
Polymorphism allows one entity to take many forms.
Compile-time (Static): Method Overloading (same method name, different parameters).
Runtime (Dynamic): Method Overriding (child class provides a specific implementation of a method already defined in the parent class).
2. Core Components
Component
Description
Class
A template or blueprint for creating objects.
Object
An instance of a class that has state and behavior.
Constructor
A special method used to initialize objects. It has no return type and the same name as the class.
this keyword
Refers to the current instance of the class.
super keyword
Refers to the immediate parent class object.
3. Code Example: Putting it all together
// Interface for Abstraction
interface Animal {
void makeSound(); // Abstract method
}
// Parent Class
class Mammal {
private String category = "Land Animal"; // Encapsulation
public String getCategory() { return category; }
}
// Child Class with Inheritance and Polymorphism
class Dog extends Mammal implements Animal {
private String name;
public Dog(String name) {
this.name = name;
}
// Method Overriding (Polymorphism)
@Override
public void makeSound() {
System.out.println(name + " says: Woof! Woof!");
}
// Method Overloading (Polymorphism)
public void play(String toy) {
System.out.println(name + " plays with " + toy);
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog("Buddy");
myDog.makeSound();
System.out.println("Category: " + myDog.getCategory());
}
}
4. Key Differences: Interface vs. Abstract Class
Feature
Abstract Class
Interface
Methods
Can have abstract & concrete methods.
Mostly abstract (can have default/static).
Variables
Can have final, non-final, static, non-static.
Only static and final (constants).
Inheritance
A class can extend only one abstract class.
A class can implement multiple interfaces.
Purpose
To share code among closely related objects.
To define a contract for what a class can do.
No comments:
Post a Comment