What is OOP?
OOP stands for Object-Oriented Programming. Writing methods that manipulate data is the essence of procedural programming, whereas object-oriented programming focuses on building objects that hold both data and methods.
Object-Oriented Programming (OOP) is a programming prototype widely used in Java and many other programming languages. In Java, every element is linked to classes and objects, which have their own attributes and methods. The concept of ‘objects’, which are instances of classes, is the focus of it. Each object contains both data (attributes) and methods (functions) that operate on that data. The main principles of OOP in Java are:
- Object
- Class
- Inheritance
- Polymorphism
- Abstraction
- Encapsulation
Class:
In Java, a class functions as a blueprint used to create objects. It acts as a template that specifies the structure and behavior of objects belonging to a specific category. When you instantiate a class by creating an object from it, the resulting object is an instance of that class, inheriting its defined characteristics and behaviors. Let’s describe the concept of class in Java:
1. Class Declaration:
- A class is defined by using the ‘class’ keyword, followed by the chosen class name. This text is generated to be distinct.
- The class name should start with an uppercase letter and follow Java naming conventions (e.g., Account, MyClass, Car, Person
2. Class Members:
- A class can contain various members, such as fields (attributes) and methods (functions).
- Fields represent the data or state of the objects created from the class.
- Methods encapsulate the functionality and operations that objects are capable of executing.
3. Fields (Attributes):
- Fields are variables declared within the class.
- They represent the data associated with each object of the class.
- Fields can be of different data types, such as int, String, boolean, or even other class types.
4. Constructors:
- Constructors are special methods used to initialize objects when they are created from the class.
- The method shares its name with the class and lacks a return type.
- Constructors are typically used to set initial values to the object’s attributes.
5. Methods (Functions):
- Methods are functions defined within the class that operate on the class’s data (fields).
- They define the behavior and actions that the objects can perform.
- Methods can have parameters and return types.
6. Access Modifiers:
- Access modifiers such as public, private, protected, and default (no modifier) are available in Java.
- Access modifiers determine the visibility and accessibility of class members, such as fields and methods, within a program.
- For example, public members are accessible from outside the class, while private members are only accessible within the class itself.
Object:
One of the core ideas of object-oriented programming (OOP) in Java is an object. An object is a live embodiment of a class, actively carrying out the class’s instructions. Once the object is created, it takes up space like other variables in memory, objects in Java are like individual things that you create based on a blueprint (class), and each object can have its own attributes and do specific actions (methods). Each object in Java has a unique identity. Objects are manipulated through references, which are memory addresses pointing to the object
To create an object, you use the new keyword followed by the class constructor. The constructor initializes the object and allocates memory for its attributes
Example:
public class Toyota { //class declaration String color; //Class Properties/Attributes int door; String model; Toyota(String color, int door, String model) { //constructor this.color = color; //'this' refers to associate Object Name this.door = door; this.model = model; } public void print() { //method System.out.println( this.color ); System.out.println( this.door ); System.out.println( this.model ); } }//class public class ObjectMain { public static void main(String[] args) { Toyota corolla; // object declare corolla = new Toyota("Red", 4, "Corolla SE"); //instantiate the cleass corolla.print(); //method call by object Toyota camry; camry = new Toyota("Blue", 2, "Camry XLE"); camry.print(); } }
Output:
Red
4
Corolla SE
Blue
2
Camry XLE
This code is an example of Object-Oriented Programming (OOP) in Java using a class called “Toyota.” Let’s break it down step by step:
1. Class Declaration:
- – The code starts with the declaration of a class named “Toyota.”
- – The class has three properties (also known as attributes or variables):
- – ‘color’ (color record for car)
- – `door` (to store the number of doors in the car)
- – `model` (to store the car model name)
2. Constructor:
- – Next, there is a constructor defined inside the class. Which has the same name as the class name, ‘ Toyota’.
- – The constructor has three parameters: `color`, `door`, and `model`.
- – When an object of the class is created, the constructor sets the provided values of `color`, `door`, and `model` to the object’s properties.
3. Method:
- – There is a method named `print()` inside the class. A method can be thought of as a distinct set of instructions within a program, dedicated to executing a particular function or task.
- – The `print()` method prints the values of the object’s properties (color, door, and model) to the console.
4. Main Method:
- The code starts with the declaration of a class named “ObjectMain”
- – In the
main
method, the program’s entry point, two objects are instantiated. - – Inside the `main` method, two objects are created:
- – `corolla`: A Toyota object with color “Red,” 4 doors, and the model “Corolla SE.”
- – `camry`: Another Toyota object with color “Blue,” 2 doors, and the model “Camry XLE.”
- – The constructor is called automatically when these objects are created, setting their respective properties. The “new” keyword in this case instantiates the “Toyota” class.
5. Method Call:
- – After creating the objects, the `print()` method is called on both objects.
- – When the `print()` method is called, it prints the color, door count, and model of each object to the console.
Overall, this code demonstrates how to create a class, define properties, use a constructor to initialize objects, and call methods on those objects to perform specific actions. In this case, it creates two Toyota cars and prints their details using the `print()` method.