Hello dear Jetto Net Followers,
Today, I will explain Object Oriented Programming (OOP), one of the most important building blocks of the Java programming language. OOP is a paradigm that is not only unique to Java, but is the foundation of modern software development.
What is OOP?
Object Oriented Programming (OOP) is an approach to designing software based on objects and the relationships between these objects. By modeling real-world objects, it makes software more understandable, organized and reusable.
Basic OOP Concepts
Class: A template that defines the properties (attributes) and behaviors (methods) of an object. For example, a "Car" class can have properties such as color, model, speed, and behaviors such as accelerate, decelerate, stop.ü
Defining an example Car class:
Object: A real instance of a class. Each object has the properties of the class and can use its methods. For example, a red "BMW" model 2023 is a car object.
The following block of code creates a new object called car1 from the Car class and then accesses the variables and methods of the class with this object.
Inheritance: A class inherits properties and behavior from another class. This enables code reuse and allows creating hierarchical structures. For example, the "ElectricCar" class can inherit from the "Car" class and additionally have properties such as battery capacity.
An example inheritance structure:
Kodpublic class SUV extends Car { private int luggageVolume; public SUV(String brand, String model, int luggageVolume) { super(brand, model); this.luggageVolume = luggageVolume; } public void showLuggageVolume() { System.out.println("Luggage Volume: " + luggageVolume + " liter"); } }
In the example above, I defined a class called "SUV" and made it inherit from the "Car" class. The "SUV" class has the properties of the "Car" class (brand and model) and also adds its own property "luggageVolume". The "showLuggageVolume" method prints the trunk volume of the SUV object to the screen.
Polymorphism: It is when an object behaves differently in different situations or when the same method is implemented in different ways by different objects. For example, the "Cat" and "Dog" classes derived from the "Animal" class may both have the "soundOut" method, but the cat meows and the dog barks.
An example of polymorphism:
KodCar car2 = new SUV("Nissan", "X-Trail", 500); car2.showInfo(); // Brand: Nissan, Model: X-Trail ((SUV) car2).showLuggageVolume(); // Trunk Volume: 500 litersCar araba2 = new SUV("Nissan", "X-Trail", 500); car2.showInfo(); // Brand: Nissan, Model: X-Trail ((SUV) car2).showLuggageVolume(); // Trunk Volume: 500 liters
In the example above, I created an object called "car2" derived from the "SUV" class. This object has the properties of both the "Car" and the "SUV" class. Thanks to polymorphism, the "car2" object can be used by both the "showInfo" method and the showLuggageVolume" method.
Abstraction: Hiding the complex details of an object and making only the necessary information available to the user. This makes the code more understandable and manageable. For example, we can use a car without having to know how it works.
Kod
Daha Çok Gösterpublic abstract class Shape{ public abstract double areaCalculate(); public abstract double environmentCalculate(); } public class Rectangle extends Shape { private double length; private double width; public Rectangle(double length, double width) { this.length = length; this.width = width; } public double areaCalculate() { return length * width; } public double environmentCalculate() { return 2 * (length + width); } }
In the example above, we defined an abstract class called "Shape" and derived the "Rectangle" class from this abstract class. The "Shape" class contains abstract methods for calculating area and perimeter, while the "Rectangle" class implements these methods. Users only call the area and perimeter calculations when using the "Rectangle" object.
Encapsulation: To bring together the data and methods of an object to prevent direct access from outside. This ensures the security and integrity of the data. For example, we can use a car engine without having to know how it works.
An example encapsulation structure:
Kod
Daha Çok Gösterpublic class Employee { private String name; private int age public Employee(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
In the example above, I defined a class called "Employee". The class has private properties named "name" and "age". We use public getter and setter methods to access and modify these properties. In this way, we can access and modify the data of the "Employee" object in a controlled manner.
Advantages of OOP
- Code Reuse: Through inheritance, we avoid code duplication by inheriting the properties and behaviors of one class to another class.
- Ease of Maintenance: OOP makes software more modular and organized, which makes it easier to find and fix bugs.
- Flexibility: Thanks to polymorphism, we can make an object behave differently in different situations.
- Scalability: OOP makes large and complex projects easier to manage.
Result
Object Oriented Programming plays an extremely important role in today's software development processes. By understanding the basic principles of object-oriented programming, you can develop higher quality and long-lasting software.
I hope this article has been useful for those who want to get started with OOP. If you have any questions, please let me know in the comments.
Happy coding!