Constructor and Object in Java

Different Types Of Constructor in Java ... Default Constructor,Pramiterized Constructor ,,Over-loaded Constructor:-

package oop;
public class Oop {

    public int ID;
    public String name;
    public char bloodgroup;
   
    Oop(){
        System.out.println("This is the Default Constructor");
    }
    Oop(int a){
        System.out.println("This is a prametrized Constructor No= "+a);
    }
    Oop(String a){
        System.out.println("This is an other Constructor name= "+a);
    }
   
    public static void main(String[] args) {
        Oop obj1=new Oop();
        Oop obj2=new Oop(2015);
        Oop obj3=new Oop("3rd Constructor");
       
        obj1.ID=34;
        obj1.name="usman";
        obj1.bloodgroup='a';
               
    }
}

=================================
package usman1;
public class Usman1 {
    int age;
    String name;
    public Usman1(int a,String b){
age=a;
name=b;
System.out.println("Name= "+name+" age= "+age);
    }
    public static void main(String[] args) {
     Usman1 obj=new Usman1(20,"Usman Siddique");
    }  
    }

No comments

Powered by Blogger.