Constructors

1,006 views

Published on

constructors

Published in: Education, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,006
On SlideShare
0
From Embeds
0
Number of Embeds
70
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Constructors

  1. 1. Constructors
  2. 2. • Has same name as that of class name• Used to initialize the instant variables of the objects
  3. 3. Types of constructors• Default constructor• Parameterized constructor• Copy constructor
  4. 4. Default constructor• Initializes instant variables of an object with definite values readily defined• Called by default while creating an object
  5. 5. Creating objects• Objects can be created in two ways  Object supplied by the compiler  Object supplied by the programmer
  6. 6. Object supplied by the compilerclass example{int a,b;example(){a=10;b=20;}void display(){S.o.pln(“The value of a is” +a);S.o.pln(“The value of b is” +b);}}
  7. 7. Object supplied by the programmerclass example{int a,b;example(){a=0;b=0;}void display(){S.o.pln(“The value of a is” +a);S.o.pln(“The value of b is” +b);}}Class mai{Void main(){Example ob = new example();Ob.display();}}
  8. 8. Parameterized constructor• Initializes the object variables by passing parametric values at the time of creation
  9. 9.  Object supplied by the compiler Object supplied by the programmer
  10. 10. Object supplied by the compilerclass example{int a,b;example(int x, int y){a=x;b=y;}void display(){S.o.pln(“The value of a is” +a);S.o.pln(“The value of b is” +b);}}
  11. 11. Object supplied by the programmerclass example{int a,b;example(int x, int y){a=x;b=y;}void display(){S.o.pln(“The value of a is” +a);S.o.pln(“The value of b is” +b);}}Class mai{Void main(){Example ob = new example(4,5);Ob.display();}}
  12. 12. Copy constructor• Copies the initial values of the instant variables of an object to the instant variables of another object
  13. 13. Types• Direct entry copy constructor• Copy constructor by passing the objects
  14. 14. Direct entry copy constructor• Initial value is copied to another object without using any specific constructorclass example{int a,b;example(int x,int y){a=x;b=y;}}Class mai{Void main(){Example ob1= new example(5,6);Example ob2 = ob1;S.o.pln(“The value of a is” +a);S.o.pln(“The value of b is” +b);}}
  15. 15. Copy constructor by passing the objectsclass example{int a,b;example(int x,int y){a=x;b=y;}Example(example p){A=p.a;B= p.b;}}Class mai{Void main(){Example ob1= new example(5,6);Example ob2 = new example(ob1);}}
  16. 16. Constructor overloading• Constructors with the same name but with different types of parameters
  17. 17. Encapsulation
  18. 18. • Wrapping up of data and function into a single class
  19. 19. Visibility modes• Public• Private• Protected
  20. 20. • A class member not declared under any visibility are by default public• Private can be used only within the scope of the class and cannot be accessed outside the class• Protected are used as private members which can only be applied within the class but can be inherited to other class

×