In Groovy, inheritance works in a similar way as in other object-oriented programming languages. To use inheritance in Groovy, you can create a new class that extends an existing class using the extends
keyword. This means that the new class will inherit all the properties and methods of the existing class.
When defining a new class that extends another class, you can access the properties and methods of the parent class using the super
keyword. This allows you to override methods from the parent class to provide custom behavior in the child class.
You can also use inheritance to create a hierarchy of classes, with multiple levels of inheritance. This can help organize your code and make it easier to manage and maintain.
Overall, inheritance is a powerful feature in Groovy that allows you to reuse code, create more flexible and extensible applications, and improve code organization.
How to achieve polymorphism using inheritance in Groovy?
In Groovy, polymorphism can be achieved using inheritance by allowing different classes to implement the same method in different ways. Here's an example demonstrating how polymorphism can be achieved using inheritance in Groovy:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
class Animal { def makeSound() { println "Animal makes a sound" } } class Dog extends Animal { def makeSound(){ println "Dog barks" } } class Cat extends Animal { def makeSound(){ println "Cat meows" } } Animal myAnimal = new Dog() myAnimal.makeSound() // Will print "Dog barks" myAnimal = new Cat() myAnimal.makeSound() // Will print "Cat meows" |
In this example, the Animal
class defines a method makeSound()
which is then overridden in the Dog
and Cat
classes with their own implementations of the method. When an object of type Dog
or Cat
is assigned to a variable of type Animal
, calls to the makeSound()
method will execute the overridden method specific to the actual object type, demonstrating polymorphism in action.
What is the diamond problem in multiple inheritance and how to resolve it in Groovy?
The diamond problem in multiple inheritance occurs when a class inherits from two classes that have a common ancestor class. This can lead to ambiguities in the code and make it difficult to determine which method to call when there are conflicting method definitions.
In Groovy, the diamond problem can be resolved by using the trait
feature. Traits are similar to interfaces in Java, but they can also contain implementation code. By using traits, you can separate the common behavior of the ancestor class into a trait and then have the conflicting classes inherit from the trait instead of directly from the common ancestor class. This allows you to avoid the diamond problem and keep your code clean and maintainable.
What is super keyword in Groovy and how to use it?
In Groovy, the super
keyword is used to refer to the superclass of the current object. It allows you to access methods and properties defined in the superclass.
Here is an example of how to use the super
keyword in Groovy:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class Person { String name = "John" void display() { println "Name: $name" } } class Employee extends Person { String company = "ABC Company" void display() { super.display() // calling display method of the superclass println "Company: $company" } } def emp = new Employee() emp.display() |
In the above example, the Employee
class extends the Person
class. The display
method in the Employee
class uses the super
keyword to call the display
method of the superclass before printing out the company name.
By using the super
keyword, you can invoke methods or access properties from the superclass in your subclass.
How to override a method from a parent class in Groovy?
In Groovy, you can easily override a method from a parent class by simply redefining the method in the child class with the same name as the method in the parent class. Here's an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Parent { def greet() { println "Hello from parent class" } } class Child extends Parent { @Override def greet() { println "Hello from child class" } } def child = new Child() child.greet() // Output: Hello from child class |
In the above example, the Child
class extends the Parent
class and overrides the greet()
method with its own implementation. When we create an instance of the Child
class and call the greet()
method, it will print "Hello from child class" instead of "Hello from parent class".
What is the default superclass in Groovy?
The default superclass in Groovy is java.lang.Object
.
What is the difference between abstract class and interface in Groovy?
In Groovy, the main difference between an abstract class and an interface is that an abstract class can have both abstract methods (methods without implementation) and concrete methods (methods with implementation), while an interface can only have abstract methods.
Another difference is that a class can implement multiple interfaces, but it can only extend one abstract class. This allows for more flexibility in the design of the class hierarchy.
Abstract classes can also have instance variables, constructors, and static methods, while interfaces can only have constants and abstract methods.
Overall, abstract classes are better suited for classes that have some common functionality, while interfaces are better for defining contracts for classes that need to adhere to a specific set of methods.