Encapsulation in Java - Javatpoint

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Custom Search

Home Java C C++ C# Servlet JSP EJB Struts2 Mail Hibernate Spring Android Design P Quiz Projects Interview Q Comment Forum

Java Training
Basics of Java

Java Object Class


Java OOPs Concepts
Naming Convention
Object and Class
Constructor
static keyword
Encapsulation in Java ← prev next →

this keyword
Encapsulation in Java is a process of wrapping code and data together into a single unit, for example, a capsule which is mixed
Java Inheritance of several medicines.
Inheritance(IS-A)
Aggregation(HAS-A) 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.
Java Polymorphism
Method Overloading The Java Bean class is the example of a fully encapsulated class.
Method Overriding
Covariant Return Type
super keyword
Instance Initializer block
final keyword
Runtime Polymorphism
Dynamic Binding
instanceof operator

Java Abstraction
Abstract class
Interface
Abstract vs Interface

Java Encapsulation Advantage of Encapsulation in Java


Package
By providing only a setter or getter method, you can make the class read-only or write-only. In other words, you can skip the
Access Modifiers
getter or setter methods.
Encapsulation

Java Array It provides you the control over the data. Suppose you want to set the value of id which should be greater than 100 only, you
Java Array can write the logic inside the setter method. You can write the logic not to store the negative numbers in the setter methods.

Java OOPs Misc It is a way to achieve data hiding in Java because other class will not be able to access the data through the private data
Object class members.
Object Cloning
The encapsulate class is easy to test. So, it is better for unit testing.
Math class
Wrapper Class
The standard IDE's are providing the facility to generate the getters and setters. So, it is easy and fast to create an
Java Recursion encapsulated class in Java.
Call By Value
strictfp keyword
javadoc tool
Command Line Arg
Object vs Class
Overloading vs Overriding

Java String

Java Regex

Exception Handling

Java Inner classes

Java Multithreading
10% OFF on Royalty-free Videos
Java I/O

Java Networking

Java AWT & Events Simple Example of Encapsulation in Java


Java Swing
Let's see the simple example of encapsulation that has only one field with its setter and getter methods.
JavaFX

Java Applet File: Student.java


Java Reflection

Java Date //A Java class which is a fully encapsulated class.

Java Conversion //It has a private data member and getter and setter methods.

Java Collection package com.javatpoint;


public class Student{
Java JDBC
//private data member
Java New Features
private String name;
RMI
//getter method for name
Internationalization
public String getName(){
Interview Questions
return name;
}
//setter method for name
public void setName(String name){
this.name=name
}
}

File: Test.java

//A Java class to test the encapsulated class.


package com.javatpoint;
class Test{
public static void main(String[] args){
//creating instance of the encapsulated class
Student s=new Student();
//setting value in the name member
s.setName("vijay");
//getting value of the name member
System.out.println(s.getName());
}
}

Compile By: javac -d . Test.java


Run By: java com.javatpoint.Test

Output:

vijay

Read-Only class

//A Java class which has only getter methods.


public class Student{
//private data member
private String college="AKG";
//getter method for college
public String getCollege(){
return college;
}
}

Now, you can't change the value of the college data member which is "AKG".

s.setCollege("KITE");//will render compile time error

Write-Only class

//A Java class which has only setter methods.


public class Student{
//private data member
private String college;
//getter method for college
public void setCollege(String college){
this.college=college;
}
}

Now, you can't get the value of the college, you can only change the value of college data member.

System.out.println(s.getCollege());//Compile Time Error, because there is no such method


System.out.println(s.college);//Compile Time Error, because the college data member is private.
//So, it can't be accessed from outside the class

Another Example of Encapsulation in Java


Let's see another example of encapsulation that has only four fields with its setter and getter methods.

File: Account.java

//A Account class which is a fully encapsulated class.


//It has a private data member and getter and setter methods.
class Account {
//private data members
private long acc_no;
private String name,email;
private float amount;
//public getter and setter methods
public long getAcc_no() {
return acc_no;
}
public void setAcc_no(long acc_no) {
this.acc_no = acc_no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public float getAmount() {
return amount;
}
public void setAmount(float amount) {
this.amount = amount;
}

File: TestAccount.java

//A Java class to test the encapsulated class Account.


public class TestEncapsulation {
public static void main(String[] args) {
//creating instance of Account class
Account acc=new Account();
//setting values through setter methods
acc.setAcc_no(7560504000L);
acc.setName("Sonoo Jaiswal");
acc.setEmail("sonoojaiswal@javatpoint.com");
acc.setAmount(500000f);
//getting values through getter methods
System.out.println(acc.getAcc_no()+" "+acc.getName()+" "+acc.getEmail()+" "+acc.getAmount());
}
}

Test it Now

Output:

7560504000 Sonoo Jaiswal sonoojaiswal@javatpoint.com 500000.0

Next Topic Object class in java

← prev next →

Help Others, Please Share

Learn Latest Tutorials

UML Artificial ES6 Tutorial Flutter Selenium Firebase


Tutorial Neural Tutorial Python Tutorial
Network ES6
UML Tutorial Flutter Selenium Py Firebase

ANN

Cobol Ansible Mockito Talend Microsoft Sharepoint


Tutorial Tutorial Tutorial Tutorial Azure Tutorial Tutorial

Cobol Ansible Mockito Talend Azure SharePoint

Preparation

Aptitude Logical Verbal Interview Company


Reasoning Ability Questions Interview
Aptitude Questions
Reasoning Verbal A. Interview
Company

Trending Technologies

Artificial AWS Selenium Cloud Hadoop ReactJS


Intelligence Tutorial tutorial tutorial tutorial Tutorial
Tutorial
AWS Selenium Cloud Hadoop ReactJS
AI

Data Science Angular 7 Blockchain Git Tutorial Machine DevOps


Tutorial Tutorial Tutorial Learning Tutorial
Git Tutorial
D. Science Angular 7 Blockchain DevOps
ML

B.Tech / MCA

DBMS Data DAA tutorial Operating Computer Compiler


tutorial Structures System tutorial Network Design tutorial
tutorial DAA tutorial
DBMS OS Compiler D.
DS C. Network

Computer Discrete Ethical Computer Software html tutorial


Organization Mathematics Hacking Graphics Engineering
and Tutorial Tutorial Tutorial Tutorial Web Tech.
Architecture
D. Math. E. Hacking C. Graphics Software E.
COA

Cyber Automata C Language C++ tutorial Java tutorial .Net


Security Tutorial tutorial Framework
tutorial C++ Java tutorial
Automata C
Cyber Sec. .Net

Python List of Control Data Mining


tutorial Programs Systems Tutorial
tutorial
Python Programs Data Mining
Control S.

Javatpoint Services
JavaTpoint offers too many high quality services. Mail us on hr@javatpoint.com, to get more information about given services.

Website Designing
Website Development
Java Development
PHP Development
WordPress
Graphic Designing
Logo
Digital Marketing
On Page and Off Page SEO
PPC
Content Development
Corporate Training
Classroom and Online Training
Data Entry

Training For College Campus


JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at hr@javatpoint.com.
Duration: 1 week to 2 week

Like/Subscribe us for latest updates or newsletter

LEARN TUTORIALS OUR WEBSITES OUR SERVICES CONTACT

Learn Java Javatpoint.com Website Development Address: G-13, 2nd Floor, Sec-3
Learn Data Structures Hindi100.com Android Development Noida, UP, 201301, India
Learn C Programming Lyricsia.com
Website Designing Contact No: 0120-4256464, 9990449935
Learn C++ Tutorial Quoteperson.com
Learn C# Tutorial Jobandplacement.com Digital Marketing Contact Us
Learn PHP Tutorial Summer Training Subscribe Us
Learn HTML Tutorial Privacy Policy
Industrial Training
Learn JavaScript Tutorial Sitemap
Learn jQuery Tutorial College Campus Training
Learn Spring Tutorial

© Copyright 2011-2018 www.javatpoint.com. All rights reserved. Developed by JavaTpoint.

You might also like