Java does not have an official list of five principles. When people ask this question, they usually mean one of two things: the five SOLID principles of object-oriented design, or the core concepts of object-oriented programming. Both ideas are language-neutral, so they apply to Java, C++ and other object-oriented languages.
The five SOLID principles
SOLID is named for five design principles: single responsibility, open/closed, Liskov substitution, interface segregation and dependency inversion. They are often discussed in Java courses, but they are not part of the Java language itself. They are guidelines for how to divide classes and connect them. For a short overview with a C++ example for each one, see solid principles in c.
The four core OOP concepts
Many Java tutorials list four concepts:
- Encapsulation: Keeping data hidden inside a class and exposing it through controlled functions.
- Inheritance: Creating a new class from an existing one, so it can reuse and extend the existing behavior.
- Polymorphism: Using one interface to work with objects of different types.
- Abstraction: Showing only the essential features of an object while hiding the details.
Some sources use different names for these ideas or add more items, so the exact list depends on the source.
These concepts map directly to C++. C++ has access specifiers (private, protected and public), derived classes, virtual functions and abstract classes. Here is encapsulation in a small C++ class:
class BankAccount {
public:
void deposit(double amount) {
if (amount > 0) {
balance_ += amount;
}
}
double balance() const { return balance_; }
private:
double balance_ = 0.0;
};
Callers cannot change balance_ directly. They must use deposit, which can check its input first. Access specifiers are covered in access modifiers in c, and virtual functions are covered in polymorphism in c.
Where Java and C++ differ
The concepts carry over, but the two languages differ in syntax and in how memory is managed. If you are comparing them, difference between c and java covers those differences in more detail.
Reading a source that claims five Java principles
When a tutorial or article says Java has five principles, check which list it means. If it lists the five SOLID names, the source is talking about general object-oriented design. If it lists encapsulation, inheritance, polymorphism and abstraction, it is talking about core OOP concepts. If it gives a different list, look for where it defines the terms.