DEV Community

pranavinu
pranavinu

Posted on

Encapsulation

package:
1)It is like a folder
2)It can have multiple classes

Encapsulation:
1)In a class,it provides the security for the data
2)It is to control the security for data by
access modifier

access modifier:
1)It controls the access to the class,variables,constructor,method()
2)It has four types
*private
*default
*protected
*public

private acccess modifier:
1)it is keyword
2)In a package,it can access the variables, method() only inside the class,it can'access in other class

package devolepment;

public class a {

 private void method1()
{
    System.out.println("ravi");

}

 private a()
{
    System.out.println("mohan");
}



public static void main(String[] args) {

    a object1=new a();
    object1.method1();


}
Enter fullscreen mode Exit fullscreen mode

}

output:
ravi
mohan

package devolepment;

public class b extends a
{

public static void main(String[] args) {

    b object2=new b();
    object2.method1();


}
Enter fullscreen mode Exit fullscreen mode

}
output: it shows an error as not visible

default access modifier:
1)It is a empty keyword
2)It don't has any name for the keyword
3)It can acccess all the class in same package
4)It can't access in other packages

package devolepment;

public class b extends a
{

public static void main(String[] args) {
    b object2=new b();
    object2.method1();



}
Enter fullscreen mode Exit fullscreen mode

}

output:
ravi
mohan

package testing;

import devolepment.a;

public class testing extends a {

public static void main(String[] args) {


    testing object2=new testing();
    object2.method1();
}
Enter fullscreen mode Exit fullscreen mode

}

output: it shows error as (not visible) while access in a different package

protected:
1)It can access a any class of a same package
2)It should have a relation(extends),while accesssing a class of other package

package devolepment;

public class a {

  protected void method1()
{
    System.out.println("ravi");

}

  protected a()
{
    System.out.println("mohan");
}



public static void main(String[] args) {

    a object1=new a();
    object1.method1();


}
Enter fullscreen mode Exit fullscreen mode

}

output:
ravi
mohan

package testing;

import devolepment.a;

public class testing extends a {

public static void main(String[] args) {


    testing object2=new testing();
    object2.method1();
}
Enter fullscreen mode Exit fullscreen mode

}

output:
ravi
mohan

public
1)It is a keyword
2)It can access by any of a class in a different package
3)It can access without relation(extends)

package devolepment;

public class a {

  public void method1()
{
    System.out.println("ravi");

}

  public a()
{
    System.out.println("mohan");
}



public static void main(String[] args) {

    a object1=new a();
    object1.method1();


}
Enter fullscreen mode Exit fullscreen mode

}
output:
ravi
mohan

package testing;

import devolepment.a;

public class testing extends a{

public static void main(String[] args) {


    testing object2=new testing();
    object2.method1();
}
Enter fullscreen mode Exit fullscreen mode

}

output
ravi
mohan

Top comments (0)