--- title: Foundations collection: Java Oops author: Suraj Kumar Jha updated: 2026-09-29 source: https://docs.chaicode.com/java-oops/foundations --- # Foundations ![A blank digital Zomato order form template (Class) producing multiple filled order tickets (Objects).](https://docs.chaicode.com/cdn-cgi/image/width=1600,format=auto,fit=scale-down/assets/0dedd399e29cf6c6ba846255be84abf7354503a1ab9f9339ec4d4884e3bb0e53.jpg) Class vs Object: The Zomato Blueprint — A Class is just a template or blueprint—it defines what data and actions an entity will have. An Object is the real, physical instance created from that blueprint. Class: A generic Zomato 'FoodOrder' design. Object: Your actual ₹350 Paneer Tikka order #9021. ![Illustration of a Swiggy delivery agent card displaying data fields on top and actionable buttons for methods at the bottom.](https://docs.chaicode.com/cdn-cgi/image/width=1600,format=auto,fit=scale-down/assets/570cebfedbf2b8d624dff6dad1b45adb7281cf896b2f49fb1d116be0990e464e.jpg) Fields & Methods: Data and Actions — Fields store the state or data of an object. Methods define the behavior or actions an object can perform. Example: A Swiggy delivery object has fields like 'driverName' and 'bikeNumber', and methods like 'acceptOrder()' or 'deliverFood()'. Every object gets its own separate copy of fields. ![A Flipkart delivery box being filled with item tags right at the assembly line stage.](https://docs.chaicode.com/cdn-cgi/image/width=1600,format=auto,fit=scale-down/assets/e8bc620d91152db6dda3dc577112fa2c9a76227657ea46a0b3f7b2ee667a8fee.jpg) Constructors: Object Setup — A constructor is a special setup block that runs automatically when an object is created. It has the exact same name as the class and no return type. Its main job is to set initial values for fields. Example: Creating a Flipkart order directly initializes 'itemName' as 'iPhone 15' and 'price' as ₹79,999. ![Flowchart showing Stack variable 'myPass' pointing with an arrow to a newly created Jio Cinema subscription block in Heap memory.](https://docs.chaicode.com/cdn-cgi/image/width=1600,format=auto,fit=scale-down/assets/52a1f7b29d23afdd0a5126d868bf2d644a7bf4a30ef713776f22349416bc987d.jpg) Under the Hood: The 'new' Keyword — Step 1: 'new' allocates fresh memory in the Heap for the object. Step 2: Java sets default values (0, null, false) to all fields. Step 3: The constructor runs and fills in your actual values. Step 4: Java returns a reference variable pointing to that Heap location. ![A PhonePe profile screen pointing an arrow back to itself labeled 'this'.](https://docs.chaicode.com/cdn-cgi/image/width=1600,format=auto,fit=scale-down/assets/e0676fbf0505ddbf71e73c86275c3824b22593c6da0070d5ab728242c8443a4e.jpg) The 'this' Keyword: Self Reference — 'this' refers to the current object executing the code. It helps resolve confusion when parameter names match field names (e.g., 'this.upiId = upiId'). Can also be used to trigger other methods or return the current instance. It only works inside instance methods, not static ones. ![A 4-tier security door system showing strict to open access permissions.](https://docs.chaicode.com/cdn-cgi/image/width=1600,format=auto,fit=scale-down/assets/814a1a74751af4eaaa3614787649941a654c34a5c60984f899d2d2f14fdbe4ef.jpg) Access Modifiers: Access Control — private: Accessible only inside the same class (like your UPI PIN). default: Accessible anywhere in the same package (no keyword). protected: Accessible in same package + child subclasses. public: Accessible from anywhere in the whole app. ![An Indian Bank ATM machine enclosing cash inside, offering a keypad (Setter) and screen (Getter) for safe access.](https://docs.chaicode.com/cdn-cgi/image/width=1600,format=auto,fit=scale-down/assets/8e2d721148490788a615d8e26bb05ba345e42d5ad2b74afb2177b26317ae3939.jpg) Encapsulation: Data Protection — Encapsulation combines data and methods into one unit while hiding direct access to sensitive fields. Keep variables 'private' so no outside code can directly corrupt them. Provide 'public' Getters and Setters to read or update data safely after checking rules. Example: An ATM machine doesn't let you grab cash manually; you must use the withdraw() method.