Object
β An Object is a fundamental unit of Object-Oriented Programming (OOP) that represents real-world entities. It is created from a class and contains the attributes and behaviors defined by the class. Once an object is created, it can execute the methods and access the properties of its class.
Class
β A Class is a blueprint for objects. It represents a collection of objects and defines their properties and behaviors. Classes do not take up memory space until an object is created from them.
- A class can be considered as a logical entity that encapsulates data and methods.
- Class names typically start with a capital letter.
Example: A class defines the common properties and methods shared by all objects of the same type.
Access Modifiers
In Java, access modifiers control the scope and visibility of classes, methods, and variables. They provide security and define accessibility.
Types of Access Modifiers:
-
Default:
- If no access modifier is specified, Java treats the variable, method, or class as having the default modifier.
- Default modifiers are accessible only within the same package. Example:
- Two code files in the same package can access default methods or variables simply by creating an object of the file containing the methods.
-
Private:
- Specified using the
private
keyword. - Accessible only within the class in which they are declared.
- Private modifiers cannot be used on top-level classes or interfaces because they limit access to within the enclosing class.
- Specified using the
Example: Private modifiers are typically used for nested classes or data members within a top-level class. Private members can only be accessed using getter and setter methods.
-
Protected:
- Declared with the
protected
keyword. - Accessible within the same package or in subclasses in different packages.
- Not accessible in unrelated classes outside the package.
- Declared with the
-
Public:
- Specified with the
public
keyword. - Accessible from any class, package, or subclass, even outside the package.
- Specified with the
Keyword this
β The this
keyword refers to the current object. It is used to access the objectβs variables or reference the object itself within a method or constructor.
OOP Principles
Object-Oriented Programming (OOP) revolves around four main concepts:
-
Encapsulation:
- Encapsulation is the process of wrapping up data and methods in a single unit.
- It promotes data hiding by restricting access to certain components.
- Example: A class acts as a capsule that holds data (attributes) and methods (behaviors).
Polymorphism: Enables methods or objects to perform different tasks based on their implementation.
Abstraction: Focuses on exposing only the essential features of an object while hiding the implementation details.
Inheritance: Allows one class to inherit the properties and methods of another class, promoting code reuse.
Constructors
A constructor is a special type of method in a class:
- It is invoked automatically when an object is created.
- It is used to initialize the properties of an object.
- Constructors share the same name as the class and do not have a return type.
follow me for more π
Top comments (0)