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

Constructors

on

  • 993 views

constructors

constructors

Statistics

Views

Total Views
993
Views on SlideShare
926
Embed Views
67

Actions

Likes
0
Downloads
0
Comments
0

3 Embeds 67

http://revisions4u.blogspot.in 46
http://www.revisions4u.blogspot.in 20
http://javaoutputs.blogspot.in 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Constructors Constructors Presentation Transcript

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