Object-Oriented Programming (OOP) is a programming paradigm, or style, that is based on the concept of "objects". Objects can contain data in the form of fields (often known as attributes or properties), and code in the form of procedures (often known as methods).
Java is a pure object-oriented language. Understanding OOP principles is fundamental to writing effective, scalable, and maintainable Java applications.
OOP offers several advantages over procedural programming:
Object-Oriented Programming is built upon four main principles, often called the "four pillars" of OOP.
Encapsulation is the practice of bundling the data (attributes) and the methods that operate on the data into a single unit, or "object". It's also about restricting direct access to some of an object's components, which is a key part of data hiding.
Abstraction means hiding complex implementation details and showing only the essential features of the object. It's about simplifying a complex reality by modeling classes appropriate to the problem.
Inheritance is a mechanism where a new class derives properties and behaviors (fields and methods) from an existing class. The class that is inherited from is called the parent or superclass, and the class that inherits is called the child or subclass.
Polymorphism (from Greek, meaning "many forms") is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. It allows you to perform a single action in different ways.
draw(). Different child classes like "Circle", "Square", and "Triangle" can all inherit from "Shape" and have their own unique implementation of the draw() method.These are the two main aspects of object-oriented programming.
Car class would define that all cars have a color and a brand.myCar object from the Car class with the color "Red" and brand "Ford".In the upcoming chapters, we will explore each of these concepts in detail with practical Java code examples.