🧵 Java Series (Deep Learning) 3: Understanding Encapsulation with Class and Object
Hi, X Family! 🌞
In our last thread, we explored OOP concepts. Today, let's dive into encapsulation with class and object in detail.
Encapsulation is about bundling data (attributes) and methods (behaviors) together.
#Java #OOP #Encapsulation
What is a class? 🤔
A class is like a blueprint or template for creating objects.
- Think of it as a blueprint for a house. The blueprint defines what a house is (walls, doors, windows, etc.), but it's not a house itself.
#JavaClass #programming
-If the class is the blueprint for a house, then the object is the actual house built from that blueprint.
- So, an object is an instance of a class. Each house built from the same blueprint can have different colors, sizes, but they all follow the same basic design.
Class: (has below data members)
Variables (attributes): Variables that belong to the class, describing the state or characteristics of an object. E.g., in the Account class, accountNumber, accountBalance are attributes.
Methods (Behaviors): Methods that belong to the class, describing the actions the object can perform. E.g., we can withDrawMoney() from Account.
#JavaAttributes #JavaMethods
Constructor: Constructor is a special method, which initialize class attributes, when creating an object.
- if a method is constructor, then class name and method name should be same
Object (Instance):
When you create an object from a class, you allocate memory to store the values of the attributes and provide implementations for the methods defined in the class.
#JavaInstance
- we will create an object by using new keyword and constructor.
- within the Account class we bundled everything (variables, methods and constructor).
- It ensures that data is safely contained within objects, making your code more modular and easier to maintain.