Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Groovy is object-oriented


May 14, 2021 Groovy


Table of contents


In Groovy, as in any other object-oriented language, there are concepts of classes and objects to represent the object-oriented nature of the programming language. in. ogether, class data and methods are used to represent some real-world objects in the problem domain.

The class in Groovy declares the state (data) and behavior of objects defined by the class. Therefore, the Groovy class describes the instance fields and methods of the class.

The following is an example of a class in Groovy. T he name of the class is Student, and it has two fields - StudentID and StudentName. 21> object's StudentID and StudentName.

class Student {
   int StudentID;
   String StudentName;
	
   static void main(String[] args) {
      Student st = new Student();
      st.StudentID = 1;
      st.StudentName = "Joe"     
   } 
}

Getter and setter methods

In any programming language, the private keyword is always used to hide instance members, but instead provides getter and setter methods to set and get the values of instance variables accordingly. is.

class Student {
   private int StudentID;
   private String StudentName;
	
   void setStudentID(int pID) {
      StudentID = pID;
   }
	
   void setStudentName(String pName) {
      StudentName = pName;
   }
	
   int getStudentID() {
      return this.StudentID;
   }
	
   String getStudentName() {
      return this.StudentName;
   }
	
   static void main(String[] args) {
      Student st = new Student();
      st.setStudentID(1);
      st.setStudentName("Joe");
		
      println(st.getStudentID());
      println(st.getStudentName());
   } 
}

When we run the program above, we will get the following results -

1 
Joe 

Please note the following key points about the above procedure -

  • In classes, both studentID and objectName are marked as private, which means that they cannot be accessed from outside the class.

  • Each instance member has its own getter and setter methods. The getter method returns the value of the instance variable, such as the method int getStudentID() and the setter method sets the value of the instance ID, such as the method-void setStudentName (String pName).

The instance method

It's usually a natural thing to include more methods in a class, and it actually implements some functionality for the class. b20> ke. ere's what the code looks like.

In the following example, the Total method is an additional Instance method that has some logic built into it.

class Student {
   int StudentID;
   String StudentName;
	
   int Marks1;
   int Marks2;
   int Marks3;
	
   int Total() {
      return Marks1+Marks2+Marks3;
   }
	
   static void main(String[] args) {
      Student st = new Student();
      st.StudentID = 1;
      st.StudentName="Joe";
		
      st.Marks1 = 10;
      st.Marks2 = 20;
      st.Marks3 = 30;
		
      println(st.Total());
   }
}

When we run the program above, we will get the following results -

60

Create multiple objects

You can also create multiple objects for a class. H ere are examples of how this can be achieved. H ere, we create 3 objects (st, st1, and st2) and call their instance members and instance methods accordingly.

class Student {
   int StudentID;
   String StudentName;
	
   int Marks1;
   int Marks2;
   int Marks3;
	
   int Total() { 
      return Marks1+Marks2+Marks3;
   } 
	
   static void main(String[] args) {
      Student st = new Student();
      st.StudentID = 1;
      st.StudentName = "Joe";
		
      st.Marks1 = 10;
      st.Marks2 = 20;
      st.Marks3 = 30;
		
      println(st.Total()); 
   
      Student st1 = new Student();
      st.StudentID = 1;
      st.StudentName = "Joe";
		
      st.Marks1 = 10;
      st.Marks2 = 20;
      st.Marks3 = 40;
		
      println(st.Total());  
        
      Student st3 = new Student();
      st.StudentID = 1;
      st.StudentName = "Joe";
		
      st.Marks1 = 10; 
      st.Marks2 = 20;
      st.Marks3 = 50;
		
      println(st.Total());
   } 
} 

When we run the program above, we will get the following results -

60 
70 
80 

Inherited

Inheritance can be defined as the process by which one class acquires the properties (methods and fields) of another class. By using inheritance, information can be managed in a hierarchical order.

Classes that inherit other properties are called sub-classes (derived classes, sub-classes), and classes that inherit properties are called super-classes (base classes, parent classes).

Extended

Extends are keywords used to inherit the properties of a class. T he syntax of the extends keyword is given below. In the following example, we did the following -

  • Create a class called Person. This class has an instance member named name named name.

  • Create a class called Sent, which inherits from theErson class. Note that the name instance members defined in theErson class are inherited in the Sent class.

  • In the Student class constructor, we call the base class constructor.

  • In our Sent class, we added two instance members of ThentID and Marks1.

class Example {
   static void main(String[] args) {
      Student st = new Student();
      st.StudentID = 1;
		
      st.Marks1 = 10;
      st.name = "Joe";
		
      println(st.name);
   }
} 

class Person {
   public String name;
   public Person() {}  
} 

class Student extends Person {
   int StudentID
   int Marks1;
	
   public Student() {
      super();
   } 
}   

When we run the program above, we will get the following results -

Joe

The internal class

The internal class is defined in another class. C losed classes can use internal classes as usual. I nternal classes, on the other hand, can access members of their closed classes, even if they are private. es.

The following is an example of an external and internal class. In the following example, we did the following -

  • Create a class called Outer, which will be our external class.
  • Define a string named named named named named name in the Outer class.
  • Create an internal or nested class in our outer class.
  • Note that in the internal class, we can access the name instance members defined in the Out class.
class Example { 
   static void main(String[] args) { 
      Outer outobj = new Outer(); 
      outobj.name = "Joe"; 
      outobj.callInnerMethod() 
   } 
} 

class Outer { 
   String name;
	
   def callInnerMethod() { 
      new Inner().methodA() 
   } 
	
   class Inner {
      def methodA() { 
         println(name); 
      } 
   } 
	
}   

When we run the program above, we will get the following results -

Joe

Abstract class

Abstract classes represent common concepts, so they cannot be instantiated and created as subsyscules. b20> bstract methods are not implemented and must be implemented through concrete sub-classes. ds. bstract methods must also be declared with abstract keywords.

In the following example, note that the Person class is now an abstract class and cannot be instantiated. /b10> Also note that there is an abstract method called DisplayMarks in the abstract class that has no implementation details. I n the student class, implementation details must be added.

class Example { 
   static void main(String[] args) { 
      Student st = new Student(); 
      st.StudentID = 1;
		
      st.Marks1 = 10; 
      st.name="Joe"; 
		
      println(st.name); 
      println(st.DisplayMarks()); 
   } 
} 

abstract class Person { 
   public String name; 
   public Person() { } 
   abstract void DisplayMarks();
}
 
class Student extends Person { 
   int StudentID 
   int Marks1; 
	
   public Student() { 
      super(); 
   } 
	
   void DisplayMarks() { 
      println(Marks1); 
   }  
} 

When we run the program above, we will get the following results -

Joe 
10 

Interface

The interface defines the contracts that the class needs to adhere to. T he interface defines only a list of methods that need to be implemented, but does not define a method implementation. or. he interface defines only method signatures. T he method of the interface is always exposed. U sing a protected or private method in an interface is an error.

The following is an example of an interface in groovy. In the following example, we did the following -

  • Create an interface called Marks and create an interface method called Display Marks.

  • In the class definition, we use the images keyword to implement the interface. Because we are the implementation

  • Because we are implementing interfaces, we have to provide implementations for the DisplayMarks method.

class Example {
   static void main(String[] args) {
      Student st = new Student();
      st.StudentID = 1;
      st.Marks1 = 10;
      println(st.DisplayMarks());
   } 
} 

interface Marks { 
   void DisplayMarks(); 
} 

class Student implements Marks {
   int StudentID
   int Marks1;
	
   void DisplayMarks() {
      println(Marks1);
   }
}

When we run the program above, we will get the following results -

10