You are on page 1of 1

Tutorials Students Courses Custom Search Hire with us!

Login

Object Oriented Programming



(OOPs) Concept in Java
Encapsulation in Java
OOPs | Object Oriented Design Encapsulation is de ned as the wrapping up of data under a single unit. It is the
mechanism that binds together code and the data it manipulates.Other way to think
Dynamic Method Dispatch or about encapsulation is, it is a protective shield that prevents the data from being
Runtime Polymorphism in
accessed by the code outside this shield.
Java
Technically in encapsulation, the variables or data of a class is hidden from any
Overloading in Java other class and can be accessed only through any member function of own class
in which they are declared.
Overriding in Java As in encapsulation, the data in a class is hidden from other classes using the
data hiding concept which is achieved by making the members or methods of

Inheritance in Java class as private and the class is exposed to the end user or the world without
providing any details behind implementation using the abstraction concept, so it is
Java Object Creation of also known as combination of data-hiding and abstraction..
Inherited Class Encapsulation can be achieved by: Declaring all the variables in the class as
private and writing public methods in the class to set and get the values of
Java and Multiple Inheritance
variables.

Super Keyword in Java ▲


We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and
Got it!
understood our Cookie Policy & Privacy Policy
Using nal with Inheritance in
Java

Encapsulation in Java

Abstraction in Java

abstract keyword in java

Interfaces in Java

Abstract Classes in Java // Java program to demonstrate encapsulation


public class Encapsulate

Difference between Abstract


 {
// private variables declared
Class and Interface in Java  // these can only be accessed by
// public methods of class

new operator in Java


 private String geekName;
private int geekRoll;
private int geekAge;
Classes and Objects in Java
// get method for age to access
// private variable geekAge
Methods in Java public int getAge()
{
return geekAge;
}

// get method for name to access


// private variable geekName
public String getName()
{
return geekName;
}

// get method for roll to access


// private variable geekRoll
public int getRoll()
{
return geekRoll;
}

// set method for age to access


// private variable geekage
public void setAge( int newAge)
{ Most popular in Java
geekAge = newAge;
}
Provider.Service getClassName() method in
// set method for name to access Java with Examples
// private variable geekName
public void setName(String newName)
{ How String Hashcode value is calculated?
geekName = newName;
} Field setByte() method in Java with
Examples
// set method for roll to access
// private variable geekRoll
public void setRoll( int newRoll) Field setChar() method in Java with
{
Examples
geekRoll = newRoll;
}
} Field setDouble() method in Java with
Examples
In the above program the class EncapsulateDemo is encapsulated as the variables are
declared as private. The get methods like getAge() , getName() , getRoll() are set as
public, these methods are used to access these variables. The setter methods like
More related articles in Java
setName(), setAge(), setRoll() are also declared as public and are used to set the values
of the variables.
Field setShort() method in Java with
Examples

The program to access variables of the class EncapsulateDemo is shown below: Field isSynthetic() method in Java with
Examples

public class TestEncapsulation


{ Field set() method in Java with Examples
 public static void main (String[] args)
{
 Encapsulate obj = new Encapsulate(); How to Become A Successful Java
Developer?
 // setting values of the variables
obj.setName("Harsh");
obj.setAge(19); AlgorithmParameters getProvider() method
obj.setRoll(51); in Java with Examples

// Displaying values of the variables


System.out.println("Geek's name: " + obj.getName());
System.out.println("Geek's age: " + obj.getAge());
System.out.println("Geek's roll: " + obj.getRoll());

// Direct access of geekRoll is not possible


// due to encapsulation
// System.out.println("Geek's roll: " + obj.geekName);
}
}

Output:

Geek's name: Harsh


Geek's age: 19
Geek's roll: 51

Advantages of Encapsulation:

Data Hiding: The user will have no idea about the inner implementation of the
class. It will not be visible to the user that how the class is storing values in the
variables. He only knows that we are passing the values to a setter method and
variables are getting initialized with that value.
Increased Flexibility: We can make the variables of the class as read-only or write-
only depending on our requirement. If we wish to make the variables as read-only
then we have to omit the setter methods like setName(), setAge() etc. from the
above program or if we wish to make the variables as write-only then we have to
omit the get methods like getName(), getAge() etc. from the above program
Reusability: Encapsulation also improves the re-usability and easy to change with
new requirements.
Testing code is easy: Encapsulated code is easy to test for unit testing.

This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like
to contribute, you can also write an article using contribute.geeksforgeeks.org or mail
your article to contribute@geeksforgeeks.org. See your article appearing on the
GeeksforGeeks main page and help other Geeks.

Please write comments if you nd anything incorrect, or you want to share more
information about the topic discussed above.

Recommended Posts:
Difference between Abstraction and Encapsulation in Java with Examples
Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
Java.util.Collections.disjoint() Method in java with Examples
Java.util.concurrent.RecursiveTask class in Java with Examples
Java.util.concurrent.Phaser class in Java with Examples
Java.util.Collections.rotate() Method in Java with Examples
Java lang.Long.numberOfLeadingZeros() method in Java with Examples
Java lang.Long.lowestOneBit() method in Java with Examples
Java lang.Long.numberOfTrailingZeros() method in Java with Examples
Java.util.function.BiPredicate interface in Java with Examples
Java.lang.Short toString() method in Java with Examples
Java.util.function.IntPredicate interface in Java with Examples
Java.util.function.LongPredicate interface in Java with Examples
Java.util.concurrent.RecursiveAction class in Java with Examples
Java lang.Long.byteValue() method in Java with Examples

Improved By : sai003, CHANDRASEKHARVARMA

Article Tags : Java Java-Object Oriented

Practice Tags : Java


37

1.7
To-do Done Based on 50 vote(s)

Feedback/ Suggest Improvement Add Notes Improve Article

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Previous Next
 Basic Operators in Java Covariant return types in Java 

Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.

Load Comments

COMPANY LEARN PRACTICE CONTRIBUTE


About Us Algorithms Courses Write an Article
Careers Data Structures Company-wise Write Interview Experience
Privacy Policy Languages Topic-wise Internships
5th Floor, A-118,
Sector-136, Noida, Uttar Pradesh - 201305 Contact Us CS Subjects How to begin? Videos
feedback@geeksforgeeks.org Video Tutorials

@geeksforgeeks, Some rights reserved

You might also like