encapculatoin

0


  1. //her out put is null because we are not assigne value of string and intigers
    class student {
        int age;
        String name;

        public void show () {
             System.out.println( name +" " +age );
        }
    }

    public class encapsulation {
        public static void main(String[] args) {
            student obj = new student();
            obj.show();


           
        }
    }
     
  2. // here we are  get access methode creating new methode and privet ketword use for hide your data
    class student {
         private int age;
         private String name;

        public void setdata (){
            age = 19;
            name = "rakesh";
        }

        public void show () {
             System.out.println( name +" " +age );
        }
    }

    public class encapsulation1 {
        public static void main(String[] args) {
            student obj = new student();
            obj.setdata();
            obj.show();


           
        }
    }


  3. class student{

        int age;
        String name;

        public void show (){
            System.out.println(name + " " + age );
        }
        public void setdata1( int a ){
            age = a;

        }
          public void setdata2( String b ){
           name = b;

        }
    }


    public class encapculation2 {
        public static void main(String[] args) {
            student obj = new student();
            obj.setdata1(18);
            obj.setdata2("rakesh");
            obj.show();
       
           
        }
       
    }

  4. //if both variable and instance variable have same inside the
    //methode then it would result in same-class adn jvm he always give perfrance in
    //local variable this appraoch is called the "shadowing problem"

    class student{

        int age;
        String name;

        public void show (){
            System.out.println(name + " " + age );
        }
        public void setdata1( int age ){
            //age = age;
            //here

        }
          public void setdata2( String name ){
           //name = name;
           //here

        }
    }


    public class encapculation3 {
        public static void main(String[] args) {
            student obj = new student();
            obj.setdata1(18);
            obj.setdata2("rakesh");
            obj.show();
       
           
        }
       
    }

    //programme will be work but is not a right way for write a programme

  5. //shadowing problem solve using "this." methode

    class student{

        int age;
        String name;

        public void show (){
            System.out.println(name + " " + age );
        }
        public void setdata1( int age ){
            this.age = age;
         

        }
          public void setdata2( String name ){
           this.name = name;
         

        }
    }


    public class encapculaton4 {
        public static void main(String[] args) {
            student obj = new student();
            student obj1 = new student();
            obj1.setdata1(18);
            obj.setdata1(22);
            obj.setdata2("Amit");
            obj1.setdata2("rakesh");
            obj1.show();
           
            obj.show();
       
           
        }
       
    }
  6. //how to use getters and setters


    class student {
        int age;
        String Name;
        public void show (){
            System.out.println(Name +" " +age);
        }
        public void setAge(int age){
            this.age =age;
        }
        public int getAge(){
            return age;
        }
         public void setName(String Name){
            this.Name =Name;
        }
           public String getName(){
            return Name;
        }
    }



    public class encapsulaton5 {
        public static void main(String[] args) {
            student obj = new student();
            obj.setAge(25);
            obj.setName("Rahul");
           int sudent1Age = obj.getAge();
           System.out.println(sudent1Age);
           String sudent1Name = obj.getName();
           System.out.println(sudent1Name);
           
        }

     
       
    }

Post a Comment

0Comments
Post a Comment (0)