0

When the program is run display function gets called. But not able to understand how?

class A
{
    class B
    {
        void display()
        {
            System.out.println("display in B.....");
        }
    }
}

class Twenty extends A.B
{
    Twenty(A temp)
    {
       temp.super();
    }
    public static void main(String args[])
    {
        A obj=new A();
        Twenty abc=new Twenty(obj);
        abc.display();
    }
}

explain this program

5
  • What's that temp.super()? Commented Apr 5, 2014 at 7:30
  • if i comment this, then it is giving an error .. Commented Apr 5, 2014 at 7:33
  • It's a inheritance. In inheritance a sub class can have all public member's of super class and those can be accessible using subclass object also. Commented Apr 5, 2014 at 7:35
  • 1
    stackoverflow.com/a/2831521/1864688 similar stuff in that question. maybe related? Commented Apr 5, 2014 at 7:36
  • @user2843171 After you ask a question, you should look through the answers, vote on them, and if one of them answers your question, mark it as the answer. (Give it some time for people to answer). You have asked 6 questions, 4 of them have answers, but you haven't marked an answer for any of these. Please read: stackoverflow.com/help/someone-answers Commented Apr 5, 2014 at 7:57

5 Answers 5

2

This is as simple as a class Twenty extending a class B.

Since there is a method display in the B class, Twenty inherits this method as if this method is declared in it. This is why you are able to call the display method on an object of the class Twenty which is abc.

Sign up to request clarification or add additional context in comments.

1 Comment

what's the purpose of temp.super?
0

Simple inheritance, Twenty calling the ".super()" from its constructor and since Twenty extends A & B then calling the method "display()" will give you this result

Comments

0

As class Twenty extends from class B it can access its non private methods .(Simple )

Comments

0

You extended Twenty class from B class: class Twenty extends A.B. Than you call display() on instance of the Twenty so inherited display method is invoked.

Comments

0

You can access inner class method from an outer class. Here outer class is A and that is the object you are passing to constructor in your main method.Also, it obeys inheritance. Because of these two reasons, "display" method is getting invoked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.