Thursday, 7 December 2017

What is the difference between Abstraction and Encapsulation in java?

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 Fruit {
    public abstract void eat();
}
class Apple extends Fruit{
               public void eat(){
                               System.out.println("Eating");
               }
}
public class Demo{
               public static void main(String args[]){
        Fruit f1 = new Fruit ();
       f1.eat();
    }
}

Output: Eating


No comments:

Post a Comment

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...