SlideShare a Scribd company logo
1 of 5
Contribute Us | Ask Question | login
JavaTpoint
Subscribe Us
91-9990449935
0120-4256464
 Home
 Core Java
 Servlet
 JSP
 EJB
 Struts2
 Mail
 Hibernate
 Spring
 Android
 Design P
 Quiz
 Projects
 Interview Q
 Comment
 Forum
 Training
Basics of Java
Java OOPs Concepts
Advantage of OOPsNaming ConventionObject and ClassMethod OverloadingConstructorstatic
keywordthis keywordInheritance(IS-A)Aggregation(HAS-A)Method OverridingCovariant
Return Typesuper keywordInstance Initializer blockfinal keywordRuntime
PolymorphismDynamic Bindinginstanceof operatorAbstract classInterfaceAbstract vs
InterfacePackageAccess ModifiersEncapsulationObject classObject CloningJava ArrayCall
By Valuestrictfp keywordAPI DocumentCommand Line ArgArray Quiz-1Array Quiz-2OOPs
Quiz-1OOPs Quiz-2OOPs Quiz-3OOPs Quiz-4
Java StringJava RegexException HandlingJava Inner
classesMultithreadingSynchronizationI/OSerializationNetworkingAWTEvent
HandlingSwingLayoutManagerAppletReflection APICollectionJDBCJava New
FeaturesRMIInternationalization
next »« prev
Encapsulation in Java
Encapsulation in java is a process of wrapping code and data together into a single
unit, for example capsule i.e. mixed of several medicines.
We can create a fully encapsulated class in java by making all the data members of the
class private. Now we can use setter and getter methods to set and get the data in it.
The Java Bean class is the example of fully encapsulated class.
Advantage of Encapsulation in java
By providing only setter or getter method, you can make the class read-only or write-
only.
It provides you the control over the data. Suppose you want to set the value of id i.e.
greater than 100 only, you can write the logic inside the setter method.
Simple example of encapsulation in java
Let's see the simple example of encapsulation that has only one field with its setter and
getter methods.
1. //save as Student.java
2. package com.javatpoint;
3. public class Student{
4. private String name;
5.
6. public String getName(){
7. return name;
8. }
9. public void setName(String name){
10. this.name=name
11. }
12. }
1. //save as Test.java
2. package com.javatpoint;
3. class Test{
4. public static void main(String[] args){
5. Student s=new Student();
6. s.setname("vijay");
7. System.out.println(s.getName());
8. }
9. }
Compile By: javac -d . Test.java
Run By: java com.javatpoint.Test
Output: vijay
Next TopicObject class in java
« prevnext »
Like/Subscribe us for latest updates or new sletter ↑Top
Tutorials
 » Core Java Tutorial
 » Servlet Tutorial
 » JSP Tutorial
 » Mail API Tutorial
 » Design Pattern Tutorial
 » Struts Tutorial
 » Spring Tutorial
 » Hibernate Tutorial
 » Android Tutorial
 » JavaScript Tutorial
 » SQL Tutorial
 » C Tutorial
 » AJAX Tutorial
 » JUnit Tutorial
 » JAXB Tutorial
 » Maven Tutorial
Interview Questions
 » Java Interview
 » Servlet Interview
 » JSP Interview
 » Hibernate Interview
 » Spring Interview
 » Android Interview
 » SQL Interview
 » PL/SQL Interview
 » Oracle Interview
 » MySQL Interview
 » SQL Server Interview
 » MongoDB Interview
 » Cloud Interview
Quizzes
 » Core Java quiz
 » Servlet quiz
 » JSP quiz
 » Struts2 quiz
 » Android quiz
 » OCJP quiz
 » OCWCD quiz
 » Hibernate quiz
 » Spring quiz
 » C quiz
 » Cloud Computing quiz
 » JavaScript quiz
 » SQL quiz
Forum
 » Core Java Ques.
 » Servlet Ques.
 » JSP Ques.
 » Struts Ques.
 » Spring Ques.
 » Hibernate Ques.
 » Android Ques.
Projects
SSS IT PVT LTD
 » Development
 » Training
 » SEO
 » Consultancy
CONTACT US
Tel. : 0120-4256464
Mob. : +91 9990449935
Email : enquiry@sssit.org
Address: Plot - 6, GT Road,
Near MMX Mall,
Mohan Nagar, Ghaziabad,
201007, UP, India
» Contact Us
» Privacy Policy
© 2011-2014 Javatpoint.
All Rights Reserved.

More Related Content

Viewers also liked

Visual Basic Codes And Screen Designs
Visual Basic Codes And Screen DesignsVisual Basic Codes And Screen Designs
Visual Basic Codes And Screen Designs
prcastano
 

Viewers also liked (10)

Tally erp 9 notes and practice book
Tally erp 9 notes and practice bookTally erp 9 notes and practice book
Tally erp 9 notes and practice book
 
History of computers mcq
History of computers mcqHistory of computers mcq
History of computers mcq
 
Tally 9 Purchase Entry
Tally 9 Purchase EntryTally 9 Purchase Entry
Tally 9 Purchase Entry
 
Computer fundamentals mcq quiz - Practice and prepare with mcqSets.com
Computer fundamentals mcq quiz - Practice and prepare with mcqSets.comComputer fundamentals mcq quiz - Practice and prepare with mcqSets.com
Computer fundamentals mcq quiz - Practice and prepare with mcqSets.com
 
Microsoft Office Package: Practical Questions
Microsoft Office Package: Practical QuestionsMicrosoft Office Package: Practical Questions
Microsoft Office Package: Practical Questions
 
MCQ Bank for Computer Fundamantals from mcqSets.com
MCQ Bank for Computer Fundamantals from mcqSets.comMCQ Bank for Computer Fundamantals from mcqSets.com
MCQ Bank for Computer Fundamantals from mcqSets.com
 
Computer systems mcq
Computer systems  mcqComputer systems  mcq
Computer systems mcq
 
Solved Question Paper of Computer Operator Examination Conducted by EPF 2016
Solved Question Paper of Computer Operator Examination Conducted by EPF 2016Solved Question Paper of Computer Operator Examination Conducted by EPF 2016
Solved Question Paper of Computer Operator Examination Conducted by EPF 2016
 
Visual Basic Codes And Screen Designs
Visual Basic Codes And Screen DesignsVisual Basic Codes And Screen Designs
Visual Basic Codes And Screen Designs
 
Advanced voucher entry tutorial in Tally ERP 9
Advanced voucher entry tutorial in Tally ERP 9Advanced voucher entry tutorial in Tally ERP 9
Advanced voucher entry tutorial in Tally ERP 9
 

Similar to Encapsulation

201 core java interview questions oo ps interview questions - javatpoint
201 core java interview questions   oo ps interview questions - javatpoint201 core java interview questions   oo ps interview questions - javatpoint
201 core java interview questions oo ps interview questions - javatpoint
ravi tyagi
 

Similar to Encapsulation (20)

Core java training - Trainmesofttech
Core java training - TrainmesofttechCore java training - Trainmesofttech
Core java training - Trainmesofttech
 
Complete java
Complete javaComplete java
Complete java
 
Java Training in Chennai - Payilagam Syllabus
Java Training in Chennai - Payilagam SyllabusJava Training in Chennai - Payilagam Syllabus
Java Training in Chennai - Payilagam Syllabus
 
Java coursec content
Java coursec content Java coursec content
Java coursec content
 
Java J2EE Training in Chennai, Tambaram
Java J2EE  Training in Chennai, TambaramJava J2EE  Training in Chennai, Tambaram
Java J2EE Training in Chennai, Tambaram
 
Java training in pune course content By Advanto Software
Java training in pune course content By Advanto SoftwareJava training in pune course content By Advanto Software
Java training in pune course content By Advanto Software
 
Anuj java 11th batch_7 am pst
Anuj java  11th batch_7 am pstAnuj java  11th batch_7 am pst
Anuj java 11th batch_7 am pst
 
Session 38 - Core Java (New Features) - Part 1
Session 38 - Core Java (New Features) - Part 1Session 38 - Core Java (New Features) - Part 1
Session 38 - Core Java (New Features) - Part 1
 
Java Online Training Institute in Hyderabad - C-Point
Java Online Training Institute in Hyderabad - C-PointJava Online Training Institute in Hyderabad - C-Point
Java Online Training Institute in Hyderabad - C-Point
 
JAVA Training in Bangalore
JAVA Training in BangaloreJAVA Training in Bangalore
JAVA Training in Bangalore
 
201 core java interview questions oo ps interview questions - javatpoint
201 core java interview questions   oo ps interview questions - javatpoint201 core java interview questions   oo ps interview questions - javatpoint
201 core java interview questions oo ps interview questions - javatpoint
 
Java training in pune course content advanto software
Java training in pune course content  advanto softwareJava training in pune course content  advanto software
Java training in pune course content advanto software
 
java classes in pune
java classes in punejava classes in pune
java classes in pune
 
Java EE 6 & Spring: A Lover's Quarrel
Java EE 6 & Spring: A Lover's QuarrelJava EE 6 & Spring: A Lover's Quarrel
Java EE 6 & Spring: A Lover's Quarrel
 
From Java 6 to Java 7 reference
From Java 6 to Java 7 referenceFrom Java 6 to Java 7 reference
From Java 6 to Java 7 reference
 
Java training in hyderabad
Java training in hyderabadJava training in hyderabad
Java training in hyderabad
 
Java course training institute ameerpet hyderabad
Java course training institute ameerpet hyderabad Java course training institute ameerpet hyderabad
Java course training institute ameerpet hyderabad
 
Java, Ruby & Rails
Java, Ruby & RailsJava, Ruby & Rails
Java, Ruby & Rails
 
Java Training in Chennai | Advanced Java Training in chennai | J2EE Training ...
Java Training in Chennai | Advanced Java Training in chennai | J2EE Training ...Java Training in Chennai | Advanced Java Training in chennai | J2EE Training ...
Java Training in Chennai | Advanced Java Training in chennai | J2EE Training ...
 
Java Tutorial For Beginners - Step By Step | Java Basics | Java Certification...
Java Tutorial For Beginners - Step By Step | Java Basics | Java Certification...Java Tutorial For Beginners - Step By Step | Java Basics | Java Certification...
Java Tutorial For Beginners - Step By Step | Java Basics | Java Certification...
 

Encapsulation

  • 1. Contribute Us | Ask Question | login JavaTpoint Subscribe Us 91-9990449935 0120-4256464  Home  Core Java  Servlet  JSP  EJB  Struts2  Mail  Hibernate  Spring  Android  Design P  Quiz  Projects  Interview Q  Comment  Forum  Training Basics of Java Java OOPs Concepts Advantage of OOPsNaming ConventionObject and ClassMethod OverloadingConstructorstatic keywordthis keywordInheritance(IS-A)Aggregation(HAS-A)Method OverridingCovariant Return Typesuper keywordInstance Initializer blockfinal keywordRuntime PolymorphismDynamic Bindinginstanceof operatorAbstract classInterfaceAbstract vs InterfacePackageAccess ModifiersEncapsulationObject classObject CloningJava ArrayCall By Valuestrictfp keywordAPI DocumentCommand Line ArgArray Quiz-1Array Quiz-2OOPs Quiz-1OOPs Quiz-2OOPs Quiz-3OOPs Quiz-4 Java StringJava RegexException HandlingJava Inner classesMultithreadingSynchronizationI/OSerializationNetworkingAWTEvent HandlingSwingLayoutManagerAppletReflection APICollectionJDBCJava New FeaturesRMIInternationalization next »« prev
  • 2. Encapsulation in Java Encapsulation in java is a process of wrapping code and data together into a single unit, for example capsule i.e. mixed of several medicines. We can create a fully encapsulated class in java by making all the data members of the class private. Now we can use setter and getter methods to set and get the data in it. The Java Bean class is the example of fully encapsulated class. Advantage of Encapsulation in java By providing only setter or getter method, you can make the class read-only or write- only. It provides you the control over the data. Suppose you want to set the value of id i.e. greater than 100 only, you can write the logic inside the setter method. Simple example of encapsulation in java Let's see the simple example of encapsulation that has only one field with its setter and getter methods. 1. //save as Student.java 2. package com.javatpoint; 3. public class Student{ 4. private String name; 5. 6. public String getName(){ 7. return name; 8. } 9. public void setName(String name){ 10. this.name=name 11. } 12. } 1. //save as Test.java 2. package com.javatpoint;
  • 3. 3. class Test{ 4. public static void main(String[] args){ 5. Student s=new Student(); 6. s.setname("vijay"); 7. System.out.println(s.getName()); 8. } 9. } Compile By: javac -d . Test.java Run By: java com.javatpoint.Test Output: vijay Next TopicObject class in java « prevnext » Like/Subscribe us for latest updates or new sletter ↑Top Tutorials  » Core Java Tutorial  » Servlet Tutorial  » JSP Tutorial  » Mail API Tutorial  » Design Pattern Tutorial  » Struts Tutorial  » Spring Tutorial  » Hibernate Tutorial  » Android Tutorial  » JavaScript Tutorial  » SQL Tutorial
  • 4.  » C Tutorial  » AJAX Tutorial  » JUnit Tutorial  » JAXB Tutorial  » Maven Tutorial Interview Questions  » Java Interview  » Servlet Interview  » JSP Interview  » Hibernate Interview  » Spring Interview  » Android Interview  » SQL Interview  » PL/SQL Interview  » Oracle Interview  » MySQL Interview  » SQL Server Interview  » MongoDB Interview  » Cloud Interview Quizzes  » Core Java quiz  » Servlet quiz  » JSP quiz  » Struts2 quiz  » Android quiz  » OCJP quiz  » OCWCD quiz  » Hibernate quiz  » Spring quiz  » C quiz  » Cloud Computing quiz  » JavaScript quiz  » SQL quiz Forum
  • 5.  » Core Java Ques.  » Servlet Ques.  » JSP Ques.  » Struts Ques.  » Spring Ques.  » Hibernate Ques.  » Android Ques. Projects SSS IT PVT LTD  » Development  » Training  » SEO  » Consultancy CONTACT US Tel. : 0120-4256464 Mob. : +91 9990449935 Email : enquiry@sssit.org Address: Plot - 6, GT Road, Near MMX Mall, Mohan Nagar, Ghaziabad, 201007, UP, India » Contact Us » Privacy Policy © 2011-2014 Javatpoint. All Rights Reserved.