Java OOP
Java - What is OOP?
OOP stands for Object-Oriented Programming.
Procedural programming is about writing procedures or methods that perform operations on the data, while object-oriented programming is about creating objects that contain both data and methods.
Object-oriented programming has several advantages over procedural programming:
- OOP is faster and easier to execute.
- OOP provides a clear structure for the programs.
- OOP helps to keep the Java code DRY (Don't Repeat Yourself), making the code easier to maintain, modify, and debug.
- OOP makes it possible to create fully reusable applications with less code and shorter development time.
Tip: The Don't Repeat Yourself (DRY) principle is about reducing the repetition of code. You should extract out the common code in the application, place it in a single location, and reuse it instead of repeating it.
Java - What are Classes and Objects?
Classes and objects are the two main aspects of object-oriented programming.
Look at the following illustration to see the difference between a class and objects:
Example 1:
Class: Fruit
Objects:
- Apple
- Banana
- Mango
Example 2:
Class: Car
Objects:
- Volvo
- Audi
- Toyota
So, a class is a template for objects, and an object is an instance of a class.
When individual objects are created, they inherit all the variables and methods from the class.
You will learn much more about classes and objects in the next chapter.