Abstraction
|
Encapsulation
|
Abstraction means providing important or necessary features without giving internal or background details.
|
Encapsulation means wrapping or hiding of data in a class
and controlling its access.
|
Abstraction is implemented in Java using interface and abstract class
|
Encapsulation is implemented using private, package-private and protected access
modifier.
|
Provides protection
|
Provides
Security
|
hiding implementation
|
hiding data
|
For Example: - When user is using Mobile application
he doesn't know internal working of all functions like calling, send Sms but he
can call or send sms without knowing internal details. His main aim is using the mobile phone not to know about internal structure
|
For Example: Mobiles and owners...
all the functions of mobiles are encapsulated with the owners. No one else can access it. |
class EncapDemo{
private String
empName;
private int
empAge;
//Getter and
Setter methods
public String
getEmpName(){
return
empName;
}
public int
getEmpAge(){
return
empAge;
}
public void
setEmpAge(int newValue){
empAge =
newValue;
}
public void setEmpName(String
newValue){
empName =
newValue;
}
}
public class EncapTest{
public static void
main(String args[]){
EncapDemo
obj = new EncapDemo();
obj.setEmpName("Debasish
");
obj.setEmpAge(24);
System.out.println("Employee
Name: " + obj.getEmpName());
System.out.println("Employee
Age: " + obj.getEmpAge());
}
}
Output:
Employee
Name:Debasish
Employee
Age: 24
|
abstract
}
class
} public
}
Output: Eating
|
Thursday, 7 December 2017
What is the difference between Abstraction and Encapsulation in java?
Subscribe to:
Post Comments (Atom)
apply function in R
1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...
-
a)Structural diagrams Structure diagrams depict the static structure of the elements in your system.It shows the things in the system - ...
-
index.html <html> <head><title>Sum of two numbers</title></head> <body> <form method="pos...
-
Write a program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer, calculate percentage and grade acc...
No comments:
Post a Comment