Constructor
Objectives
Able to define constructor
Able to declare constructor
Able to create overloading
constructor
Purpose and Function of
Constructor
Constructors have one purpose in life: to
create an instance of a class. This can
also be called creating an object, as in:
University unikl = new University();
The purpose of methods, by contrast, is
much more general.
A method's basic function is to execute
Java codes.
Signature differences
Constructors and methods differ in three aspects of the
signature: modifiers, return type, and name.
Like methods, constructors can have any of the access
modifiers: public, protected, private, or none (often
called package or friendly). Unlike methods,
constructors can take only public access modifiers.
Therefore, constructors cannot be final or static.
The return types are very different too. Methods can
have any valid return type, or no return type, in which
case the return type is given as void. Constructors have
NO return type, not even void.
Signature differences
Finally, in terms of the signature, methods and
constructors have different names.
Constructors have the same name as their
class; by convention, methods use names
other than the class name.
If the Java program follows normal
conventions, methods will start with a
lowercase letter, constructors with an
uppercase letter. Also, constructor names are
usually nouns because class names are usually
nouns; method names usually indicate actions.
Example1: Declaring
Constructor
public class University
{ Constructor name
private String institute;
Receiving parameter
public University (String input)
{ Store value of input into name
institute = input;
}
}
public class UniversityTest
{
public static void main(String args[ ])
{
University unikl = new University (“miit");
}
}
Example2: Declaring
Constructor
public class Transportation
{
private String name; Constructor name
private int year;
private String model;
No parameter
public Transportation( )
{ Store default value into data member
name = “Pesona";
year = 2007;
model = “Sedan1.4";
}
public void DisplayInfo( )
{
System.out.println("The default car name is "+name);
System.out.println("The default car year is "+year);
System.out.println("The default car model is "+model);
}
}
Example2: Declaring
Constructor
public class TransportationTest
{
public static void main(String[ ] args)
{
Create an instance called car
Transportation car = new Transportation();
car.DisplayInfo( ); Call DisplayInfo method
}
}
Output
The following will be display if
you compile and run the
program.
Example3: Declaring
Constructor
public class Transportation
{
String name;
int year;
String model;
public Transportation (String CarName, int CarYear, String CarModel)
{
name=CarName;
year=CarYear;
model=CarModel
public void DisplayInfo( )
{
System.out.println("The default car name is "+name);
System.out.println("The default car year is "+year);
System.out.println("The default car model is "+model);
}
}
Example3: Declaring
Constructor
public class TransportationTest
{
public static void main(String[ ] args)
{
Transportation car = new Transportation(“Perdana”,2002,”DANA1.6”);
car.DisplayInfo( );
}
}
Example3: Declaring
Constructor
The following will be displayed if
you compile and run the
program.
Check Point
Class Bicycle has 3 data members as String id,
String ownerName and int yearBuilt.
Create a constructor without parameter (default
constructor) that will assigned a default value of
“001”,”Malek” and 2006 when an instance of Bike1 is
created.
Create a constructor with parameter that will pass
default value of “001”,”Malek” and 2006 when an
instance of Bike1 is created through parameter.
Create the test class too !. Compile and execute in your
virtual compiler
Write your answer…
Overloaded Constructor
Constructor has exactly the same name
as the defining class.
Therefore, if you provide two or more
constructors for the same class, they
are overloaded by definition.
Those overloaded constructors used in
the same program MUST have different
argument lists so that compiler can
differentiate them.
Example4: Overloaded
Constructor
public class Number
{ private double a;
private String b;
public Number ( ) // default constructor
{ a = 0.0;
b = “Hello”;
}
public Number ( double m, String n ) // constructor
with 2 arguments
{ a = m;
b = n;
}
}
Example4: Overloaded
Constructor
public class TestNumber
{
public static void main (String[ ] args)
{
Number myObj1 = new Number (100.0,“Welcome” );
// call constructor with arguments
Number myObj2 = new Number( ); // call default
constructor
}
} // end class TestNumber
::Check point::
public class V
{ private double a;
private double b;
public V ( )
public class TestV
{ a = 0.0; {
b = 0.0; public static void main (String[ ]
}
args)
public V ( double m, double n ) {
{ a = m; V x = new V(100.0,
b=n
} 200.0 );
x.updateValues();
public void updateValues()
{ a = a + 5;
x.display();
b = b +10; V y = new V( );
}
public void display( )
y.updateValues();
{ y. display( );
System.out.println( a + b ); }
System.out.println( b - a );
} } // end class TestV
} // end class V
What is the output for the above program
Exercise
Create a class ShortCourse. ShortCourse class has 4 private data
members, courseTitle, courseId, courseFee and courseDuration.
A public method, updatedFee() will calculate the
new fees of specific courses based on the following discount rate
shown below:
courseId Discount rate
ICT1010 25%
ICT1056 10%
Others 8%
Another public method, displayCourseDetails() will display all
information about the course.
If the new fees is greater than RM100, acknowledge the user that
they will be given a free one-week workshop.
Create your test class called ShortCourseTest. In this class, write a
main() method and call updatedFee() to calculate new fees and
display course details by calling displayCourseDetails() .
You are to produce a program using :
default constructor(coded)
constructor with arguments
Extra Reading
http://java.sun.com/docs/books/tutori
al/java/javaOO/constructors.html