• Like
  • Save
Constructors
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Published

constructors

constructors

Published in Education , Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
830
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
0
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Constructors
  • 2. • Has same name as that of class name• Used to initialize the instant variables of the objects
  • 3. Types of constructors• Default constructor• Parameterized constructor• Copy constructor
  • 4. Default constructor• Initializes instant variables of an object with definite values readily defined• Called by default while creating an object
  • 5. Creating objects• Objects can be created in two ways  Object supplied by the compiler  Object supplied by the programmer
  • 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. 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. Parameterized constructor• Initializes the object variables by passing parametric values at the time of creation
  • 9.  Object supplied by the compiler Object supplied by the programmer
  • 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. 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. Copy constructor• Copies the initial values of the instant variables of an object to the instant variables of another object
  • 13. Types• Direct entry copy constructor• Copy constructor by passing the objects
  • 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. 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. Constructor overloading• Constructors with the same name but with different types of parameters
  • 17. Encapsulation
  • 18. • Wrapping up of data and function into a single class
  • 19. Visibility modes• Public• Private• Protected
  • 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