How to resolve the algorithm Inheritance/Multiple step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Inheritance/Multiple step by step in the Wren programming language

Table of Contents

Problem Statement

Multiple inheritance allows to specify that one class is a subclass of several other classes. Some languages allow multiple inheritance for arbitrary classes,   others restrict it to interfaces,   some don't allow it at all.

Write two classes (or interfaces) Camera and MobilePhone,   then write a class CameraPhone which is both a Camera and a MobilePhone. There is no need to implement any functions for those classes.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Inheritance/Multiple step by step in the Wren programming language

Source code in the wren programming language

class Camera {
    construct new() {}
    snap() { System.print("taking a photo") }
}

class Phone {
    construct new() {}
    call() { System.print("calling home") }
}

class CameraPhone is Camera {
    construct new(phone) { _phone = phone } // uses composition for the Phone part
    // inherits Camera's snap() method
    // Phone's call() method can be wrapped
    call() { _phone.call() }
}

var p = Phone.new()
var cp = CameraPhone.new(p)
cp.snap()
cp.call()


  

You may also check:How to resolve the algorithm Permutations by swapping step by step in the Sidef programming language
You may also check:How to resolve the algorithm Sum to 100 step by step in the 11l programming language
You may also check:How to resolve the algorithm Quine step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Compiler/lexical analyzer step by step in the Lua programming language
You may also check:How to resolve the algorithm Population count step by step in the COBOL programming language