This is part 6 of the Java programming series. Earlier parts: Part 2.
Object-Oriented Programming in Java
Java is fundamentally an object-oriented language. The four pillars of OOP are:
1. Encapsulation
Encapsulation means bundling data (fields) and the methods that operate on that data within a single class, and restricting direct access to some of the object's components. In Java, this is done using access modifiers (private, public, protected).
2. Inheritance
Inheritance allows a new class to reuse the properties and methods of an existing class. In Java, the extends keyword is used:
class Animal {
void eat() { System.out.println("Eating..."); }
}
class Dog extends Animal {
void bark() { System.out.println("Barking..."); }
}
3. Polymorphism
Polymorphism allows objects to be treated as instances of their parent class. Method overriding and method overloading are two forms of polymorphism in Java.
4. Abstraction
Abstraction hides implementation details and shows only essential features. In Java, this is achieved using abstract classes and interfaces.
See also: Wrapper Classes | Java Data Types