Real Java Programming
Real Java programming is not just about writing a calculator or printing something to the console. It is about building large applications that follow clear OOP principles.
This article marks the beginning of our section “Java Programming: OOP”, where we will introduce the basic concepts of Object-Oriented Programming, explaining what it is and why it is important.
What is OOP?
OOP (Object-Oriented Programming) is a programming paradigm where we don’t just write functions and variables, but we define objects (classes) that behave like real-world entities—having properties (attributes) and actions (methods).
Why is OOP useful and important?
✅ 1. Reusing Templates
We create a class once (like a blueprint) and then generate hundreds of similar objects with different characteristics.
Example:
Dog dog1 = new Dog();
dog1.name = "Rex";
Dog dog2 = new Dog();
dog2.name = "Sharik";
Here, the
name
variable was defined once inside theDog
class, and every object of this class has it. We simply assign a unique name to each individual dog.
Although usingthis
is a better practice for this, we will discuss it later.
✅ 2. Classes Can Work Together
One class can interact with another, allowing us to build complex systems from simple components.
✅ 3. OOP Keeps Code Organized
Without OOP, large projects quickly become messy.
OOP allows us to divide code into clean, independent blocks.
✅ 4. Polymorphism Makes Code Flexible
The same method can behave differently in different classes, making applications more flexible and extendable.
✅ 5. Saves Time and Effort
When we modify a class, all objects based on it automatically update—no need to rewrite code.
✅ 6. Improves Readability
OOP makes structured, understandable code that is easy for teams to work on.
The Four Main Principles of OOP
- Encapsulation – Hiding details and exposing only necessary parts.
- Inheritance – Creating new classes based on existing ones.
- Polymorphism – Using a single interface for different implementations.
- Abstraction – Focusing only on essential characteristics.
Example Code with Explanations
// Dog class
class Dog {
String name; // Dog's name
int age; // Dog's age
void bark() { // Method for barking
System.out.println(name + " says Woof!");
}
}
// Dog Trainer class
class DogTrainer {
// Method to "train" a dog (works with Dog objects)
void trainDog(Dog dog) {
System.out.println("Training dog: " + dog.name);
dog.bark(); // Trainer calls bark method
}
}
public class Main {
public static void main(String[] args) {
Dog rex = new Dog(); // Creating dog Rex
rex.name = "Rex";
rex.age = 2;
Dog luna = new Dog(); // Creating dog Luna
luna.name = "Luna";
luna.age = 3;
DogTrainer trainer = new DogTrainer(); // Creating a trainer
trainer.trainDog(rex); // Passing dog Rex to trainDog method
trainer.trainDog(luna); // Passing dog Luna
}
}
Explanation:
- We define a
Dog
class with attributes (name
,age
) and a methodbark()
, which prints a message. - The
DogTrainer
class contains a methodtrainDog(Dog dog)
, which takes a Dog object and calls itsbark()
method. - In the
main
method:- We create two Dog objects with different names.
- We create one DogTrainer object and call
trainDog()
for each dog.
Polymorphism in This Example
While this example does not yet fully implement polymorphism (since we are not using inheritance or interfaces), the idea is that the trainDog()
method can work with any object of type Dog
or its future subclasses.
For example, if we later add a GuardDog
subclass, the trainer could still train it, and it could have its own version of the bark()
method.
This is the essence of polymorphism—one method, but different behaviors depending on the object.
Key Takeaways
- OOP helps organize and scale projects efficiently.
- Instead of writing repetitive code, we use templates (classes) to create multiple objects.
- OOP allows us to build complex applications from simple components.
- It makes code easier to maintain, extend, and understand.
In one of the next lessons, we will learn how to properly use constructors, apply this
, and write cleaner, more professional Java code.