JavaBeat Certifications Certifications Kits Articles Interview Questions OCAJP 7 OCPJP 5 OCPJP 6 OCEJWCD 6 SCBCD 5.0 SCEA SCJA

Introduction to Groovy - Scripting Language

Author : Raja
Topic : groovy  
Pages :
Submit Your Blog Feedback Request Article Print Email

3) Classes, Methods and Objects Declaration in Groovy

In this section, we will cover the how Classes and Objects into the groovy Environment. More specifically, we will see how to declare Groovy classes, define class methods, creating Objects etc. Creation of classes in Groovy almost follows the same syntax and semantics of the Java Language. However, there are quite a number of interesting and easy to use features available in Groovy. For example, consider the following class declaration called Product.

Product.groovy

				
class Product{

    private String name
    private def price
    def vendor

    public Product(){
    }

    Product(name, price, String vendor){
        this.name = name
        this.price = price
        this.vendor = vendor
    }

    public String getName(){
        return name
    }

    def setName(name){
        this.name = name
    }

    public String getPrice(){
        return price
    }

    def setPrice(price = 100.00){
        this.price = price
    }

    def toString(){
        return "Name = $name, Price = $price, Vendor = $vendor";
    }

    static main(arguments){

        def p1 = new Product("Mobile", "10000", "Nokia")
        println(p1.toString())

        def p2 = new Product(name: 'Laptop', price: "540000", vendor: "IBM")
        println(p2.toString())

        def p3 = new Product()
        p3['name'] = "Television"
        p3.'price' = "45454"
        p3['vendor'] = "Samsung"
        println(p3.toString())

        def p4 = new Product(name: "DVD Player", vendor: "TCL")
        p4.setPrice();
        println(p4.toString())
    }
}
				

The following is the output for the above program.

Ouput

				
Name = Mobile, Price = 10000, Vendor = Nokia
Name = Laptop, Price = 540000, Vendor = IBM
Name = Television, Price = 45454, Vendor = Samsung
Name = DVD Player, Price = 100.00, Vendor = TCL
				

Lots and lots of interesting things are there in the above code. Since Groovy is a Scripting language it is a Loosely Typed Language. What is meant by a loosely typed language is that, there is no need to define the data-types for the variables and for the return type of the methods. The defaults (which are very specific to the particular Scripting Languages) will be taken. Same is the case of Groovy.

For example, in the declaration section, we didn't mention the data-type as well the modifier of the variables. Groovy's default access modifier is public. Also examine the same standards followed in one of the method definition. Groovy also supports Default Parameters, which means that, if you didn't pass any value to a particular parameter in some method definition, then the default value will be taken.

For example consider the method signature,

				
def setPrice(price = 100.00){
    this.price = price
}
				

The above method setPrice() has one parameter and that too a default parameter having a value of 100.00. If the callee doesn't pass any argument to this method, then the value of 100.00, which is the default, will be taken for the price field.

Groovy support various ways of supply values to an object of a class. One is the traditional Constructor, Named Parameter Support and the Property-based Access. Let us see these approaches one by one.

The first way is to define a Constructor for a class and to supply values for the object directly during the time of Instantiation. For example, our sample class Product has a three argument constructor defined like this,

				
Product(name, price, String vendor){
    this.name = name
    this.price = price
    this.vendor = vendor
}
				

and the instantiation of the product p1 uses this Constructor to give the initial values to the object.

				
def p1 = new Product("Mobile", "10000", "Nokia")
				

The second object p2 uses the named parameter support to give values to the object. Watch carefully the syntax for this, the field name followed by a colon and then the value for the field.

				
def p2 = new Product(name: 'Laptop', price: "540000", vendor: "IBM")
				

The third object p3 is making using the property-based support for instantiating the object. The general syntax for accessing a property for a class is ObjectName.'propertyName' or ObjectName.['propertyName'] and the same rule has been followed while instantiating the object p3.

Submit Your Blog Feedback Request Article Print Email

Related Articles


javabeat | advertise | about us | contact | useful resources
Copyright (2004 - 2013), JavaBeat