Java Abstraction & Encapsulation


Abstraction

It shows necessary details of a class such like variables, methods, objects and etc (Ex : In Car Riding we just known about how to drive it and don't want to know about the inner process of that Car..). Here also same like that, we don't want to know about the inner process of variable, methods, objects and etc..

It's just a abstract of a Class.


Encapsulation

It provides Security for a Class through Access Specifiers.


Access Specifier Class Sub-Class Package Whole Application
Public yes yes yes yes
Protected yes yes yes no
Private yes no no no

  1. Use Access Specifier in Java
  2. Public Access Specifier
  3. Protected Access Specifier
  4. Private Access Specifier

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Use Access Specifier in Java


Encap_Ex1.java

public class Encap_Ex1 {
 
	public int a = 10;
	protected int b = 20;
	private int c = 30;
	
	void privateDisplay() {

		c = c+100;
		System.out.println("Private Variable : "+c);
	}
}
 
class SubClass extends Encap_Ex1 {
	
	void subDisplay() {
		
		a = a+100;
		b = b+100;
		
		System.out.println("Protected Variable : "+b);
		System.out.println("Public Variable : "+a);
	}
}

class MainClass {
	
	public static void main(String args[]) {
		
		SubClass subObj = new SubClass();
		
		subObj.privateDisplay();
		subObj.subDisplay();
	}
}

Sample Output

Private Variable : 130
Protected Variable : 120
Public Variable : 110




B. Public Access Specifier


Encap_Ex2.java

public class Encap_Ex2 {
 
	public int a;
	public float b;
	public String str;
	
	Encap_Ex2() {
		
		a = 10;
		b = (float)12.34;
		str = "Public Values !!!";		
	}
	
	public void display() {
		
		System.out.println("The Public Integer Value is : "+a);
		System.out.println("The Public Float Value is : "+b);
		System.out.println("The Public String Value is : "+str);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Encap_Ex2 obj = new Encap_Ex2();
		
		obj.display();
	}
}

Sample Output

The Public Integer Value is : 10
The Public Float Value is : 12.34
The Public String Value is : Public Values !!!




C. Protected Access Specifier


Encap_Ex3.java

public class Encap_Ex3 {
	
	protected int a;
	protected float b;
	protected String str;
	
	Encap_Ex3() {
		
		a = 111;
		b = (float)123.456;
		str = "Protected Values !!!";
	}
}
 
class SubClass extends Encap_Ex3 {
	
	public void display() {
		
		System.out.println("The Protected Integer Value is : "+a);
		System.out.println("The Protected Float Value is : "+b);
		System.out.println("The Protected String Value is : "+str);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		SubClass obj = new SubClass();
		
		obj.display();
	}
}

Sample Output

The Protected Integer Value is : 111
The Protected Float Value is : 123.456
The Protected String Value is : Protected Values !!!




D. Private Access Specifier


Encap_Ex4.java

public class Encap_Ex4 {
 
	private int a;
	private float b;
	private String str;
	
	Encap_Ex4() {
		
		a = 123;
		b = (float)52.38;
		str = "Private Values !!!";
	}
	
	private void get() {
		
		System.out.println("The Private Integer Value is : "+a);
		System.out.println("The Private Float Value is : "+b);
		System.out.println("The Private String Value is : "+str);
	}
	
	public void display() {
		
		get();
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Encap_Ex4 obj = new Encap_Ex4();
		
		obj.display();
	}
}

Sample Output

The Private Integer Value is : 123
The Private Float Value is : 52.38
The Private String Value is : Private Values !!!






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com