Friday, January 25, 2013

Introduction to Object Oriented programming


Constructors

Object creation requires four things, the type of object to be created, a variable to store the reference of the created object, the new keyword to effect the actual creation and a constructor. You have seen and used all these things with the exception of the last one, the constructor. In the statement , Phone is the type and Phone() is the constructor.
A constructor call is a function, first and foremost but it is not an ordinary function. It is special because it bears the same name as that of the class where it is declared and unlike ordinary functions, it is not written with a return type.
Constructors are important, you cannot create objects without them. They are of extreme importance that Java will actually supply a default constructor in case you do not provide one explicitly.
class Phone {

    public static void main(String []args) {
        Phone p1 = new Phone();
    }
}
In the preceding code sample, we did not define an explicit constructor but we were still able to call . The reason for this is because Java supplied our code with a default no-arg constructor 3. As if we had written something like
class Phone {

    Phone() {
    }

    public static void main(String []args) {
        Phone p1 = new Phone();
    }
}
There is also no return statement within the body of the constructor. When the constructor returns, it will create an object of type Phone, the variable p1 will hold a reference (an address) to actual Phone object.

Call me and you will get the knowledge and skills you need instantly.
For inquiries contact me on my mobile number or email me at inquiry@eglobiotraining.com. View my multiply site http://erwinglobio.multiply.com
 
 
Prof. Erwin M. Globio, MSIT
Senior IT Lecturer
Far Eastern University
Email Address: inquiry@eglobiotraining.com
Skype: erwinglobio
 
 
Call Now:
 
SMART: 09393741359
SUN: 09323956678






No comments:

Post a Comment