ConstructorObjectives 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; private int year; private String model; public Transportation( ) { name = “Pesona"; year = 2007; model = “Sedan1.4"; } Constructor name No parameter Store default value into data member } 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. public class Transportation { String name; int year; String model; Example3: Declaring Constructor 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 ( ) { a = 0.0; b = “Hello”; } // default constructor public Number ( double m, String n ) with 2 arguments { a = m; b = n; } } // constructor 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( ); constructor } } // end class TestNumber // call default ::Check point:: public class V { private double a; private double b; public V ( ) { a = 0.0; b = 0.0; } public V ( double m, double n ) { a = m; b=n } public void updateValues() { a = a + 5; b = b +10; } public void display( ) { System.out.println( a + b ); System.out.println( b - a ); } } // end class V public class TestV { public static void main (String[ ] args) { V x = new V(100.0, 200.0 ); x.updateValues(); x.display(); V y = new V( ); y.updateValues(); y. display( ); } } // end class TestV 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 ICT1010 ICT1056 Others Discount rate 25% 10% 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
Report "Introduction to Java Programming - Constructor"