Results 1 to 11 of 11
  1. #1
    mitra is offline Member
    Join Date
    Jun 2011
    Posts
    59
    Rep Power
    0

    Default understanding class

    Hi everybody........
    Please make me understand of the following program line by line, basically from line 8.

    class Program21_21 {
    public void m(){
    System.out.println("m method in Program21_21 class");
    }
    }


    public class Program21{
    public Program21_21 m(){
    return new Program21_21(){
    public void m(){
    System.out.println("m method in Program21 class");
    }
    };
    }

    public static void main(String args[]){
    Program21_21 obj = new Program21().m();
    obj.m();
    }
    }


    output is -- m method in Program21 class

    this program is not clear to me.
    Thanks.........

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Who wrote the code?

    db

  3. #3
    mitra is offline Member
    Join Date
    Jun 2011
    Posts
    59
    Rep Power
    0

    Default

    output is -- m method in Program21 class

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default

    Which method prints that message?

  5. #5
    mitra is offline Member
    Join Date
    Jun 2011
    Posts
    59
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Which method prints that message?
    from method m() of class Program21


    In this program there are two class --
    1. Program21_21

    2. Program21

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default

    What class does the obj variable refer to? Which method will be called in main?

  7. #7
    mitra is offline Member
    Join Date
    Jun 2011
    Posts
    59
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    What class does the obj variable refer to? Which method will be called in main?
    I'm totally confuse about these code.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default

    Here's a rewrite that should do the same thing:
    Java Code:
    // output is -- m method in Program21 class
    
    
    public class TestInheritance {
    
      static class Program21_21 {
        public void m(){
          System.out.println("m method in Program21_21 class");
        }
      }
      
      
      static class Program21{
      
        // a method
        public Program21_21 method(){
          // Create new object and return it
          return new Program21_21(){
            @Override 
            public void m(){
              System.out.println("m method in Program21 class");
            }
          };
        }
      } // end class
      
      public static void main(String args[]) {
        Program21 pgm21 = new Program21();  // create an instance
        Program21_21 obj = pgm21.method();  // call a method to get another object
        obj.m();                                           // call a method on new object
      } // end main
      
    } // end class Test....

  9. #9
    mitra is offline Member
    Join Date
    Jun 2011
    Posts
    59
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Here's a rewrite that should do the same thing:
    Java Code:
    // output is -- m method in Program21 class
    
    
    public class TestInheritance {
    
      static class Program21_21 {
        public void m(){
          System.out.println("m method in Program21_21 class");
        }
      }
      
      
      static class Program21{
      
        // a method
        public Program21_21 method(){
          // Create new object and return it
          return new Program21_21(){
            @Override 
            public void m(){
              System.out.println("m method in Program21 class");
            }
          };
        }
      } // end class
      
      public static void main(String args[]) {
        Program21 pgm21 = new Program21();  // create an instance
        Program21_21 obj = pgm21.method();  // call a method to get another object
        obj.m();                                           // call a method on new object
      } // end main
      
    } // end class Test....

    Thanks for ur help.
    But could u explain me actually what happen in these code ---


    static class Program21{

    // a method
    public Program21_21 method(){
    // Create new object and return it
    return new Program21_21(){
    @Override
    public void m(){
    System.out.println("m method in Program21 class");
    }
    };
    }
    } // end class

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default

    There is a class, the class has a method, the method returns a reference to another class.

  11. #11
    mitra is offline Member
    Join Date
    Jun 2011
    Posts
    59
    Rep Power
    0

Similar Threads

  1. Problem understanding class statement.
    By martijnburger in forum New To Java
    Replies: 3
    Last Post: 07-21-2011, 11:07 PM
  2. Need some understanding and help!
    By Kevinius in forum New To Java
    Replies: 8
    Last Post: 05-14-2011, 05:50 AM
  3. Understanding -- new Class() -- syntax.
    By AcousticBruce in forum New To Java
    Replies: 8
    Last Post: 12-14-2010, 12:53 AM
  4. Having trouble understanding Class Graphic
    By Bernard Robitaille in forum JCreator
    Replies: 1
    Last Post: 04-18-2009, 02:55 AM
  5. Replies: 3
    Last Post: 10-06-2008, 11:03 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •