Haskell Program to Implement multiple inheritance


Haskell is a functional programming language and does not have a concept of inheritance. Instead, it uses type classes to achieve similar functionality. This tutorial will help us in implementing the same in Haskell.

Method 1: Implement a type class that simulates multiple inheritances.

This method uses type class to simulate the multiple inheritance. Here, we define two parent class and one child class. And the function of child class is being called. Once the function is being called it will take instance from both the parent class and the final output is displayed.

Algorithm

  • Step 1 − The first parent class is defined.

  • Step 2 − The second parent class is defined.

  • Step 3 − The child class is defined with Parent1 and Parent2.

  • Step 4 − The example instance for both the parent and child class is defined.

  • Step 5 − Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do.

  • Step 6 − The instance of function3 from child class is being called and the final output is displayed.

Example

In the following example, we are going to implement a type class that simulates multiple inheritances.

class Parent1 a where
   function1 :: a -> String
class Parent2 a where
   function2 :: a -> String
class (Parent1 a, Parent2 a) => Child a where
   function3 :: a -> String
   function3 x = (function1 x) ++ (function2 x)
data Example = Example
instance Parent1 Example where
   function1 _ = "Hello "
instance Parent2 Example where
   function2 _ = "World!"
instance Child Example
main :: IO ()
main = do
   putStrLn (function3 Example)

Output

Hello World!

Method 2: Implement multiple inheritances using Composition.

This method uses composition, where instead of inheriting from multiple classes, we create a new data type that has fields for each of the types that we want to "inherit" from, and then provide functions that operate on those fields. This is a way to achieve code reuse while still maintaining a strong separation of concerns.

Algorithm

  • Step 1 − Parent1 and Parent2 data types are defined.

  • Step 2 − Child data type is defined that will be inherited from Parent1 and Parent2.

  • Step 3 − exampleChild instance is defined by using Composition.

  • Step 4 − Program execution will start from main function. The main() function has whole control of the program.

  • Step 5 − The final output is displayed by using ‘putStrLn’ statement.

Example

In this example, we are going to implement multiple inheritances using Composition

data Parent1 = Parent1 { function1 :: String }
data Parent2 = Parent2 { function2 :: String }
data Child = Child { parent1 :: Parent1, parent2 :: Parent2 }
exampleChild :: Child
exampleChild = Child { parent1 = Parent1 { function1 = "Hello " }, parent2 = Parent2 { function2 = "World!" } }
main :: IO ()
main = putStrLn (function1 (parent1 exampleChild) ++ function2 (parent2 exampleChild))

Output

Hello World!

Method 3: Implement multiple inheritance using a combination of type classes and data types.

This method uses a combination of type classes and data types, where we can create a new data type that has fields for each of the types that we want to "inherit" from, and then provide type classes that operate on those fields. And by this way, finally child class get inherited from both the parent classes and the final output is displayed.

Algorithm

  • Step 1 − Parent1 and Parent2 data types are defined.

  • Step 2 − Child data type is defined that will be inherited from Parent1 and Parent2.

  • Step 3 − Class of Parent1 and Parent2 functions are defined.

  • Step 4 − Instance of Parent1 and Parent2 functions are created.

  • Step 5 − Program execution will be started from main function. The main() function has whole control of the program.

  • Step 6 − Child class instance is being called with Parent1 and Parent2 functions as parameters.

  • Step 7 − The final output is displayed by using ‘putStrLn’ statement.

Example

In this example, we are going to implement multiple inheritance using a combination of type classes and data types

data Parent1 = Parent1
data Parent2 = Parent2
data Child = Child { parent1 :: Parent1, parent2 :: Parent2 }
class Parent1Functions a where
   function1 :: a -> String
class Parent2Functions a where
   function2 :: a -> String
instance Parent1Functions Parent1 where
   function1 _ = "Hello "
instance Parent2Functions Parent2 where
   function2 _ = "World!"
main :: IO ()
main = do
   let exampleChild = Child { parent1 = Parent1, parent2 = Parent2 }
   putStrLn (function1 (parent1 exampleChild) ++ function2 (parent2 exampleChild))

Output

Hello World!

Conclusion

Since Haskell is a functional programming language and does not have a concept of inheritance. But we can achieve the functionality of multiple inheritance by using type classes. There are various approaches to achieve the same. We can directly use the type class to simulate the multiple inheritance. We can implement multiple inheritance using Composition. Also, we can illustrate the concept of multiple inheritance by using the combination of type classes and data types. In every approach, the child class get inherited from the two parent classes.

Updated on: 19-Jan-2023

328 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements