Parent constructor calls abstract method

Hi everybody!
I'm wondering if there is something wrong with java or if the idea is just too ill?
Anyway, I think it would be great if this hierachy would work...
Two classes A and B.
Class A defines an astract method.
In A's constructor this abstract method is called.
Class B extends A and provides an implementation for the abstract method in A.
Class B also defines a member variable that is set in B's implementation of the abstract method.
In class' B constructor the parent constructor A() is called.
example:
public abstract class A {
  public A() {
    createComponents();
  public abstract void createComponents();
public class B extends A {
  private String string = null;
  public B() {
    super();
    System.out.println("B::B() " + string);
  public void createComponents() {
    System.out.println("B::createComponents() begin");
    string = new String("test");
    System.out.println("B::createComponents() " + string);
  public void describe() {
    System.out.println("B::describe() " + string);
  public static void main(String[] args) {
    B b = new B();
    b.describe();
}running the code above produces the following output:
B::createComponents() begin
B::createComponents() test
B::B() null
B::describe() null
why is the string member variable null in B's constructor??
thanks in advance
Peter Bachl
Polytechnic University of Upper Austria, Hagenberg
[email protected]

The answer is that the call of the super-constructor
is allways done before the initialization
of the member variable. That's all and that's the
normal behavior.
order :
- initialization of static variables
- call to the super-constructor
- initialization of the instance variables
- execution of the constructor
Since this is the advanced forum it is relevant to point out that that is not exactly true.
There is a step in java that 'initializes' member variables before any constructors are called, super or other wise.
From the JLS 12.5...
Otherwise, all the instance variables in the new object, including those declared in superclasses, are initialized to their default values (4.5.5)
Using the following code as an example
  class MyClass
     int i1;
     int i2 = 1;
.When creating an instance of the above there will be three 'initializations'
// Part of object creation...
i1 = 0; // The default value
i2 = 0; // The default value
// Part of construction...after super ctors called.
i2 = 1; // The variable initializer (see step 4 of JLS 12.5)
Unfortunately the descriptions are rather similar and so confusion can result as to when the assignment actually occurs.

Similar Messages

  • Calling abstract method inside constructor

    what will happen if you call abstract method inside a constructor ?

    AMARSHI wrote:
    Then wat is the purpose of that object then.
    When u create an object then the control will move to the default constructor,If there is one. Not all classes have default constructors. Some c'tor will be called though.
    inside the constructor u r having an abstract method.
    But theere are 2 cases now:
    1 the top-level class is an abstract class
    2 the abstract class is an inner class.
    1. for case 1 , u cannot create an object ,so no need of having the constructor.Yes, you can create an object. You can instantiate a concrete subclass. The abstract parent's constructor is still called, and that c'tor may call an abstract method, which will be implemented in the concrete subclass, or some class between it and the parent.
    2.for case 2 u cannot have an object like:
    outerClass obj = new innerAbstractClass()I have no idea what you're talking about here.

  • Calling abstract method from method, where is functional code

    HI All
    I'm trying to find the actual code that is responsible for the play() method to play sound of Applet or of AudioClip. I looked in the source files and found that the two are circle-refering to each other. Audio clip is an interface so it doesnt specify any code for how the method play() should work. That should be left to the classes that implement it. But the play(URL) method of Applet refers to the the AudioClips method:public void play(URL url) {
    AudioClip clip = getAudioClip(url);
    if (clip != null) {
    clip.play();
    So where is the real code for the method?
    This seems to be very normal because almost all methods in Applet where refering to interfaces like AppletContext an AppletStub.
    How does this work? Can anybody explain?

    This is a design-pattern called "factory-method". The factory-method (here getAudioClip(URL)) has a return value of an interface but really returns an object of a class implementing this interface. The user can call this factory-method and then call the methods of the returned object without knowing wich class this methods implements. If you want to change the implementing class later, you only have to build a new class and to change the factory-method - but the code of the "user" will not change.

  • Abstract classes and constructors - cannot call abs. methods in CONSTRUCTOR

    Let me explain the scenario:
    I'm building a program in which I need to read a file (among other things) and I intend to use object orientation to it's fullest in doing so. I thought of creating an abstract FILE class which has the commonalities, and two subclasses SERVER_FILE and PC_FILE, which implement the abstract method GET_CONTENTS in different ways (OPEN DATASET / GUI_UPLOAD), same for the CHOOSE method which allows to select the file from it's corresponding source.
    Initially I've used an interface but since another tasks like setting the file path are common for both, switched to an ABSTRACT class.
    Now, the problem is, from the main code I intend to use a FILE reference to handle either type of file. At the instantiation moment I'd like the path attribute to be set; if it was not set by parameter, i'd like to call the CHOOSE method which is abstract for the superclass. Since this is common for either subclass, I need a way to code it once in the superclass. But I get an error because the CHOOSE method is abstract.
    This is the problem code (extracts):
    *       CLASS lcl_file DEFINITION
    CLASS lcl_file DEFINITION ABSTRACT.
      PUBLIC SECTION.
        METHODS:
          constructor
            IMPORTING
              i_path  TYPE string OPTIONAL
            EXCEPTIONS
              no_path_chosen,
          get_contents ABSTRACT
            RETURNING
              value(rt_contents) TYPE string_table
            EXCEPTIONS
              read_error.
      PROTECTED SECTION.
        DATA:
          _v_path        TYPE string.
        METHODS:
          choose ABSTRACT
            EXCEPTIONS
              no_path_chosen,
          set_path
            IMPORTING
              i_path  TYPE string.
    ENDCLASS.                    "lcl_file DEFINITION
    *       CLASS lcl_file IMPLEMENTATION
    CLASS lcl_file IMPLEMENTATION.
      METHOD constructor.
        IF i_path IS SUPPLIED.
          CALL METHOD set_path
            EXPORTING
              i_path = i_path.
        ELSE.
    *---->>>> PROBLEM CALL - CAN'T BE DONE!!
          CALL METHOD choose
            EXCEPTIONS
              no_path_chosen = 1.
          IF sy-subrc = 1.
            RAISE no_path_chosen.
          ENDIF.
        ENDIF.
      ENDMETHOD.                    "constructor
      METHOD set_path.
        _v_path = i_path.
      ENDMETHOD.                    "set_path
    ENDCLASS.                    "lcl_file IMPLEMENTATION
    *       CLASS lcl_server_file DEFINITION
    CLASS lcl_server_file DEFINITION
                          INHERITING FROM lcl_file.
      PUBLIC SECTION.
        METHODS:
          get_contents REDEFINITION.
      PROTECTED SECTION.
        METHODS:
          choose       REDEFINITION.
    ENDCLASS.                    "lcl_server_file  DEFINITIO
    *       CLASS lcl_server_file IMPLEMENTATION
    CLASS lcl_server_file IMPLEMENTATION.
      METHOD choose.
        DATA:
          l_i_path     TYPE dxfields-longpath,
          l_o_path     TYPE dxfields-longpath.
        CALL FUNCTION 'F4_DXFILENAME_TOPRECURSION'
          EXPORTING
            i_location_flag = 'A'  " Application server
            i_path          = l_i_path
            fileoperation   = 'R'  " Lectura
          IMPORTING
            o_path          = l_o_path
          EXCEPTIONS
            rfc_error       = 1
            OTHERS          = 2.
        IF sy-subrc = 0 AND l_o_path <> l_i_path.
          MOVE l_o_path TO _v_path.
        ELSE.
          RAISE no_path_chosen.
        ENDIF.
      ENDMETHOD.                    "choose
      METHOD get_contents.
        DATA: l_line   LIKE LINE OF rt_contents,
              l_osmsg  TYPE string.
        CHECK NOT _v_path IS INITIAL.
        OPEN DATASET _v_path FOR INPUT
                                 IN TEXT MODE
                                 MESSAGE l_osmsg.
        IF sy-subrc <> 0.
          MESSAGE e000(oo) WITH l_osmsg
                           RAISING read_error.
        ELSE.
          DO.
            READ DATASET _v_path INTO l_line.
            IF sy-subrc = 0.
              APPEND l_line TO rt_contents.
            ELSE.
              EXIT.
            ENDIF.
          ENDDO.
          CLOSE DATASET _v_path.
        ENDIF.
      ENDMETHOD.                    "get_contents
    ENDCLASS.                    "lcl_server_file IMPLEMENTATION
    *       CLASS lcl_pc_file DEFINITION
    CLASS lcl_pc_file  DEFINITION
                       INHERITING FROM lcl_file.
      PUBLIC SECTION.
        METHODS:
          get_contents REDEFINITION.
      PROTECTED SECTION.
        METHODS:
          choose       REDEFINITION.
    ENDCLASS.                    "lcl_pc_file  DEFINITIO
    *       CLASS lcl_pc_file IMPLEMENTATION
    CLASS lcl_pc_file IMPLEMENTATION.
      METHOD choose.
        DATA:
          l_i_path     TYPE dxfields-longpath VALUE 'C:\',
          l_o_path     TYPE dxfields-longpath.
        CALL FUNCTION 'F4_DXFILENAME_TOPRECURSION'
          EXPORTING
            i_location_flag = 'P'  " PC
            i_path          = l_i_path
            fileoperation   = 'R'  " Lectura
          IMPORTING
            o_path          = l_o_path
          EXCEPTIONS
            rfc_error       = 1
            OTHERS          = 2.
        IF sy-subrc = 0 AND l_o_path <> l_i_path.
          MOVE l_o_path TO _v_path.
        ELSE.
          RAISE no_path_chosen.
        ENDIF.
      ENDMETHOD.                    "choose
      METHOD get_contents.
        CHECK NOT _v_path IS INITIAL.
        CALL METHOD cl_gui_frontend_services=>gui_upload
          EXPORTING
            filename                = _v_path
          CHANGING
            data_tab                = rt_contents
          EXCEPTIONS
            file_open_error         = 1
            file_read_error         = 2
            no_batch                = 3
            gui_refuse_filetransfer = 4
            invalid_type            = 5
            no_authority            = 6
            unknown_error           = 7
            bad_data_format         = 8
            header_not_allowed      = 9
            separator_not_allowed   = 10
            header_too_long         = 11
            unknown_dp_error        = 12
            access_denied           = 13
            dp_out_of_memory        = 14
            disk_full               = 15
            dp_timeout              = 16
            OTHERS                  = 17.
        IF sy-subrc <> 0.
          RAISE read_error.
        ENDIF.
      ENDMETHOD.                    "get_contents
    ENDCLASS.                    "lcl_pc_file IMPLEMENTATION
    * Data
    DATA: gr_file          TYPE REF TO lcl_file.
    * Main Program
    START-OF-SELECTION.
    *   Get text lines from file
        IF p_srv = abap_true.
          CREATE OBJECT gr_file
            TYPE
              lcl_server_file
            EXCEPTIONS
              no_path_chosen  = 1.
        ELSE.
          CREATE OBJECT gr_file
            TYPE
              lcl_pc_file
            EXCEPTIONS
              no_path_chosen = 1.
        ENDIF.
    On a 4.6c system this code gave me a dump, while on my NW7.0 SP it doesn't even activate with the following error:
    You cannot call abstract methods in the "CONSTRUCTOR" method.
    - Following some suggestions from Java forums i've tried to define the constructor in the base class as PROTECTED or PRIVATE instead, then calling super->constructor from the subclasses, but I get this error in german:
    Sichtbarkeit des Konstruktors darf nicht spezieller als die Sichtbarkeit der Instanzerzeugung (CREATE-Zuzatz) sein.
    which Altavista translates like:
    Visibility of the constructor may not be more special than the
    visibility of the instance production (CREATE Zuzatz).
    - I've also thought of defining the CHOOSE method as a class (not instance) one, then calling it before creating the file object which maybe solves the problem, but I see that approach more "procedural oriented" which i'm trying to avoid.
    - Of course I could define a constructor for each subclass, but both would have exactly the same code.
    I'm really lost on how should I code this. My main focus is on avoiding code dupplication.
    I hope someone with more OO experience can see what I'm trying to do and sheds some light.
    Many thanks for reading all this!

    Dear Alejandro,
        When i saw your code, you are trying to access an astract method CHOOSE(which is actually implemented in sub class) from the constructor of the base class which is not possible.  By this time, we don't know which sub class it is refering to, so it gives an error.   I see two solutions for this..
    1.  To define constructor in sub class and call the choose method from the consturctor of the sub class(which in this case is reputation of the same again for each sub class)
    2.  Remove the calling of choose method from the constructor of the main class and call it separately(after creating the object).   By now we know which sub class we are refering to.   I would have designed the program in the following way.
    *       CLASS lcl_file DEFINITION
    CLASS lcl_file DEFINITION ABSTRACT.
      PUBLIC SECTION.
        METHODS:
          constructor
            IMPORTING
              i_path  TYPE string OPTIONAL
            EXCEPTIONS
              no_path_chosen,
          get_contents ABSTRACT
            RETURNING
              value(rt_contents) TYPE string_table
            EXCEPTIONS
              read_errorm,
          set_path ABSTRACT
            EXCEPTIONS
              no_path_chosen.
      PROTECTED SECTION.
        DATA:
          _v_path        TYPE string.
    *    METHODS:
    *      choose ABSTRACT
    *        EXCEPTIONS
    *          no_path_chosen,
    *      set_path ABSTRACT
    *        IMPORTING
    *          i_path  TYPE string.
    ENDCLASS.                    "lcl_file DEFINITION
    *       CLASS lcl_file IMPLEMENTATION
    CLASS lcl_file IMPLEMENTATION.
      METHOD constructor.
        IF i_path IS SUPPLIED.
          _v_path = i_path.
    *      CALL METHOD set_path
    *        EXPORTING
    *          i_path = i_path.
    *    ELSE.
    **---->>>> PROBLEM CALL - CAN'T BE DONE!!
    *      CALL METHOD choose
    *        EXCEPTIONS
    *          no_path_chosen = 1.
    *      IF sy-subrc = 1.
    *        RAISE no_path_chosen.
    *      ENDIF.
        ENDIF.
      ENDMETHOD.                    "constructor
    * METHOD set_path.
    *    _v_path = i_path.
    * ENDMETHOD.                    "set_path
    ENDCLASS.                    "lcl_file IMPLEMENTATION
    *       CLASS lcl_server_file DEFINITION
    CLASS lcl_server_file DEFINITION
                          INHERITING FROM lcl_file.
      PUBLIC SECTION.
        METHODS:
          get_contents REDEFINITION,
          set_path     REDEFINITION.
    *  PROTECTED SECTION.
    *    METHODS:
    *      choose       REDEFINITION.
    ENDCLASS.                    "lcl_server_file  DEFINITIO
    *       CLASS lcl_server_file IMPLEMENTATION
    CLASS lcl_server_file IMPLEMENTATION.
      METHOD set_path.
        DATA:
          l_i_path     TYPE dxfields-longpath,
          l_o_path     TYPE dxfields-longpath.
        CHECK _v_path IS INITIAL.
        CALL FUNCTION 'F4_DXFILENAME_TOPRECURSION'
          EXPORTING
            i_location_flag = 'A'  " Application server
            i_path          = l_i_path
            fileoperation   = 'R'  " Lectura
          IMPORTING
            o_path          = l_o_path
          EXCEPTIONS
            rfc_error       = 1
            OTHERS          = 2.
        IF sy-subrc = 0 AND l_o_path  = l_i_path.
          MOVE l_o_path TO _v_path.
        ELSE.
          RAISE no_path_chosen.
        ENDIF.
      ENDMETHOD.                    "set_path
      METHOD get_contents.
        DATA: l_line   LIKE LINE OF rt_contents,
              l_osmsg  TYPE string.
        CHECK NOT _v_path IS INITIAL.
    *    OPEN DATASET _v_path FOR INPUT
    *                             IN TEXT MODE
    *                             MESSAGE l_osmsg.
        IF sy-subrc  = 0.
    *      MESSAGE e000(oo) WITH l_osmsg
    *                       RAISING read_error.
        ELSE.
          DO.
            READ DATASET _v_path INTO l_line.
            IF sy-subrc = 0.
              APPEND l_line TO rt_contents.
            ELSE.
              EXIT.
            ENDIF.
          ENDDO.
          CLOSE DATASET _v_path.
        ENDIF.
      ENDMETHOD.                    "get_contents
    ENDCLASS.                    "lcl_server_file IMPLEMENTATION
    *       CLASS lcl_pc_file DEFINITION
    CLASS lcl_pc_file  DEFINITION
                       INHERITING FROM lcl_file.
      PUBLIC SECTION.
        METHODS:
          get_contents REDEFINITION,
          set_path     REDEFINITION.
    *  PROTECTED SECTION.
    *    METHODS:
    *      choose       REDEFINITION.
    ENDCLASS.                    "lcl_pc_file  DEFINITIO
    *       CLASS lcl_pc_file IMPLEMENTATION
    CLASS lcl_pc_file IMPLEMENTATION.
      METHOD set_path.
        DATA:
          l_i_path     TYPE dxfields-longpath VALUE 'C:\',
          l_o_path     TYPE dxfields-longpath.
        CHECK _v_path IS INITIAL.
        CALL FUNCTION 'F4_DXFILENAME_TOPRECURSION'
          EXPORTING
            i_location_flag = 'P'  " PC
            i_path          = l_i_path
            fileoperation   = 'R'  " Lectura
          IMPORTING
            o_path          = l_o_path
          EXCEPTIONS
            rfc_error       = 1
            OTHERS          = 2.
        IF sy-subrc = 0 AND l_o_path  = l_i_path.
          MOVE l_o_path TO _v_path.
        ELSE.
          RAISE no_path_chosen.
        ENDIF.
      ENDMETHOD.                    "set_path
      METHOD get_contents.
        CHECK NOT _v_path IS INITIAL.
        CALL METHOD cl_gui_frontend_services=>gui_upload
          EXPORTING
            filename                = _v_path
          CHANGING
            data_tab                = rt_contents
          EXCEPTIONS
            file_open_error         = 1
            file_read_error         = 2
            no_batch                = 3
            gui_refuse_filetransfer = 4
            invalid_type            = 5
            no_authority            = 6
            unknown_error           = 7
            bad_data_format         = 8
            header_not_allowed      = 9
            separator_not_allowed   = 10
            header_too_long         = 11
            unknown_dp_error        = 12
            access_denied           = 13
            dp_out_of_memory        = 14
            disk_full               = 15
            dp_timeout              = 16
            OTHERS                  = 17.
        IF sy-subrc  = 0.
    *      RAISE read_error.
        ENDIF.
      ENDMETHOD.                    "get_contents
    ENDCLASS.                    "lcl_pc_file IMPLEMENTATION
    * Data
    DATA: gr_file          TYPE REF TO lcl_file.
    * Main Program
    START-OF-SELECTION.
    *   Get text lines from file
      IF abap_true = abap_true.
        CREATE OBJECT gr_file
          TYPE
            lcl_server_file
          EXCEPTIONS
            no_path_chosen  = 1.
      ELSE.
        CREATE OBJECT gr_file
          TYPE
            lcl_pc_file
          EXCEPTIONS
            no_path_chosen = 1.
      ENDIF.
      gr_file->set_path( ).
    Regards
    Kesava
    Edited by: Kesava Chandra Rao on Mar 19, 2008 11:44 AM

  • Abstract method called in an abstract class

    Hello,
    I am writing some code that I'd like to be as generic as possible.
    I created an abstract class called Chromozome. This abstract class has a protected abstract method called initialize().
    I also created an abstract class called Algorithm which contains a protected ArrayList<Chromozome>.
    I would like to create a non abstract method (called initializePopulation()) which would create instances of Chromozome, call their method initialize() and full the ArrayList with them.
    In a practical matter, only subclass of Algorithm will be used, using an ArrayList of a subclass of Chromozome implementing their own version of initialize.
    I have been thinking of that and concluded it was impossible to do. But I'd like to ask more talented peaple before forgetting it !
    Thanks,
    Vincent

    Ok, let's it is not impossible, juste that I had no idea of how doing it :-)
    The difficulty is that Algorithm will never have to deal with Chromozome itself, but always with subclass of Chromozome. This is usually not an issue, but in that case, Algorithm is required to create instances of the desired subclass of Chromozome, but without knowing in advance wich subclass will be used (I hope what I say makes any sense).
    Actually I may have found a way in the meantime, but maybe not the best one.
    I created in Algorithm an abstract method :
    protected abstract Chromozome createChromozome()The method initializePopulation will call createChromozome instead of calling directly the constructor and the initialize() method of Chromozome.
    Then subclass of Algorithm will implement the method createChromozome using the desired subclass of Chromozome.

  • [OO Concept] Make a parent class perform code after constructor calls?

    Hello,
    I want to do something like this:
    class Parent {
       Parent(){
          // do some parent stuff
       private void postConstruction(){
          // parent and all subclasses have completed construction, now do some post-construction code
          // analyze some of the child construction stuff
    class Child extends Parent{
         Child(){
           // do some child stuff
         public static void main(String[] args){
              Child child = new Child();
    }I assume I am having this problem because of bad OO design. What would be the problem way to design this so that the parent can check the values of its members after all of its subclasses have been constructed?
    Thanks..

    You can certainly call the postConstruction() method from your Parent constructor. But then (as you can see) it will be called before anything in your Child constructor. Bad design? Yes, bad design of the Java language in my opinion. It should have allowed the user to control the order of initialization more. But it doesn't. So you can't do that. Which basically means you can't use the constructor to construct your objects. You'll have to call some other method where you aren't required to call super.postConstruction() at the beginning.

  • Call an abstract method

    Hello developers,
    I am trying to implement the following (simple) classes:
    class abstract_class definition abstract
         public
         create public .
         protected section
              methods parse_string abtract
                   importing
                        value(i_str) type string.
              methods to_table.
    endclass.
    class abstract_class implementation.
         method to_table.
              me->parse_string( str ).
         endmethod.
    endclass.
    class other_class definition
         public
              inheriting from abstract_class
              final
              create public .
         protected section.
              methods parse_string redefinition.
    endclass.
    class other_class implementation.
         method parse_string.
    *        some codes         
         endmethod.
    endclass.
    I have instantiated the class other_class. But when I call the method to_table, I am getting the error "CALL METHOD NOT IMPLEMENTED" on the method parse_string. Where is my mistake ?
    Thank you for your replies.
    Best regards,
    Nicolas

    Hello everyone,
    First of all, thank you for all your reply.
    During the night, I have thought to my problem and I found the solution. In fact, I made a (very) great mistake. Here is my code:
    CLASS abstract_class IMPLEMENTATION.
         method constructor.
              me->to_table( ).
         endmethod.
         method to_table.
              me->parse_string( a_string ).
         endmethod.
    ENDCLASS.
    CLASS other_class IMPLEMENTATION.
         method constructor.
              call method super->constructor.
         endmethod.
         method parse_string.
    *          some code
         endmethod.
    ENDCLASS.
    The problem is: I call the method parse_string of my child class (with the instruction me->parse_string) before my child class is instantiated... (Now, I can jump out the window.) Vadim Vologzhanin 's reply helped me by asking where I instantiated my child class.
    Thank you !
    Best regards,
    Nicolas

  • Can i call non -abstract method in abstract class into a derived class?

    Hi all,
    Is it possible in java to call a non-abstract method in a abstact class from a class derived from it or this is not possible in java.
    The following example will explain this Ques. in detail.
    abstract class A
    void amethod()
    System.out.println(" I am in Base Class");
    public class B extends A
    void amethod()
    System.out.println(" I am in Derived Class");
    public static void main (String args[])
    // How i code this part to call a method amathod() which will print "I am in Base Class
    }

    Ok, if you want to call a non-static method from a
    static method, then you have to provide an object. In
    this case it does not matter whether the method is in
    an abstract base class or whatever. You simply cannot
    (in any object oriented language, including C++ and
    JAVA) call a nonstatic method without providing an
    object, on which you will call the method.
    To my solution with reflection: It also only works,
    if you have an object. And: if you use
    getDeclaredMethod, then invoke should not call B's
    method, but A's. if you would use getMethod, then the
    Method object returned would reflect to B's method.
    The process of resolving overloaded methods is
    performed during the getMethod call, not during the
    invoke (at least AFAIK, please tell me, if I'm wrong).You are wrong....
    class A {
        public void dummy() {
             System.out.println("Dymmy in A");
    class B extends A {
         public void dummy() {
              System.out.println("Dymmy in B");
         public static void main(String[] args) throws Exception {
              A tmp = new B();
              Class clazz = A.class;
              Method method = clazz.getDeclaredMethod("dummy", null);
              method.invoke(tmp, null);
    }Prints:
    Dymmy in B
    /Kaj

  • Calling a method in Parent component from Title Window

    Hi all,
    I have a parent component that opens up a Title window when I
    click a button. Now I want to call a method in that parent
    component from the Title window. How do I do this in Flex? Could
    anyone give me a hint please.
    Thank you in advance for the help

    "happybrowndog" <[email protected]> wrote in
    message
    news:gctmql$4t5$[email protected]..
    > That's goddamned ridiculous. What were Flex developers
    thinking that you
    > have
    > to write a custom event to call back to a parent
    component?? Other GUI
    > libraries such as WxWidgets, Fox, Qt, Delphi, MFC,
    WinForms, etc., all
    > allow
    > you to either call via a reference to the parent object
    or submit a
    > callback
    > function into the child object. That's just basic OO
    programming. Flex
    > is
    > looking more and more ridiculous and more like Swing -
    tons of unnecessary
    > coding to do simple things.
    You absolutely _do_ have the capability to pass in a
    reference to the parent
    component, or to create a "hard" reference to
    Application.application. But
    these are not recommended practices, because anything you
    create this way is
    then tied to an environment that implements those properties
    and methods.
    Q (3): I want to run a function in my main application from
    inside my
    custom component. But when I try to refer to myFunction() in
    that
    component, I get a compile time error Call to a possibly
    undefined function
    myFunction. How can I fix this?
    A: Your component has its own scope, so it doesn't know
    anything
    about the functions in the main file. You can get around this
    by directly
    referencing the main application scope like this:
    Application.application.myFunction(). However, this makes
    your component
    tightly coupled, which is a quick way of saying that your
    component is only
    usable in an application that has a myFunction() function in
    it. You're
    better off dispatching an event from your component and
    letting the
    application decide how to handle it. For more information,
    check out the
    following resources:
    http://www.adobe.com/devnet/flex/articles/loose_coupling.html
    http://www.adobe.com/devnet/flex/articles/graduating_pt1.html
    From
    http://www.magnoliamultimedia.com/flex_examples/Amys_Flex_FAQ.pdf

  • How to call parent constructor from subtype body ?

    create or replace TYPE person as object(
    p_name varchar2(10),
    p_age NUMBER,
    p_status CHAR,
    p_addr addr_type,
    constructor function person ( p_name char, p_age NUMBER, p_status CHAR, p_addr addr_type ) RETURN SELF AS RESULT
    )NOT FINAL;
    -- subclass
    create or replace TYPE employee under person(
    e_number number,
    hire_date date,
    e_designation varchar2(15),
    constructor function employee ( e_name char, e_age NUMBER, e_status CHAR,
    e_addr addr_type, e_number NUMBER, hire_date date, e_designation CHAR )
    return SELF AS RESULT
    In this scenario how do we call parent constructor from employee type ?

    Can't You write complete example? Tried few variants but get nothing:(

  • How do you call a method from  another class without extending as a parent?

    How do you call a method from another class without extending it as a parent? Is this possible?

    Why don't you just create an instance of the class?
    Car c = new Car();
    c.drive("fast");The drive method is in the car class, but as long as the method is public, you can use it anywhere.
    Is that what you were asking or am I totally misunderstanding your question?
    Jen

  • Call a method of a class in a constructor of another class?

    Is this good programming or should i do it in another way?
    Consider an ordering system, where I have three classes, "Booking", "Table" and "Customer.
    My idea is to create a booking assigned to a table and a customer, the fields in the booking class is a Customer and a Table and the constructor assigns values to these field.
    In my Table class i have a method that books the table "bookTable(Customer customer)" and sets the availablility to 'false'.
    So my question is, is it good programming to call the method 'bookTable()' in the table class from the constructor of the Booking class? Or should i do it in another way?
    Edited by: DJHingapus on Jul 6, 2009 5:12 AM

    This sounds the wrong way around. What is the return value of bookTable(Customer)?
    I'd expect it to return a Booking object that it creates.
    Generally, you want the constructor to do as little as possible (while still only ever returning in a valid state, of course!).
    The problem you can get when you call other methods is that you introduce the possibility of other objects getting access to your object before the constructor has finished running (i.e. before the object is completely initialized). This can lead to nasty problems that are hard to find and debug.

  • How to call the grand parent constructor from the child ??

    Hi All,
    I am having 3 classes.
    Class A.
    Class B extends Class A.
    Class C extends Class A
    i need to call the constructor of ClassA from Class C. how to call ??
    Thanks,
    J.Kathir

    Each class will implicitly call the parent default constructor if you do not call it in the code ie
    super();However you can call any of the parent constructors ie
    class Parent {
        int value;
        Parent(int v) {
            value = v;
        public void display() {
            System.out.println(value);
    class Child extends Parent {
        Child(int v) {
            super(v);
        public static void main(String[] args) {
            Child c = new Child(10);
            c.display();
    }

  • Superclass Constructor calling overridable validation method

    I have the following class:
    public class SuperClass
       private int x;
       public SuperClass(int x)
          setX(x);
       protected void validateX(int x)
          if(x < 0)
             throw new IllegalArgumentException("x < 0");
       public int getX()
          return x;
       public void setX(int x)
          validateX(x);
          this.x = x;
    }My question is: How can I avoid calling setX(int) in the constructor besides simply dupicating validateX(int) in the constructor if I want to subclass SuperClass? [I read in a recent Java Fundamentals newsletter that calling overridable methods in superclass constructors is a bad idea int that it tends to destabilize the whole class hierarchy.  I don't want to make validateX(int) and setX(int) final.]

    Never call overrideable methods from constructors. No exceptions.
    You can use a factory method to validate your object after its created if you must.
    public class SuperClass
       private int x;
       public SuperClass()
       static SuperClass createSuperClass(int x){
             SuperClass sc = new SuperClass();
             sc.setX(x);
             return sc;
       protected void validateX(int x)
          if(x < 0)
             throw new IllegalArgumentException("x < 0");
       public int getX()
          return x;
       public void setX(int x)
          validateX(x);
          this.x = x;
    }Or you can use template method
    public class SuperClass
    private int x;
    public SuperClass(int x)
    setXImpl(x);
    final protected void validateX(int x)
    if(x < 0)
    throw new IllegalArgumentException("x < 0");
    public int getX()
    return x;
    final void setXImpl(int x){
    validateX(x);
    this.x = x;
    public void setX(int x)
    setXImpl(x);
    In general I don't like to call any overrideable methods from within my class. I try not to be anal about it though. My personal choice would be to duplicate the set method in the constructor. You should be constructing the object not using it. access instance variables not methods.

  • Conventions: creating a new constructor and calling a method on it

    Hello,
    recently i had a little discussion about the following:
    should we do it like this:
    MyClass c = new MyClass();
    c.doSomething();
    or
    new MyClass().doSomething();
    now i also wanted to know if you should avoid one of the following, like do they use (in business programs for example) the first or rather the second.
    I know it doesn't really matters, but if some1 could give some information about this little thing i would be grateful
    Thanks in advance

    796870 wrote:
    the static part makes sense as i only call one method at that instance.. :DKayaman also said:
    ... if you're creating an instance and throwing it away right afterwards,Which suggests that there may be no reason to create an instance in the first place. Logic Dictates: IF there is no reason to create an instance, other than to call a method ONE TIME, then...
    maybe the method should be static?OP, you will need some level of critical thinking to untangle the kind of question asked. And a certain level of thinking to understand fully what experienced developers have said.

Maybe you are looking for

  • How to convert from Finder Object reference to POSIX path

    I'm new to AppleScript. I'm super close to getting what I need done, but I've ran across a snag in the middle. The error I'm getting is Can’t make quoted form of POSIX path of item 1 of {«class docf» \"filename\" of «class cfol» \"foldername\" of «cl

  • Hp pavilion p7-1167c windows 7 64bit

    Boots up most of the time with a "Default" desktop. File folders etc. are no longer on desktop but appear in program list. Fire Fox browser is in default with no saved information. Sometimes after various restore operations my regular desktop appears

  • Stock In Transit In IM (MMBE) Not Showing in WM

    Hi SAP Experts, The user used VLMOVE in moving the material from storage location to storage location using movement type 313. The destination storage location is in a different warehouse. In IM, it posted in the transfer storage location or stock in

  • Standard reports in R/3 needed in MSS

    Hi Experts, I had created transactional iviews for some Time Mgt reports like PT64, PT90, PT90_ATT, PT65 and Travel Mgt reports like S_AHR_61016405, S_AHR_61018613. The issue here is, Managers are able to view all employees data and not restricted to

  • Best practice for storing user's generated file?

    Hi all, I have this web application that user draws an image off the applet and is able to send the image via mms. I wonder what is the best practice to store the user image before sending the mms. Message was edited by: tomdog