As we saw in
the last tutorial, variables are memory locations where we store values. We
also said that in Groovy, everything is an object. So what exactly are objects?
Object-Oriented Programming (OOP)
There are
many programming paradigms, each with its own strengths and weaknesses. OOP is
well-suited to large and complex software systems due to its various properties
such as information hiding and encapsulation.
In OOP, the
building blocks of a software system are classes and objects. Classes are like
a blueprint of an entity, whether concrete e.g. person, document, car or
building, or abstract, such as the state of a game, an idea or a communication/
http session. Objects are instances of a class, or the realization of the
blueprint (class).
Classes
Classes are
blueprints or templates of a program’s building block. They group together data
and operations relevant to that data. Classes are composed of fields and
methods. Fields store the state of a class’ instantiation i.e. an object, while
methods represent the behavior of a class.
For example,
the class car will have fields to represent color, mileage and registration
number while its methods will be getColor(), setMileage(), addMileage() and
getRegNum(). As you can guess, each determines the way we can interact with
fields.
This
provides a way of achieving two of the core principle of object-oriented
programming, encapsulation and information-hiding. Encapsulation means grouping
together relevant information, ‘in a capsule’. Information-hiding is achieved
through being able to access and modify fields via methods using access
modifiers. Access modifiers will be discussed in greater detail in the next
tutorial. We shall now get to code samples to show examples of using classes
and object since there is much more to OOP than I can cover in this tutorial. You
should definitely check it out in more detail.
By
convention, variable, class and object names use camel-case, with class names
starting with a capital letter while variable, object, field and method names
start in lowercase.
When saving
a groovy script/ file, make sure it is does not clash with any class names in
the script.
Persons.groovy
class Person
{
def name //defines a property
def income
def gender
}
def p1=new Person()
p1.name='Cabee'
p1.income=7000000
p1.gender='Male'
println
p1.name
Output
Cabee
To see what
happens when you give a file a name used in as a class name, change both
instances of Person to Persons. The following compilation error will be thrown:
Invalid
duplicate class definition…
For java developers, the thing to remember is that Groovy
will create the getters and setters in the background, thus p1.name does not
reference the field name but rather, calls the auto-created setName() method in
the background.
To show some of the syntactic sugar Groovy adds to
classes, here is a slightly longer code sample we shall look into more details
in the next tutorial:
class
Persona
{
def name //defines a property
def income
def
gender
}
def p1=new Persona()
p1.name='Cabee'
p1.income=7000000
p1.gender='Male'
println "name: $p1.name"
println "income: $p1.income"
println "gender: $p1.gender"
println()
p1=new Persona([name:'David',income:500000,gender:'Male'])
println "name: $p1.name"
println "income: $p1.income"
println "gender: $p1.gender"
Output
name: Cabee
income: 7000000
gender: Male
name: David
income: 500000
gender: Male
In the next
tutorial, we shall look at constructors, access modifiers to control data
access and get into classes and objects in more details.
Happy coding with Cabee Groovy
Mastery Course!!!