Ohio Computer Academy - IT Training & Vocational Education in Dublin, OH
Ohio Computer Academy - IT Training & Vocational Education in Dublin, OH
All Courses
  • Free Training Courses
  • AI for Your Role
  • AI & Machine Learning
  • Cloud Tech
  • Data Science
  • DevOps
  • Dashboarding/BI
  • Database
  • WebDevelopment
  • Programming & Coding
  • MS Office & Workplace Productivity
  • QA & Test Automation
  • Professional & Career Development
  • Cyber Security

Courses

  • Computer Fundamentals for Beginners
  • MS Word for Beginners
  • Microsoft Excel for Beginners
  • Microsoft Azure for Beginners
  • AWS Cloud for Beginners
  • Oracle SQL Fundamentals
  • Tableau Fundamentals
  • Power BI Fundamentals
Explore All Programs

Courses

  • AI Essentials
  • AI for Administrative Professionals New Launch
  • AI for Managers & Team Leads
  • AI for Data Analytics & Power BI
  • AI for Software Developers
  • AI for Database & SQL Professionals
  • AI for Project Managers
  • AI for Marketing Professionals
  • AI for Sales Professionals
  • AI for HR Professionals
  • AI for Finance Professionals
  • AI for Customer Support Professionals
  • AI for QA & Test Engineers
  • AI for IT Support & System Admins
  • AI for Cybersecurity Analysts
  • AI for Job Seekers
Explore All Programs

Courses

  • Learn Claude Basics New Launch
  • Learn Claude Advanced New Launch
  • Applied Artificial Intelligence (AI) New Launch
  • AI for Workplace Productivity
  • Azure Fundamentals with AI
  • Advanced Microsoft Azure with AI
  • AI Implications for Business Strategy
  • Mastering ChatGPT & Generative AI Tools
  • Prompt Engineering
  • Agentic AI Solutions
  • AI Product Management
  • AI for Quality Control
Explore All Programs

Courses

  • AZ-900 Azure Fundamentals
  • DP-900 Azure Data Fundamentals
  • DP-100 Data Science on Azure
  • AWS Essentials
  • AWS Cloud Practitioner
  • AWS Solutions Architect
Explore All Programs

Courses

  • DP-100 Data Science on Azure
Explore All Programs

Courses

  • DevOps Essentials
  • DevOps with AWS
  • DevOps with Azure
Explore All Programs

Courses

  • Business Analytics: From Data to Insights New Launch
  • Power BI Advanced
  • Data Analytics with Excel
  • Tableau Beginner
  • Tableau Data Consumer
  • Alteryx & Tableau
  • Tableau Advanced
  • Tableau Certification prep
  • Power BI Beginner
  • Cognos Analytics
Explore All Programs

Courses

  • SQL Server for Beginners
  • SQL for Analysts
  • SQL Server Advanced
  • SQL Server T-SQL Programming
  • SSIS
  • Oracle SQL for Beginners
  • Oracle PL/SQL
  • Oracle SQL Advanced
  • Big Data with Hadoop: HDFS, MapReduce & Ecosystem Tools
  • MS Access
Explore All Programs

Courses

  • Angular Frontend Development
  • Spring 5 Framework
  • Full Stack Web Development
Explore All Programs

Courses

  • Learn Python
  • Python PCEP
  • Python PCAP
  • JavaScript & jQuery
  • Java Certification
  • Python for Data Science
  • Machine Learning with Python
Explore All Programs

Courses

  • Analyzing Data with Excel New Launch
  • Workplace Digital Skills
  • Excel Beginners to Advanced Trending Now
  • Excel for Finance
  • Excel for HR
  • Workplace Computer Skills
Explore All Programs

Courses

  • Selenium QA Automation (Java)
  • Selenium QA Automation (Python)
  • API Testing
  • QA Automation with Python (Advanced)
Explore All Programs

Courses

  • Scrum Master Certification
  • Lean Six Sigma Green Belt Certification
  • Business Analyst
Explore All Programs

Courses

  • Cyber Security Foundations for Beginners
  • Computer Networking for Cybersecurity
  • Windows & Linux Security Administration
  • Ethical Hacking & Penetration Testing
  • Artificial Intelligence (AI) in Cybersecurity
Explore All Programs
  • About Us
    • OCA Skill Sprint Method
    • Meet the Founder
  • Blogs
    • Career Building
    • AI / Machine Learning
    • MS Excel
    • Programming Essentials
    • Database
    • Cloud Computing
    • Tableau
    • Power BI
    • Web Development
    • Tips for Success
    • Qlik
    • Soft Skills
    • Technology
    • Business Intelligence Analyst
    • Message from the CEO
    • DATA ANALYST
  • TechCred
  • Find Your Training
  • Contact Us
Ohio Computer Academy - IT Training & Vocational Education in Dublin, OH
Ohio Computer Academy - IT Training & Vocational Education in Dublin, OH

 

  • info@ohiocomputeracademy.com
  • +1 866-648-7284
  • Home
  • About Us
  • Blogs
    • Career Building
    • AI / Machine Learning
    • MS Excel
    • Programming Essentials
    • Database
    • Cloud Computing
    • Tableau
    • Power BI
    • Web Development
    • Tips for Success
    • Qlik
    • Soft Skills
    • Technology
    • Business Intelligence Analyst
    • Message from the CEO
    • DATA ANALYST
  • TechCred
  • Find Your Training
  • Contact Us
Find With Us
  • Home
  • Blogs
  • Technology
  • Encapsulation in Java

Encapsulation in Java

Encapsulation, Polymorphism, and Inheritance are the core object oriented programming principles. In this blog, I will talk about encapsulation in Java.

Encapsulation is the mechanism that binds together code and the data it manipulates.

In Java, the basis of encapsulation is the Class. A class defines the structure and behaviour (data and code) that is shared by a set of objects.

Encapsulation (information hiding) is achieved

  • By combining data and behaviour in one package and hiding the implementation details from the users of the object.

  • The key to encapsulation is to have methods never directly access instance variables in a class other than their own.

  • Programs should interact with object data only through the objects methods. Declare all the variables in the class as private and have public Mutator methods and Accessor methods.

In the following program, all Client class variables are declared as private and can be accessed only with their Mutator and Accessor methods as shown by the test code.

// Java program to demonstrate encapsulation
public class Client {
// private variables declared
// these can only be accessed by
// public methods of class
private Portfolio portfolio;
private String first_Name;
private String last_Name;
private String location;
private int age;
public Client() {
portfolio=null;
first_Name=””;
last_Name=””;
location=””;
age=0;
}
public Portfolio getPortfolio() {
return portfolio;
}
public void setPortfolio(Portfolio portfolio) {
this.portfolio = portfolio;
}
public String getFirstName() {
return first_Name;
}
public void setFirstName(String name) {
this.first_Name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getLast_Name() {
return last_Name;
}
public void setLast_Name(String last_Name) {
this.last_Name = last_Name;
}
}
public class TestEncapsulation
{
public static void main (String[] args)
{
Client obj = new Client();
// setting values of the variables
obj.setFirstName(“Dan“);
obj.setLast_Name(“Burke“);
obj.setAge(19);
obj.setLocation(“America“);
// Displaying values of the variables
System.out.println(“Client’s name: “ + obj.getFirstName());
System.out.println(“Client’s age: “ + obj.getAge());
System.out.println(“Client’s roll: “ + obj.getLast_Name());
System.out.println(“Client’s roll: “ + obj.getLocation());
// Direct access of first_Name is not possible
// due to encapsulation
// System.out.println(“Client’s name: ” + obj.first_Name());
}
}
Output:
Dan
19
Burke
America
Advantages of Encapsulation:
  • Data Hiding: The user has no idea about the inner implementation of the class. The class is then safe from unrestricted manipulations, as the only access to the variables is through their getter and setter.

  • Increased Access Control: The variables of the class can be made read-only or write-only depending on the requirement. To make the variables read-only, omit the setter methods or to make the variables as write-only, omit the getter.

  • Reusability: Encapsulation enables reusability and redesign as per new requirements.

  • Testing code is easy: It is much easier to test encapsulated code with unit testing.

Like
Chandraish Sinha

Chandraish Sinha is the Founder and President of Ohio Computer Academy, a leading institution committed to delivering high-quality IT education. With a passion for teaching and a belief in his company’s mission—Inspire, Educate & Evolve—Chandraish brings over 25 years of experience in the Information Technology industry.

Explore more of his work on his Amazon Author Profile.

0 Comments

Add Your Comment

Post a Comment

Required fields are marked *

✅ Thanks for adding your comment! It will appear after moderation.

0 Comments

Related Blogs
View All Blogs
Tableau vs QlikView
Read More
What is System Development Life Cycle (SDLC)
Read More
Power BI vs Tableau
Read More
Ohio Computer Academy - IT Training & Vocational Education in Dublin, OH
Ohio Computer Academy - IT Training & Vocational Education in Dublin, OH

Registered with the Ohio State Board of Career Colleges and Schools — School Registration No. 2395

About Us
  • Contact Us
  • Our Story
  • Training Gallery
  • Reviews
  • Books
  • Greater Columbus Training Center
Trainings/Services
  • Course Categories
  • Learning Paths
  • Frequently Asked Questions
  • AI Advantage
Contact Us
  • Phone:+1 866-648-7284
  • E-mail: info@ohiocomputeracademy.com
  • Address:

    Dublin, Ohio
    5900 Sawmill Road, Suite # 150, Dublin, OHIO 43017 USA

Thank you
Failed
Free Training Courses
  • Computer Fundamentals for Beginners
  • MS Word for Beginners
  • Microsoft Excel for Beginners
AI Courses
  • Claude Basics
  • Claude Advanced
  • AI for Workplace Productivity
  • Prompt Engineering
  • Agentic AI Solutions
  • AI for Quality Control
Cloud Tech
  • DP-900 Azure Data Fundamentals
  • DP-100 Data Science on Azure
  • AWS Technical Essentials
  • AWS Cloud Practitioner
  • AWS Solutions Architect
DevOps
  • DevOps Essentials
  • DevOps with AWS
  • DevOps with Azure
Dashboarding/BI
  • Tableau Beginner
  • Power BI Beginner
  • Data Analytics with Excel
Database
  • Oracle SQL for Beginners
  • SQL for Analysts
  • SQL Server for Beginners
MS Office & Workplace Productivity
  • Workplace Digital Skills
  • Excel for Finance
  • Excel for HR
  • Excel Beginners to Advanced
  • Workplace Computer Skills
Learning Hub
  • Begin Your IT Career
  • Generative AI & Workplace Productivity Foundations
  • Claude AI Learning Path
  • Data Visualization Foundations
  • Data Analytics Career Path
  • AI for Business & Data Applications
  • Programming Foundations & Development Path

© 2026 Ohio Computer Academy. All rights reserved.

  • Privacy
  • Terms & Conditions