SlideShare ist ein Scribd-Unternehmen logo
1 von 28
Concept of Constructors and Types of Constructors




                          http://improvejava.blogspot.in/
                                                            1
Objectives

On 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
Recap
• We have discussed in the previous class
   • ‘new’ operator
   • Usage of ‘new’ operator
   • Methods in a class




                     http://improvejava.blogspot.in/
                                                       3
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/
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
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
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/
Difference Between Classes
        WITHOUT CONSTRCTOR                              WITH CONSTRUCTOR
class 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 A
public 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
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).
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
Types of Constructors

•   Default constructors
•   Explicit constructors
•   Parameterized constructors
•   Overloaded constructors




      http://improvejava.blogspot.in/
                                        11
Default constructor

What 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/
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 constructor
class 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
Example on Explicit Constructor
• When a user defines a constructor in a class,
   • the compiler will not provide any default constructor
class 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
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
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
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
Example For Overloaded Constructor
class 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
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
Another Example For Overloaded Constructor
class 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
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
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/
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/
Quiz
1. Default constructors provided by?

     a. Developer
     b. User
     c. JVM




                    http://improvejava.blogspot.in/
                                                      24
Quiz             Contd..

2. Overloaded constructor has?
    a. same name
     b. different names




                  http://improvejava.blogspot.in/
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?
Assignment

1. 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
Assignment       Contd..



2. Perform the above operation using parameterized
   constructor

3. Perform the above operation using overloaded
   constructors for the two student objects




                                                     28

Weitere ähnliche Inhalte

Was ist angesagt?

C++ Constructor destructor
C++ Constructor destructorC++ Constructor destructor
C++ Constructor destructorDa Mystic Sadi
 
11 constructors in derived classes
11 constructors in derived classes11 constructors in derived classes
11 constructors in derived classesDocent Education
 
Core Java Certification
Core Java CertificationCore Java Certification
Core Java CertificationVskills
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorJussi Pohjolainen
 
Friend this-new&delete
Friend this-new&deleteFriend this-new&delete
Friend this-new&deleteShehzad Rizwan
 
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)Gandhi Ravi
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cppgourav kottawar
 
Object Oriented Programming With C++
Object Oriented Programming With C++Object Oriented Programming With C++
Object Oriented Programming With C++Vishnu Shaji
 
OOP in C - Before GObject (Chinese Version)
OOP in C - Before GObject (Chinese Version)OOP in C - Before GObject (Chinese Version)
OOP in C - Before GObject (Chinese Version)Kai-Feng Chou
 
Constructor and destructor in oop
Constructor and destructor in oop Constructor and destructor in oop
Constructor and destructor in oop Samad Qazi
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++Swarup Kumar Boro
 
Constructor and desturctor
Constructor and desturctorConstructor and desturctor
Constructor and desturctorSomnath Kulkarni
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and DestructorSunipa Bera
 
class and objects
class and objectsclass and objects
class and objectsPayel Guria
 

Was ist angesagt? (20)

C++ Constructor destructor
C++ Constructor destructorC++ Constructor destructor
C++ Constructor destructor
 
Oop objects_classes
Oop objects_classesOop objects_classes
Oop objects_classes
 
11 constructors in derived classes
11 constructors in derived classes11 constructors in derived classes
11 constructors in derived classes
 
Core Java Certification
Core Java CertificationCore Java Certification
Core Java Certification
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Constructor and destructor
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
 
Friend this-new&delete
Friend this-new&deleteFriend this-new&delete
Friend this-new&delete
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
 
Object Oriented Programming With C++
Object Oriented Programming With C++Object Oriented Programming With C++
Object Oriented Programming With C++
 
OOP in C - Before GObject (Chinese Version)
OOP in C - Before GObject (Chinese Version)OOP in C - Before GObject (Chinese Version)
OOP in C - Before GObject (Chinese Version)
 
Constructor and destructor in oop
Constructor and destructor in oop Constructor and destructor in oop
Constructor and destructor in oop
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 
C#2
C#2C#2
C#2
 
Dot Net csharp Language
Dot Net csharp LanguageDot Net csharp Language
Dot Net csharp Language
 
Constructor and desturctor
Constructor and desturctorConstructor and desturctor
Constructor and desturctor
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
class and objects
class and objectsclass and objects
class and objects
 

Ähnlich wie Constructors.16

Introductions to Constructors.pptx
Introductions to Constructors.pptxIntroductions to Constructors.pptx
Introductions to Constructors.pptxSouravKrishnaBaul
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptxRassjb
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCEVenugopalavarma Raja
 
constructor in object oriented program.pptx
constructor in object oriented program.pptxconstructor in object oriented program.pptx
constructor in object oriented program.pptxurvashipundir04
 
Constructor and Destructor.pdf
Constructor and Destructor.pdfConstructor and Destructor.pdf
Constructor and Destructor.pdfMadnessKnight
 
Constructors and destructors in C++
Constructors and destructors in  C++Constructors and destructors in  C++
Constructors and destructors in C++RAJ KUMAR
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdfstudy material
 
Constructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfConstructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfLadallaRajKumar
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and DestructorsKeyur Vadodariya
 
Padroes Projeto
Padroes ProjetoPadroes Projeto
Padroes Projetolcbj
 
object oriented programming language.pptx
object oriented programming language.pptxobject oriented programming language.pptx
object oriented programming language.pptxsyedabbas594247
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxDeepasCSE
 
constructors.pptx
constructors.pptxconstructors.pptx
constructors.pptxEpsiba1
 

Ähnlich wie Constructors.16 (20)

Introductions to Constructors.pptx
Introductions to Constructors.pptxIntroductions to Constructors.pptx
Introductions to Constructors.pptx
 
Oops
OopsOops
Oops
 
Constructor
ConstructorConstructor
Constructor
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptx
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
 
constructor in object oriented program.pptx
constructor in object oriented program.pptxconstructor in object oriented program.pptx
constructor in object oriented program.pptx
 
Constructor and Destructor.pdf
Constructor and Destructor.pdfConstructor and Destructor.pdf
Constructor and Destructor.pdf
 
Constructors and destructors in C++
Constructors and destructors in  C++Constructors and destructors in  C++
Constructors and destructors in C++
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdf
 
Constructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfConstructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdf
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Constructor,destructors cpp
Constructor,destructors cppConstructor,destructors cpp
Constructor,destructors cpp
 
Tut Constructor
Tut ConstructorTut Constructor
Tut Constructor
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
 
Padroes Projeto
Padroes ProjetoPadroes Projeto
Padroes Projeto
 
object oriented programming language.pptx
object oriented programming language.pptxobject oriented programming language.pptx
object oriented programming language.pptx
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
 
Inheritance and Interfaces
Inheritance and InterfacesInheritance and Interfaces
Inheritance and Interfaces
 
constructors.pptx
constructors.pptxconstructors.pptx
constructors.pptx
 

Mehr von myrajendra (20)

Fundamentals
FundamentalsFundamentals
Fundamentals
 
Data type
Data typeData type
Data type
 
Hibernate example1
Hibernate example1Hibernate example1
Hibernate example1
 
Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflow
 
2 jdbc drivers
2 jdbc drivers2 jdbc drivers
2 jdbc drivers
 
3 jdbc api
3 jdbc api3 jdbc api
3 jdbc api
 
4 jdbc step1
4 jdbc step14 jdbc step1
4 jdbc step1
 
Dao example
Dao exampleDao example
Dao example
 
Sessionex1
Sessionex1Sessionex1
Sessionex1
 
Internal
InternalInternal
Internal
 
3. elements
3. elements3. elements
3. elements
 
2. attributes
2. attributes2. attributes
2. attributes
 
1 introduction to html
1 introduction to html1 introduction to html
1 introduction to html
 
Headings
HeadingsHeadings
Headings
 
Forms
FormsForms
Forms
 
Css
CssCss
Css
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Starting jdbc
Starting jdbcStarting jdbc
Starting jdbc
 

Constructors.16

  • 1. Concept of Constructors and Types of Constructors http://improvejava.blogspot.in/ 1
  • 2. Objectives On 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. Recap • We have discussed in the previous class • ‘new’ operator • Usage of ‘new’ operator • Methods in a class http://improvejava.blogspot.in/ 3
  • 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. 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. 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. 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. Difference Between Classes WITHOUT CONSTRCTOR WITH CONSTRUCTOR class 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 A public 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. 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. 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. Types of Constructors • Default constructors • Explicit constructors • Parameterized constructors • Overloaded constructors http://improvejava.blogspot.in/ 11
  • 12. Default constructor What 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. 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 constructor class 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. Example on Explicit Constructor • When a user defines a constructor in a class, • the compiler will not provide any default constructor class 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. 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. 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. 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. Example For Overloaded Constructor class 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. 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. Another Example For Overloaded Constructor class 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. 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. 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. 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. Quiz 1. Default constructors provided by? a. Developer b. User c. JVM http://improvejava.blogspot.in/ 24
  • 25. Quiz Contd.. 2. Overloaded constructor has? a. same name b. different names http://improvejava.blogspot.in/
  • 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. Assignment 1. 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. Assignment Contd.. 2. Perform the above operation using parameterized constructor 3. Perform the above operation using overloaded constructors for the two student objects 28

Hinweis der Redaktion

  1. http://improvejava.blogspot.in
  2. http://improvejava.blogspot.in
  3. http://improvejava.blogspot.in
  4. http://improvejava.blogspot.in
  5. http://improvejava.blogspot.in
  6. http://improvejava.blogspot.in