TechWriterDev
  • Cloud
    • AWS
      • 00_Doubts
      • CloudPractitioner
        • Cloud Computing
        • AWS Global Infrastructure
        • Introduction to AWS EC2
        • Elastic load balancer(ELB)
        • 04_Messaging_Queuing
        • Aditional Computing Service
        • Accessing AWS resources
        • AWS Networking
        • Storage
        • Amazon Database Solutions
        • Monitoring Tools
        • AWS Security
        • Distributed Denial Of Service Attacks
      • DeveloperAssociate
        • References
        • AWS DVA-C02 Services Index
        • Services
          • 00_IAM
            • Identity and Access Management (IAM)
            • Account Protection Mechanisms
            • Access Mechanism of AWS Resources
            • Security Tools
            • Responsibility Model
            • Advanced Concepts
          • 01_EC2
            • Elastic Compute Cloud (EC2)
            • EC2 Volume Types
            • Amazon Machine Image (AMI)
            • AWS charges for IPv4 address
          • 02_SecurityGroups
            • Security Groups
          • 03_Elastic_LoadBalancing
            • Terminology
            • Elastic load balancer
            • Features
            • Basics
          • 04_AutoScaling
            • Auto Scaling
          • 05_RDS
            • Relational Database Service (RDS)
            • Aurora
            • Security
            • RDS Proxy
          • 06_ElastiCache
            • Cache
            • Cache Offerings
          • 07_Route53
            • Basics of DNS
            • Route 53
          • 08_VPC
            • Virtual Private Cloud (VPC)
          • 09_S3
            • Simple Storage Service (S3)
            • S3 Features
            • S3 Encryption
            • S3 Features
            • S3 Bucket Policy and IAM Policy
          • 10_ECS
            • Elastic Container Service (ECS)
            • Elastic Container Registry (ECR)
            • AWS Copilot
          • 11_EKS
            • Elastic Kubernetes Service (EKS)
          • 12_SDK_CLI_Tips
            • Access AWS Resources
          • 13_CloudFront
            • Cloud Front
          • 14_Messaging
            • Simple Queue Service (SQS)
            • Simple Notification Service (SNS)
            • Fan Out Pattern
            • Kinesis
            • Compare and Contrast
          • 15_ElasticBeanStalk
            • Elastic Beanstalk
          • 16_CloudFormation
            • CloudFormation
            • Dynamic References
          • 17_Monitoring
            • AWS Monitoring
            • AWS CloudWatch
            • CloudWatch Alarms
            • Synthetics Canary
            • Amazon EventBridge (formerly CloudWatch Events)
            • X-Ray
            • OpenTelemetry
            • CloudTrail
          • 18_Lambda
            • Lambda
            • Lambda Integrations
            • Configuring Lambda
            • Lambda Layers
          • 19_API_Gateway
            • API Gateway
            • API Gateway Integrations
          • 20_DynamoDB
            • DynamoDB
            • Operations
            • Indexes
            • DynamoDB Accelerator (DAX)
            • DynamoDB Streams
            • Transactions
            • Integrations
          • 21_CICD
            • CICD
            • CodeCommit
            • CodePipeline
            • CodeBuild
            • CodeDeploy
            • CodeArtifact
            • CloudGuru
          • 22_SAM
            • Serverless Application Model (SAM)
          • 23_CDK
            • Cloud Development Kit (CDK)
          • 24_StepFunctions
            • Step Functions
            • Types of step function
          • 25_AppSync
            • AppSync
          • 26_Amplify
            • Amplify
          • 27_STS
            • Security Token Service (STS)
          • 28_DirectoryService
            • Active Directory
          • 29_KMS
            • Encryption
            • KMS API
            • Features
            • Cloud Hardware Security Module (HSM)
          • 30_SSM_Store
            • SSM Parameter Store
          • 31_SecretsManager
            • Secrets Manager
          • 32_Cognito
            • Cognito
      • Questions
        • AWS_Region
        • EC2
        • IAM
  • Database
    • MongoDb
      • Mongo db Basics
      • Mongo DB Atlas
      • Document
      • Import-Export based on Data Format
      • Mongo Shell Commands
      • Query Operators
      • Indexes
      • Upsert
      • MongoDB Aggregation Framework
      • Aggregation Framework Operators
    • PostgreSQL
      • POSTGRE SQL DataTypes
      • About table
      • Constraints
  • Technologies
    • RabbitMQ
      • RabbitMQ Concepts
      • Introduction to Exchanges
      • Introduction to Queues
    • Terraform
      • 00_Introduction
      • Configuration blocks
      • Commands
      • Variables
      • Terraform Cloud
      • Modules
  • Languages
    • Java
      • Logging
        • Getting Started
      • 00_Core
        • 00_Basics
          • Java Vs C++
          • Object oriented principles
          • Steps to compile a java program
          • JVM Internals
          • Understanding Java Development Kit
          • What is JIT Compiler?
          • Java data types
          • 07_identifiers_type_conversion
          • 08_references_and_packages
          • Steps for attaching scanner
        • Concurrency
          • 00_Threads
            • Threads
          • 01_ExecutorFramework
            • Executor Framework
            • Asynchronous Computation
      • 01_Backend
        • 01_HttpAndWebServerBasics
          • HTTP
          • Content Type
          • Web Server
        • 02_J2EE_Basics
          • J2EE_Basics
          • Why HttpServlet classs is declared as abstract class BUT with 100 % concrete functionality ?
        • 03_TomCatAndSession
          • What is a Session?
          • WebContainer
        • 04_PageNavigation
          • Cookies Additional Information
          • Page Navigation Techniques
        • 05_AboutServlet
          • CGI v/s Servlet
          • Executor Framework
          • Servlet Life cycle
          • SERVLET CONFIG
          • Servlet Context
          • Servlet Listener (web application listener)
        • 08_SpringBoot
          • Spring Boot
          • Some common annotations used in spring eco system
        • 09_SpringDataJPA
          • Spring Data JPA
        • Java_Language_Changes
          • JDK enhancement tracking reference
        • 06_ORM_Hibernate
          • readmes
            • Hibernate
            • Advantages of Hibernate
            • Hibernate Caching
            • Hibernate API
            • Hibernate Query API
            • Hibernate Annotations and JPQL
            • Entity and Value Type
        • 07_SpringFramework
          • bean_validation
            • Bean Validation
          • core
            • readme
              • Spring
              • Spring Framework Modules
              • Spring MVC Request flow
              • Dependency Injection
              • Spring Beans
              • 06_Spring_Framework_Annotations
      • 03_Tools
        • Maven
          • Maven
  • SoftwareEngineering
    • DesignPatterns
      • Notes
        • Basics
        • OOP
        • SOLID Principles
        • 03_Creational
          • Abstract Factory (aka Kit)
          • Builder
          • Factory Method (aka Virtual constructor)
          • Prototype
          • Singleton
        • 04_Structural
          • Adapter (aka Wrapper)
          • Bridge (aka Handle | Body)
          • Composite
          • Decorator (aka Wrapper)
          • Facade
          • Flyweight
          • Proxy (aka Surrogate)
        • 05_Behavioral
          • Chain of Responsibility
          • Command (aka Action | Transaction)
          • Iterator (aka Cursor)
          • Observer (aka Publish-Subscribe | Dependents)
          • Strategy (aka Policy)
    • Principles
      • REST
        • REST
  • Tools
    • Containers
      • Docker
        • Docker
        • Docker Image
        • Commands
        • Compose
        • Best Practices
      • Kubernetes
        • Kubernetes
    • VCS
      • Git
        • Quick reference of useful Git commands
Powered by GitBook
On this page
  • Class & Object
  • Class
  • Object
  • Usage of this
  • Encapsulation in Java
  • method local variables v/s instance data members
  1. Languages
  2. Java
  3. 00_Core
  4. 00_Basics

Object oriented principles

Class & Object

  • Classes and Objects are basic concepts of Object Oriented Programming which revolve around the real life entities.

Class

  • A class is a user defined blueprint or prototype from which objects are created.

  • It represents the set of properties or methods that are common to all objects of one type.

  • Class declaration includes,

    1. Access specifiers: A class can be public or has default access (package private).

    2. Class name: The name should begin with a capital letter & then follow camel case convention.

    3. Superclass:(if any): The name of the class's parent (superclass), if any, preceded by the keyword extends.

      • Implicit super class of all java classes is java.lang.Object.

    4. Interfaces:(if any): A comma-separated list of interfaces implemented by the class, preceded by the keyword implements.

      • A class can implement more than one interface.

    5. Body: Class body is surrounded by {}.

    6. Constructors: Used for initializing new objects.

    7. Fields: Variables that provides the state of the class and its objects.

    8. Methods: Used to implement the behavior of the class and its objects.

      eg : Student,Employee,Flight,PurchaseOrder, Shape ,BankAccount

Object

  • It is a basic unit of Object Oriented Programming and represents the real life entities.

  • A typical Java program creates many objects, which interacts by invoking methods.

  • An object consists of :

    Construct
    Description

    State

    It is represented by attributes of an object (properties of an object).

    Behavior

    It is represented by methods of an object (actions upon data)

    Identity

    It gives a unique identity to an object and enables one object to interact with other objects. eg : Emp id / Student PRN / Invoice No

  • Creating an object

    • The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the class constructor.

    • Constructor is a special method having same name as the class name with no explicit return type and may be parameterized or parameter less (Default constructor).

    • Parameterized constructor is used initialize the state of the object.

    • If a class does not explicitly declare any constructor, then the Java compiler automatically provides a no-argument constructor, called the default constructor.

    • This default constructor implicitly calls the super class's no-argument constructor i.e., java.lang.Object class's constructor.

Usage of this

  1. To unhide, instance variables from method local variables (to resolve the conflict).

  this.name=name;
  1. To invoke the constructor, from another overloaded constructor in the same class. (constructor chaining -> to avoid duplication)

Encapsulation in Java

  • Encapsulation is defined as the wrapping up of data & code as a single unit. It is the mechanism that binds together code and the data it manipulates.

  • It's is a protective shield that prevents the private data from being accessed by the code outside this shield.

  • The variables or data of a class is hidden from any other class and can be accessed only through any member function/method of own class in which they are declared.

  • As in encapsulation, the data in a class is hidden from other classes, so it is also known as data-hiding.

  • Tight Encapsulation can be achieved by declaring all the variables in the class as private and writing public methods as its accessors.

Advantages of Encapsulation:

  1. Data Hiding (security)

  2. Increased flexibility: We can make the variables of the class as read-only or write only or r/w.

  3. Reusability: Encapsulation also improves the re-usability and easy to change with new requirements.

  4. Testing code is easy.

  • Summary

    • Encapsulation, consists of Data hiding + Abstraction.

    • Information hiding is achieved by private data members & supplying public accessors.

    • Abstraction is achieved by supplying an interface to the Client (customer).

    • Highlighting only WHAT is to be done & not highlighting HOW it's internally implemented.

method local variables v/s instance data members

  • Method local variable are allocated on stack, by default uninitialized.

  • Javac(compiler) doesn't allow accessing ANY(primitive type or reference type) un-initialized variable.

  • Instance data members are allocated on heap, initialized to default values.

  boolean ---> false
  int --> 0
  double --> 0.0
  ref types --> null
PreviousJava Vs C++NextSteps to compile a java program

Last updated 3 months ago