0

I'm a beginner in Eiffel. I got 2 classes. The main one called APPLICATION:

class
    APPLICATION

inherit
    ARGUMENTS

create
    make

feature {NONE} -- Initialization

    make
            -- Run application.
        do
            print ("test")
        end    
end

and another class called BLUE:

class
    BLUE
create
    make

feature
    make
        local
            dead:BOOLEAN
            active:BOOLEAN
            number:BOOLEAN

        do
            io.putstring ("writetest")
        end

end

and I'm wondering how to make the methods in the BLUE class accessible and callable from the APPLICATION class?.

nbro
  • 15,395
  • 32
  • 113
  • 196
Phil_oneil
  • 267
  • 3
  • 11

3 Answers3

1

In class APPLICATION, you can add a local variable b of type BLUE, and then call make as its creation procedure:

make
    local
        b: BLUE
    do
        create b.make
    end
nbro
  • 15,395
  • 32
  • 113
  • 196
Emmanuel Stapf
  • 213
  • 1
  • 7
1

In general, there are two kinds of relationship between classes in object-oriented languages that allow one class to access features of another one:

  1. inheritance, and
  2. client-supplier relationship.

In the first case, the class inherits all features of the parent and may call them as its own:

class APPLICATION

inherit
    BLUE
        rename
                -- Class `APPLICATION' has `make' as well,
                -- so the inherited one has to be renamed to avoid a clash.
            make as make_blue
        end

create
    make

feature {NONE} -- Initialization

    make
            -- Run application.
        do
                -- This prints "test".
            print ("test")
                -- Call the inherited feature `{BLUE}.test'
                -- renamed above into `make_blue'
                -- that prints "writetest".
            make_blue
        end

end

In the case of inheritance, the call is performed on the same object.

In the client-supplier relationship, the call is performed on a different object. In your example the feature to be called coincides with a creation procedure, so the call becomes a part of a creation instruction of that object being created:

class APPLICATION

create
    make

feature {NONE} -- Initialization

    make
            -- Run application.
        local
            other: BLUE
        do
                -- This prints "test".
            print ("test")
                -- Create an object of type `BLUE'
                -- calling its creation procedure `make'
                -- that prints "writetest".
            create other.make
        end

end

A rough approximation regarding when to use one method over the other is the following. We can see inheritance as "is-a" and client-supplier as "has" relationship. For example, an apple has a color, but it is not a color, so client-supplier relationship fits better. On the other hand, it is a fruit, so inheritance relationship fits better. In the latter case, an apple class refines a fruit class by specifying some other properties like shape and seed location. But it cannot refine a color class. The same with your example: it does not look like APPLICATION is an example of BLUE, so the client-supplier relationship seems to fit better.

nbro
  • 15,395
  • 32
  • 113
  • 196
Alexander Kogtenkov
  • 5,770
  • 1
  • 27
  • 35
1

First, in BLUE you need a method, you should not do your writing in the create method, it will make it more difficult for you to write the program. Especially as the programs get more complex. So I have added write_message it is not a create method.

class
    BLUE

feature
    write_message
        local
            dead:BOOLEAN
            active:BOOLEAN
            number:BOOLEAN
        do
            io.putstring ("writetest")
        end
end

Now, we need to call the new method

class
    APPLICATION   
inherit
    ARGUMENTS
create
    make

feature {NONE} -- Initialization

    make
            -- Run application.
        local
             blue: BLUE
        do
            print ("test")
            create blue
            blue.write_message
        end
end
nbro
  • 15,395
  • 32
  • 113
  • 196
ctrl-alt-delor
  • 7,506
  • 5
  • 40
  • 52