Tuesday, 30 August 2016

Param-con.

1.       Parameterized constructor
If we provide any constructor with at least one parameter then this constructor is called as “parameterized constructor”.

Ex code :- 

class Student
{
        String sid;
        String sname;
        String saddr;
        Student()
        {
                        sid="S-111";
                        sname="AAA";
                        saddr="Hyderabad";
        }
        public void getStudentDetails()
        {
                        System.out.println("Student Details");
                        System.out.println("----------------");
                        System.out.println("Student Id     :"+sid);
                        System.out.println("Student Name   :"+sname);
                        System.out.println("Student Address:"+saddr);
        }
}
class Test
{
        public static void main(String[] args)
        {
                        Student std=new Student();
                        std.getStudentDetails();
        }
}

Output:-
Student Details
----------------
Student Id     :S-111
Student Name   :AAA
Student Address:Hyderabad


Date july-26-2016
   In the above program, we are provided initialization for class level variables inside the 0-args constructor, it will provide same data to all the objects which we are creating to the same class.
As per the requirements, if we want to create multiple object for the same class with different data then we have to use parameterized constructor.
Ex code :-
class Student
{
                String sid;
                String sname;
                String saddr;
                Student(String sid1, String sname1, String saddr1)
                {
                                sid=sid1;
                                sname=sname1;
                                saddr=saddr1;
                }
                public void getStudentDetails()
                {
                                System.out.println("Student Details");
                                System.out.println("----------------");
                                System.out.println("Student Id     :"+sid);
                                System.out.println("Student Name   :"+sname);
                                System.out.println("Student Address:"+saddr);
                }
}
public class Test
{
                public static void main(String[] args)
                {
                                Student std1=new Student("S-111","Sai","Hyderabad");
                                std1.getStudentDetails();
                                System.out.println();
                                Student std2=new Student("S-222","Teja","Palamaner");
                                std2.getStudentDetails();
                                System.out.println();
                                Student std3=new Student("S-333","Balakrishna","Hyderabad");
                                std3.getStudentDetails();
                                System.out.println();
                }
}
Output :-
Student Details
----------------
Student Id     :S-111
Student Name   :AAA
Student Address:Hyderabad

Student Details
----------------
Student Id     :S-222
Student Name   :Teja
Student Address:Palamaner

Student Details
----------------
Student Id     :S-333
Student Name   :Balakrishna
Student Address:Hyderabad

Constructor overloading :-
If we declare more then one constructor with the same name and with the different parameter list then it is called as “constructor overloading”.
Ex code:-
class A
{
                int i,j,k;
                A(int i1)
                {
                                i=i1;
                }
                A(int i1, int j1)
                {
        i=i1;
        j=j1;
                }
                A(int i1, int j1, int k1)
                {
                                i=i1;
                                j=j1;
                                k=k1;
                }
                public void add()
                {
                                System.out.println("Addition :"+(i+j+k));
                }
}
class Test
{
                public static void main(String[] args)
                {
                                A a1=new A(10);
                                a1.add();
                                A a2=new A(10,20);
                                a2.add();
                                A a3=new A(10,20,30);
                                a3.add();
                }
}

Output:-
Addition :10
Addition :30

Addition :60

No comments:

Post a Comment