What is OOPS concept in Java?
Object-Oriented Programming (OOP) is a programming paradigm centered around the concept of “objects.” Java, being an object-oriented language, heavily relies on the OOP concepts to structure its code. The key principles of OOP are:
- Object
- Class
- Inheritance
- Polymorphism
- Abstraction
- Encapsulation
What is object ?
In Java, an object is an instance of a class. Objects are the fundamental building blocks in object-oriented programming (OOP) and represent real-world entities with attributes (data) and behaviors (methods).
Key Characteristics of Objects in Java: #
1. State: The state of an object is represented by its attributes (also known as fields or properties). These attributes hold the data or values that define the object. For example, a Car object might have attributes like color, make, model, and year.
2. Behavior: The behavior of an object is represented by its methods. Methods define the actions or operations that can be performed by the object. For example, a Car object might have methods like start(), stop(), and accelerate().
3. Identity: Every object in Java has a unique identity, which distinguishes it from other objects, even if they have the same state and behavior. The identity is managed internally by Java and can be accessed through the object’s memory address.
What is class?
In Java, a class is a blueprint or a template for creating objects. It defines a data type by bundling data (attributes or fields) and methods (functions or behaviors) that operate on the data. A class encapsulates data for the object and methods to manipulate that data.
Key Components of a Class: #
1. Fields (Attributes): These are variables that hold the data or state of an object. Each object of the class will have its own copy of these variables.
2. Methods: These are functions defined inside the class that describe the behaviors or actions that an object of the class can perform.
3. Constructors: Special methods used to initialize objects. A constructor is called when an object of a class is created.
4. Access Modifiers: These control the visibility of class members (fields and methods) outside the class. Common access modifiers are public, private, protected, and the default (no modifier).
What is Inheritance?
Inheritance allows a new class to inherit the properties and behavior (methods) of an existing class. The class that is inherited from is called the superclass or parent class, and the class that inherits is called the subclass or child class.
What is Polymorphism?
Polymorphism allows objects of different classes to be treated as objects of a common superclass. It enables a single method to perform different tasks based on the object it is acting upon. Polymorphism can be achieved through method overloading (compile-time polymorphism) and method overriding (runtime polymorphism).
What is Abstraction ?
Abstraction is the concept of hiding the complex implementation details of an object and exposing only the essential features. In Java, abstraction is achieved using abstract classes and interfaces.
What is Encapsulation ?Encapsulation is the practice of bundling the data (attributes) and the methods (functions) that operate on the data into a single unit, or class. It also involves restricting direct access to some of the object’s components, which is usually done using access modifiers like private, protected, and public.