cunstructor

0

  1. //cunstructor

    class student{

        private int age;
        private String Name;
        student( int age ,String name){
            this.age=age;
            this.Name=name;

        }
        public int getAge(){
            return age;
        }
         public String getName(){
            return Name;
        }
    }

    public class cunstructor1 {
        public static void main(String[] args) {
            student obj = new student(18, "rakesh");
            int age=obj.getAge();
            System.out.println(age);
            String name= obj.getName();
            System.out.println(name);

           
        }
       
    }

  2. class student {
        private int age;
        private String Name;
        public student(){
            System.out.println("this constuctor parameter is zero");

        }

         public student (int age, String Name){
            this.age=age;
            this.Name=Name;

         }
         public void show(){
            System.out.println(age);
            System.out.println(Name);
         }
    }


    public class cunstructor2 {
        public static void main(String[] args) {
            student obj= new student( 18, "Rakesh");
            obj.show();
            //this custructor output will be null and 0 beacause in this cunstructor parameter is 0
            student obj1 = new student();
            obj1.show();
       
           
        }
       
    }
  3. //cunstuctor overloading

    class student{

        int Age;
        String Name;

        public student(){
            System.out.println("this custuter is 0 parameterize");
            Age = 19;
            Name = "Rakesh";
        }
            public student(int Age){
            this.Age=Age;
            Name = "Amit";
        }
            public student(int Age, String Name){
           this.Age=Age;
           this.Name=Name;
        }
        void show(){
            System.out.println(Name + " " +Age );
        }


    }



    public class cunstructor3 {
        public static void main(String[] args) {
            student obj= new student();
            obj.show();
             student obj2 = new student(21);
            obj2.show();
             student obj1 = new student(20,"Rahul");
            obj1.show();
           
           
           
        }
       
    }
  4. //cunstuctor overloading

    class student{

        int Age;
        String Name;

        public student(){
            System.out.println("this custuter is 0 parameterize");
            Age = 19;
            Name = "Rakesh";
        }
            public student(int Age){
                 
            this.Age=Age;
            Name = "Amit";
        }
            public student(int Age, String Name){
           this.Age=Age;
           this.Name=Name;
        }
        void show(){
            System.out.println(Name + " " +Age );
        }


    }



    public class cunstructor4 {
        public static void main(String[] args) {
            student obj= new student();
            obj.show();
             student obj2 = new student(21);
            obj2.show();
             student obj1 = new student(20,"Rahul");
            obj1.show();
           
           
           
        }
       
    }

 

Post a Comment

0Comments
Post a Comment (0)