Groovy Object oriented Programming concepts Explanation
Class definition in Groovy is almost identical to Java; classes are declared using the class keyword and may contain fields, constructors, initializers, and methods. Methods and constructors may themselves use local variables as part of their implementation code.
Groovy uses Java’s modifiers—the keywords private, protected, and public for modifying visibility; final for disallowing reassignment; and static to denote class variables. A non static field is also known as an instance variable. These modifiers all have the same meaning as in Java.class Example1
{
public fieldWithModifier
String typedField
def untypedField
protected field1, field2, field3
private assignedField = new Date()
static classField
public static final String CONSTA = 'a', CONSTB = 'b'
def someMethod()
{
def localUntypedMethodVar = 1
int localTypedMethodVar = 1
def localVarWithoutAssignment, andAnotherOne
}
}
def localvar = 1
boundvar1 = 1
def someMethod()
{
def localMethodVar = 1
boundvar2 = 1
}
someMethod()
Assignments to typed references must conform to the type. Groovy provides auto boxing and coercion when it makes sense. All other cases are type-breaking assignments and lead to a ClassCastException at runtime. Example Program for Groovy object calling and reference
class Person
{
def walk() { println "Walking..." }
def walk(int miles) { println "Walking $miles miles..." }
def walk(int miles, String where) { println "Walking $miles miles $where..." }
}
arun = new Person()
arun.invokeMethod("walk" , null)
arun.invokeMethod("walk" , 10)
arun.invokeMethod("walk" , [2, 'uphill' ] as Object[])
Ouput of above program see below
Walking...
Walking 10 miles...
Walking 2 miles uphill...
Methods and parameters for Groovy Script
Method declarations follow the same concepts you’ve seen for variables: the usual Java modifiers can be used; declaring a return type is optional; and, if no modifiers or return type are supplied, the def keyword fills the hole. In this case, under the covers, the return type will be java.lang.Object. The default visibility of methods is public.
class demo1
{
static void main(args)
{
def some = new demo1()
some.publicVoidMethod()
assert 'hi' == some.publicUntypedMethod()
assert 'ho' == some.publicTypedMethod()
combinedMethod()
}
void publicVoidMethod()
{
}
def publicUntypedMethod()
{
return 'hi'
}
String publicTypedMethod()
{
return 'ho'
}
private static final void combinedMethod() { }
}
Constructors for Groovy programming
Objects are instantiated from their classes via constructors. If no constructor is given, an implicit constructor without arguments is supplied by the compiler. This appears to be exactly like in Java, but because this is Groovy, it should not be surprising that additional features are available.
class VendorWithCtor
{
String name, product
VendorWithCtor(name, product)
{
this.name = name
this.product = product
}
}
def first = new VendorWithCtor('Canoo','ULC')
def second = ['Canoo','ULC'] as VendorWithCtor
VendorWithCtor third = ['Canoo','ULC']
Example programming see below code
class Car
{
def miles = 0
final year
Car(theYear)
{
year = theYear
}
}
Car car = new Car(2008)
println "Year: $car.year"
println "Miles: $car.miles"
println 'Setting miles'
car.miles = 25
println "Miles: $car.miles"
Output of the above program
Year: 2008 Miles: 0 Setting miles Miles: 25
Organizing classes in packages
Groovy follows Java’s approach of organizing files in packages of hierarchical structure.
The package structure is used to find the corresponding class files in the file system’s directories.
Because *.groovy source files aren’t necessarily compiled to *.class files, there’s also a need to look up *.groovy files.
package business
class Vendor
{
public String name
public String product
public Address address = new Address()
}
class Address
{
public String street, town, state
public int zip
}
To reference Vendor in the business package, you can either use business.Vendor within the code or use import statements for abbreviation. IMPORTS for Groovy Programming
Groovy follows Java’s notion of allowing import statements before any class declaration to abbreviate class references.
import business.*
def canoo = new Vendor()
canoo.name = 'Canoo Engineering AG'
canoo.product = 'UltraLightClient (ULC)'
assert canoo.dump() =~ /ULC/
Groovy default import statements
By default, Groovy imports six packages and two classes, making it seem like every
Groovy code program contains the following initial statements:
import java.lang.* import java.util.* import java.io.* import java.net.* import groovy.lang.* import groovy.util.* import java.math.BigInteger import java.math.BigDecimal
Using inheritance in Groovy Programming
Inheritance allows you to implicitly add fields and methods from a base class.We simply let you know that all the inheritance features of Java (including abstract classes) are available in Groovy and also work between Groovy and Java.
Groovy classes can extend Groovy and Java classes and interfaces alike. Java classes can also extend Groovy classes and interfaces
Groovy classes can extend Groovy and Java classes and interfaces alike. Java classes can also extend Groovy classes and interfaces. You need to compile your Java and Groovy classes in a particular order for this to work.
0 Comments:
Post a Comment