FALLSEM2016-17_2245_RM001_29-JUL-2016_SWE1007_ETH.pdf

FALLSEM2016-17_2245_RM001_29-JUL-2016_SWE1007_ETH.pdf -...

This preview shows page 1 - 13 out of 45 pages.

Constructor 8/19/2016 1 VIT - SITE By Dr. K. John Singh
Image of page 1

Subscribe to view the full document.

Constructors Java has a mechanism, known as constructor, for automatically initializing the values for an object, as soon as the object is created. Constructors have the same name as the class it resides in and is syntactically similar to a method. It is automatically called immediatey after the object for the class is created by new operator. Contructors have no return type, not even void, as the implicit return type of a class’ constructor is the class type itself. Types of Constructors: Implicit/Default, Explicit, Parameterized
Image of page 2
Explicit Constructor Example class Room{ double length, breadth, height, volume; Room( ) { length = 14; breadth = 12; height = 10; } double volComp( ) { volume = length * breadth * height; return volume; }
Image of page 3

Subscribe to view the full document.

Example (contd.) public static void main (String args[ ]) { Room r1 = new Room(); Room r2 = new Room(); System.out.println("The volume of the room is "+r1.volComp( )); System.out.println("The volume of the room is "+r2.volComp( )); }
Image of page 4
Parameterized Constructor Example class Room2{ double length, breadth, height, volume; Room2( double l, double b, double h) { length = l; breadth = b; height = h; } // Computation of volume of the room double volComp( ) { volume = length * breadth * height; return volume; }
Image of page 5

Subscribe to view the full document.

Example (contd.) public static void main (String args[ ]) { Room2 r1 = new Room2(14, 12, 10 ); Room2 r2 = new Room2(16, 15, 11 ); System.out.println("The volume of the room is "+r1.volComp( )); System.out.println("The volume of the room is "+r2.volComp( )); } }
Image of page 6
Constructor Vs Methods
Image of page 7

Subscribe to view the full document.

Constructor Overloading Constructors for a class have the same name as the class but they can have different signature i.e. different types of arguments or different number of arguments. Such constructors can be termed as overloaded constructors. In the example given on next slide, we have two different classes, Rectangle and ConstOverloading. Rectangle class has two constructors, both with same names but different signatures. Second class ConstOverloading, has the main( ) method inside it.
Image of page 8
Constructor Overloading Example class Rectangle{ int l, b; Rectangle(){ l = 10; b = 20; } Rectangle(int x, int y){ l = x; b = y; } int area() { return l*b; }}
Image of page 9

Subscribe to view the full document.

Constructor Overloading Example….. class ConstOverloading { public static void main(String args[]) { Rectangle rectangle1=new Rectangle(); System.out.println("The area of a rectangle using first constructor is: "+rectangle1.area()); Rectangle rectangle2=new Rectangle(4,5); System.out.println("The area of a rectangle using second constructor is: "+rectangle2.area(); } }
Image of page 10
The Output The area of a rectangle using first constructor is: 200 The area of a rectangle using second constructor is: 20
Image of page 11

Subscribe to view the full document.

this keyword The keyword “this” is used in an instance method to refer to the object that contains the method, i.e. the it refers to the current object. Whenever and wherever, a reference to an object of the current class type is required,‘this’ can be used.
Image of page 12
Image of page 13

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern