Results 1 to 3 of 3

Thread: Need Explanation for java code

  1. #1
    Join Date
    Dec 2009
    Posts
    7

    Need Explanation for java code

    class a
    {
    public a get()
    {
    return this;
    }

    }
    class b extends a
    {
    public static void main(String args[])
    {
    a a1=new b(){public a get(){return this;};
    System.out.println(a1.getClass.getSuperClass.getName());
    }
    }

    what is output for this program?
    I can't understand this program
    will u please explain
    a a1=new b(){public a get(){return this;};

    Above statement explains what concept?

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: Need Explanation for java code

    a a1=new b(); By this statement, A new instance of "b" has been created from its parent class that is "a". After that "get" method has been called of the calss a by doing "a.get()" . Check it again that there is no DOT between "a" and "get()" as it is a syntax of java language, so it is mandatory. And this program will not compile instead of giving any output as there are several mistakes about its syntax. Another mistake is in the System.out.println line where there is a space between "na me" of getName method.

  3. #3
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Need Explanation for java code

    As I think
    a a1=new b(){public a get(){return this;};
    In this line of code we are making object of “class a” as “a1”
    And this object a1 is referenced to “class b” which is calling the function “get()” of class a
    For understanding these just try to understand this example
    A = 10
    B = 20
    C = 30
    No if I make the object of c and make it reference to b then what will be the value of c
    If you think value of c will 30 then you are wrong, value of c will be 20. Because I am referencing the object of c to b, so the value of c will change to 20

    Now again if I make the object of b and reference to then what will the value of b and c.
    Answer is: b = 10 and c =10.
    Because object of “c” is referencing to “b” and object of “b” is referencing to “a”.
    In-directly I am referencing the object of “c” to “a”.


    As you can see in the above figure the actual memory space is only one i.e. “a” and we are pointing “b” and “c” to same memory space
    I hope this would have help you.

Similar Threads

  1. help me to understand java code (calendar java code) ?
    By so0oma in forum Software Development
    Replies: 2
    Last Post: 10-01-2011, 12:01 AM
  2. help with java code
    By unikshegz in forum Software Development
    Replies: 2
    Last Post: 24-10-2010, 02:47 PM
  3. Use Java code in PHP
    By Gajendra1 in forum Software Development
    Replies: 6
    Last Post: 12-08-2010, 10:18 AM
  4. need explanation on nested group code
    By vivekmohan in forum Software Development
    Replies: 4
    Last Post: 30-06-2009, 03:09 PM
  5. JAVA code attached with explanation
    By Daren in forum Software Development
    Replies: 1
    Last Post: 13-05-2009, 01:08 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,975,258.29700 seconds with 17 queries