Abstraction and Encapsulation are basically interdependent with each other.If you want to achieve Abstraction then Encapsulation should use for it and same case other.
Lets take look on example by Java Coding
abstract class Person{
private int personId;
private String name;
public int getPersonId() {
return personId;
}
public String getName() {
return name;
}
public void setPersonId(int personId) {
this.personId = personId;
}
public void setName(String name) {
this.name = name;
}
public abstract double earning();
}//end of abstract class
public class Employee extends Person{
private double salary;
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public double earning() {
return this.salary;
}
}
- Abstraction mean we know what is this but don't know how is this and Encapsulation mean we hide information from user.
- Abstraction come from abstract classes or interface and Encapsulation come from
access modifier + getter/setter method.
- Abstraction give you Polymorphism and Encapsulation give you information hiding.
Lets take look on example by Java Coding
abstract class Person{
private int personId;
private String name;
public int getPersonId() {
return personId;
}
public String getName() {
return name;
}
public void setPersonId(int personId) {
this.personId = personId;
}
public void setName(String name) {
this.name = name;
}
public abstract double earning();
}//end of abstract class
public class Employee extends Person{
private double salary;
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public double earning() {
return this.salary;
}
}
- Abstraction mean we know what is this but don't know how is this and Encapsulation mean we hide information from user.
- Abstraction come from abstract classes or interface and Encapsulation come from
access modifier + getter/setter method.
- Abstraction give you Polymorphism and Encapsulation give you information hiding.
No comments:
Post a Comment