Java Programing laungage

Core Java Tutorial

Introduction of Core Java

How To Install JDk and Set of Path

Syntax of java Program

Difference between Java and C/C++

Advantage and Disadvantage of Java

What is Java

Why Java is not Pure Object Oriented Language

Java has Following Features/Characteristics

Limitation of Java Language and Java Internet

Common Misconception about Java

Simple Program of Java

Integrated Development Environment in java

Compile and Run Java Program

Applet and Comments in Java

Tokens in Java

Keywords in Java

Identifier and Variables in Java

Literals/Constants

Data Type in Java

Assignments and Initialization in Java

Operators in Java

Rule of Precedence in Java

Operator on Integer and Separators in Java Programming

Java Control Flow of Statements

If and If-else Selection Statement

Nested If-else and If-else-If Selection Statement

switch case and conditional operator Selection Statement

for and while Loop

do..while and for each Loop

break and labeled break statement

continue and labeled continue statement

return Statement and exit() Method

Escape Sequence for Special Characters and Unicode Code

Constants and Block or Scope

Statement in Java

Conversions between Numeric Types in Java

Import Statement in Java

User Input in Java using Scanner Class

User Input in Java using Console Class

Array in Java

One Dimensional Array

Two Dimensional Array

Two Dimensional Array Program

Command Line Argument in Java

String args Types in Java

Uneven/Jagged array in java

Math Class Function and Constant

Math Class all Function used in a program

Enumerated Types in Java

Object Oriented Programming v/s Procedural Programming

Object Oriented Programming Concepts in Java

Introduction to Class,Object and Method in Java

Class Declaration in Java

Class & Objects in java

Encapsulation in Java

Modifiers/Visibility for a Class or Interrface or member of a Class

Polymorphism in Java

Runtime polymorphism (dynamic binding or method overriding)

adplus-dvertising
Encapsulation in Java
Previous Home Next

Encapsulation is a bundling of member variable and member method together in a class as a single entity is known as called as encapsulation. Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. For this reason, encapsulation is also referred to as data hiding. for example capsule i.e. mixed of several medicines. Data Hiding is declaring a member has private we can hide them from external objects. The Java Bean class is the example of fully encapsulated class.

Benefits of Encapsulation:

The fields of a class can be made read-only or write-only.

A class can have total control over what is stored in its fields.

The users of a class do not know how the class stores its data. A class can change the data type of a field and users of the class do not need to change any of their code.

Example :

import java.util.Scanner;
class B
{
	private int x;
	private float y;
	void getData()//for user input
	{
		Scanner sc;
		sc=new Scanner(System.in);
		System.out.print("Enter Value Of x :");
		x=sc.nextInt();
		System.out.print("Enter value of y :");
		y=sc.nextFloat();
	} 
	void setdata(int x1,float y1)//for testing purpose
	{
		x=x1;
		y=y1;
	}
	void display()
	{
		System.out.println(x+","+y);
	}
}
class BTest
{
	public static void main(String args[])
	{ 
		B a1,a2;
		a1=new B();
		a2=new B();
		//a1.x=5 ;//error
		//a1.getdata();
		//a2.gatdata();
		a1.setdata(10,10.5f);
		a2.setdata(2,5.4f);
		a1.display();
		a2.display();
	}
}

output :

10,10.5

2,5.4
 
Previous Home Next