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

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • What's that `temp.super()`? – Bhesh Gurung Apr 05 '14 at 07:30
  • if i comment this, then it is giving an error .. – user2843171 Apr 05 '14 at 07: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. – Shekhar Khairnar Apr 05 '14 at 07:35
  • 1
    http://stackoverflow.com/a/2831521/1864688 similar stuff in that question. maybe related? – guest Apr 05 '14 at 07: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: http://stackoverflow.com/help/someone-answers – Erwin Bolwidt Apr 05 '14 at 07:57

5 Answers5

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.

Kashif Nazar
  • 20,775
  • 5
  • 29
  • 46
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

wam090
  • 2,823
  • 8
  • 33
  • 36
0

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

Ashok_Pradhan
  • 1,159
  • 11
  • 13
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.

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.

UVM
  • 9,776
  • 6
  • 41
  • 66