Constructors.16

288 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
288
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • http://improvejava.blogspot.in
  • http://improvejava.blogspot.in
  • http://improvejava.blogspot.in
  • http://improvejava.blogspot.in
  • http://improvejava.blogspot.in
  • http://improvejava.blogspot.in
  • Constructors.16

    1. 1. Concept of Constructors and Types of Constructors http://improvejava.blogspot.in/ 1
    2. 2. ObjectivesOn completion of this period, you would be able to know• What is constructor• How to implement constructors• Different types of constructors http://improvejava.blogspot.in/ 2
    3. 3. Recap• We have discussed in the previous class • ‘new’ operator • Usage of ‘new’ operator • Methods in a class http://improvejava.blogspot.in/ 3
    4. 4. Concept of Constructors• Constructor is special member of the class• It initializes an object immediately upon creation• It has the same name of the class• It is automatically called after the object is create, before the ‘new’ operator completes• It has no return type, not even void http://improvejava.blogspot.in/
    5. 5. Concept of Constructors• Example program class Point{ int x,y; Point(int x, int y){ this.x = x; this.y = y; }• Class name is Point• It has 2 instance variables : x and y• A constructor is Point defined : Point(int x, int y)• The constructor initializes the instance variables x and y http://improvejava.blogspot.in/ 5
    6. 6. Concept of Constructors class PointCreate{ public static void main(String args[]){ Point p = new Point(10,20); System.out.println(“x=“+p.x + “y=“+p.y); } }• The object name is ‘p’• It is created using the constructor• The constructor sets the values of x and y to 10 and 20 respectively 6
    7. 7. Use of Constructors• Constructors are used • if we know the specific value to be assigned to a data member when an object is created • to initialize the data member of the class, when an object is created http://improvejava.blogspot.in/
    8. 8. Difference Between Classes WITHOUT CONSTRCTOR WITH CONSTRUCTORclass A { class A { int x; // data member int x; // data member void set_x() { public A() { // constructor x = 10; x = 10; } } void display_x() { void display_x(){// method display System.out.println(“x value is:”+x) ; System.out.println(“x value is:”+x) ; } } } // end of class A } // end of class Apublic class B{ public static void main (String args[ ]) public class B { { public static void main (String args[ A ref_a = new A(); ]) { ref_a.set_x(); // calling set _x A ref_a = new A(); ref_a.display_x();// calling disp ref_a.display_x(); } }} // end of class B } // end of class B
    9. 9. Comparison• Without Constructor • Output of the above program is : x value is 10 • In the above program constructor is NOT used to initialize the data member x • But an explicit method (set_x) is used to initialize the data member x , and also the set_x method invoked explicitly (ref_a.set_x).
    10. 10. Comparison contd..• With Constructor • Output of the above program is : x value is 10 • In the above program constructor is used to initialize the data member x • No explicit invoking statement is used to call the constructor • Except ( A ref_a = new A()) 10
    11. 11. Types of Constructors• Default constructors• Explicit constructors• Parameterized constructors• Overloaded constructors http://improvejava.blogspot.in/ 11
    12. 12. Default constructorWhat is default constructor ?• Compiler creates the default constructor when user has not defined a constructor in a class• It is also called as “do-nothing” or no-operation constructor http://improvejava.blogspot.in/
    13. 13. Example On Default Constructor class A { • no explicit constructor provided so the compiler will provide for you // public • it will be like A() { } public A() { } • is called do –nothing constructor or no } // end class A operation constructor or default constructorclass B { public static void main(Strins[] args) { A ref_a = new A(); } // end of main } // end of class B • Output of this program is : No output, because default constructor will do nothing http://improvejava.blogspot.in/ 13
    14. 14. Example on Explicit Constructor• When a user defines a constructor in a class, • the compiler will not provide any default constructorclass A { public A() { System.out.println(“ constructor provided by programmer”); } } class B { public static void main(Strins[] args) { A ref_a = new A(); } }Output of this program is http://improvejava.blogspot.in/ by programmer : constructor provided 14
    15. 15. Parameterized Constructor • The constructors can have parameters, those constructors are called “parameterized constructors”class A { int i; // data member of class A public A(int fp) { // constructor i = fp; } // end of constructor A void display_x() { // to display me System.out.println(“i value is: ”+i); } // end of display method } // end of class A http://improvejava.blogspot.in/ 15
    16. 16. Parameterized Constructor contd..class B { public static void main(Strins[] args) { A ref_a = new A(10); // 10 is a.p ref_a.display_x(); } // end of main } // end of class B • Output of the above program is : i value is: 10 • In the above example the compiler will not provide any constructor http://improvejava.blogspot.in/ 16
    17. 17. Overloaded Constructor• If class contain one or more constructors, this is called as ‘constructer overloading’• If you have more than one constructor in a class, the constructor must have different argument lists• The argument list includes the order and type of the arguments• In the following example class A consist of three constructors• These constructors are called or invoked automatically when the objects are created with the respective arguments http://improvejava.blogspot.in/ 17
    18. 18. Example For Overloaded Constructorclass A { int i; boolean b; A() { System. out. println (“ No argument constructor ”); } A( int fp ) { i=fp; S.o.p(“ one argument constr. of int”); } A( boolean fp1, int fp2 ) { b=fp1; i = fp2; S.o.p(“two argument constr. of boolean and int”);}} // end of class 18
    19. 19. Example For Overloaded Constructor contd..public class B { public static void main (String args[]) { A a1 = new A(); // calls no-argument constructor A a2 = new A(10);// calls one argument constructor A a3 = new A(true,10);// call two argument constructor }} // end of class B 19
    20. 20. Another Example For Overloaded Constructorclass A { int i; boolean b; A(boolean fp1, int fp2) { b=fp1; i = fp2; System. Out. println (“two argument constr. of boolean and int”); } A( int fp1, boolean fp2) { i = fp1; b=fp2;System. out. println(“two argument constr. of int and boolean”); }} // end of class A 20
    21. 21. Another Example For Overloaded Constructor contd.. public class B { public static void main .(String args[]) { A a1 = new A(10, true); A a2 = new A( false, 20); } } // end of class B 21
    22. 22. Discussion• What are the differences between method and constructor 1. Name • Class and constructor have the same name • Different names 1. Return type • Constructor have no return type • Methods have return type http://improvejava.blogspot.in/
    23. 23. Summary• In this class, we have discussed • Concepts of constructor • How constructors are created ? • Types of constructors • Default • Explicit • Parameterized • Overloaded http://improvejava.blogspot.in/
    24. 24. Quiz1. Default constructors provided by? a. Developer b. User c. JVM http://improvejava.blogspot.in/ 24
    25. 25. Quiz Contd..2. Overloaded constructor has? a. same name b. different names http://improvejava.blogspot.in/
    26. 26. Frequently Asked Questions• Define Constructor?• What is default constructor ?• When constructor method is executed?• What is overloaded constructor ?• Why do you need to write a constructor if the compiler writes one for you?• How can you differentiate a constructor from a method?• Can you also have a method that’s the same name as the class?
    27. 27. Assignment1. Write a class called Student consisting of pin_no, sname, fname, city as instance variables, have a constructor in your class that by default assigns some values to the instance variables and disp_stu_details as methods Create an object for the above class, and access display method to display the values of instance variables 27
    28. 28. Assignment Contd..2. Perform the above operation using parameterized constructor3. Perform the above operation using overloaded constructors for the two student objects 28

    ×