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

Groovy features


May 14, 2021 Groovy


Table of contents


Features are the structural construction of the language, allowing -

  • The composition of the behavior.
  • The runtime implementation of the interface.
  • Compatibility with static type check/compilation

They can be thought of as interfaces that host default implementations and states. Use the trait keyword to define the trait.

Here's an example of a feature:

trait Marks {
   void DisplayMarks() {
      println("Display Marks");
   } 
}

You can then use the implement keyword to implement trait in an interface-like manner.

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

trait Marks { 
   void DisplayMarks() {
      println("Display Marks");
   } 
} 

class Student implements Marks { 
   int StudentID
   int Marks1;
}

Implement the interface

Traits can implement an interface, in which case the interface keyword is used to declare the interface.

An example of implementing the characteristics of an interface is given below. In the following example, you can pay attention to the following points.

  • Interface Total is defined using the method DisplayTotal.

  • Feature Marks implements the Total interface, so you need to provide an implementation for the DisplayTotal method.

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

interface Total {
   void DisplayTotal() 
} 

trait Marks implements Total {
   void DisplayMarks() {
      println("Display Marks");
   }
	
   void DisplayTotal() {
      println("Display Total"); 
   } 
} 

class Student implements Marks { 
   int StudentID
   int Marks1;  
} 

The output of the above program will be -

Display Marks 
Display Total

Property

Features can define properties. An example of a trait with attributes is given below.

In the following example, Marks1 of type integer is a property.

class Example {
   static void main(String[] args) {
      Student st = new Student();
      st.StudentID = 1;
		
      println(st.DisplayMarks());
      println(st.DisplayTotal());
   } 
	
   interface Total {
      void DisplayTotal() 
   } 
	
   trait Marks implements Total {
      int Marks1;
		
      void DisplayMarks() {
         this.Marks1 = 10;
         println(this.Marks1);
      }
		
      void DisplayTotal() {
         println("Display Total");
      } 
   } 
	
   class Student implements Marks {
      int StudentID 
   }
} 

The output of the above program will be -

10 
Display Total

The composition of the behavior

Features can be used to achieve multiple inheritances in a controlled manner, avoiding diamond problems. I n the following code example, we defined two characteristics - Marks and Total. al. ecause the student class extends both features, it can access both methods - DisplayMarks and DisplayTotal.

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

trait Marks {
   void DisplayMarks() {
      println("Marks1");
   } 
} 

trait Total {
   void DisplayTotal() { 
      println("Total");
   } 
}  

class Student implements Marks,Total {
   int StudentID 
}   

The output of the above program will be -

Marks1
Total 

Extended features

The feature may extend another feature, in which case the extends keyword must be used. In the following code example, we extended Total trait with Marks trait.

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

trait Marks {
   void DisplayMarks() {
      println("Marks1");
   } 
} 

trait Total extends Marks {
   void DisplayMarks() {
      println("Total");
   } 
}  

class Student implements Total {
   int StudentID 
}

The output of the above program will be -

Total