Define Abstraction and Encapsulation Give examples of both A
Define Abstraction and Encapsulation
Give examples of both.
Abstraction is hide from user. Because system is too complex to handle. Just show them interface of system and make them handle the system do what ever they want with that interface.
Encapsulation is a base of OOP. In this way we can bundling many types of data and its interface together.
Solution
Abstraction:
Abstraction is procedure of hiding the implementation details and illustrating only the functionality.
Abstraction in java is achieved by using interface and abstract class.
Encapsulation:
The whole design following encapsulation is to hide the implementation details from users. If a data member is private it means it can only be right to use within the same class. No outside class can access private data member of other class.
Abstraction Example:
class Employee extends Person {
private String codeEmp;
public String getCodeEmp () {
return codeEmp;
}
public void setCodeEmp(String codeEmp) {
this. codeEmp = CodeEmp;
}
}
abstract class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Main{
public static void main(String args[]){
Person person = new Employee();
person.setName(\"Rama\");
System.out.println(person.getName());
}
}
Encapsulation Example:
public class Demo{
private int ss;
private String empNam;
private int empAg;
public int getEmpSS(){
return ss;
}
public String getEmpNam(){
return empNam;
}
public int getEmpAg(){
return empAg;
}
public void setEmpAg(int newValue){
empAg = newValue;
}
public void setEmpNam(String newValue){
empNam = newValue;
}
public void setEmpSS(int newValue){
ss = newValue;
}
}
public class DemoTest{
public static void main(String args[]){
Demo obj = new Demo();
obj.setEmpNam(\"Seetha\");
obj.setEmpAg(20);
obj.setEmpSS(131557);
System.out.println(\"Employee Name: \" + obj.getEmpNam());
System.out.println(\"Employee SSN: \" + obj.getEmpSN());
System.out.println(\"Employee Age: \" + obj.getEmpAg());
}
}


