Java Programming - Inner Classes - Discussion

Discussion :: Inner Classes - Finding the output (Q.No.4)

4. 

What will be the output of the program?

public abstract class AbstractTest 
{
    public int getNum() 
    {
        return 45;
    }
    public abstract class Bar 
    {
        public int getNum() 
        {
            return 38;
        }
    }
    public static void main (String [] args) 
    {
        AbstractTest t = new AbstractTest() 
        {
            public int getNum() 
            {
                return 22;
            }
        };
        AbstractTest.Bar f = t.new Bar() 
        {
            public int getNum() 
            {
                return 57;
            }
        };
        
        System.out.println(f.getNum() + " " + t.getNum());
    }
}

[A]. 57 22
[B]. 45 38
[C]. 45 57
[D]. An exception occurs at runtime.

Answer: Option A

Explanation:

You can define an inner class as abstract, which means you can instantiate only concrete subclasses of the abstract inner class. The object referenced by the variable t is an instance of an anonymous subclass of AbstractTest, and the anonymous class overrides the getNum() method to return 22. The variable referenced by f is an instance of an anonymous subclass of Bar, and the anonymous Bar subclass also overrides the getNum() method (to return 57). Remember that to instantiate a Bar instance, we need an instance of the enclosing AbstractTest class to tie to the new Bar inner class instance. AbstractTest can't be instantiated because it's abstract, so we created an anonymous subclass (non-abstract) and then used the instance of that anonymous subclass to tie to the new Bar subclass instance.


Mariselvam said: (Feb 8, 2012)  
Abstract can have object?

Mahanthesh A V said: (Mar 7, 2013)  
Can we create objects of abstract class without extending them?

Sohi said: (Sep 11, 2013)  
Hi, We can not create instance of Abstract classes. The above syntax is creating an anonymous class of type AbstractTest.

The syntax :
AbstractTest t = new AbstractTest()
{
public int getNum()
{
return 22;
}
};

Means:

We are creating a anonymous class of type AbstractTest and this anonymous class is also overriding method go from AbstarctTest.

Remember anonymous classes always are sub classes of a type or implementer of some interface.

Piyali said: (Dec 22, 2014)  
We can't create instances of abstract class. It should be extended.

Abhinit said: (Oct 13, 2015)  
We can't create abstract class object.

Sudev said: (Sep 6, 2016)  
An object of abstract class never be created.

S Kumar said: (Apr 24, 2017)  
@Sohi.

In your Expalanation it is creating an anonumous class is right.

I am just explaining it further.

// Code
AbstractTest t = new AbstractTest()
{
public int getNum()
{
return 22;
}
};
System.out.println("Through anonymous inner subclass " + t.getNum());

// Above code is creating a anonymous inner class under abstract class AbstractTest.
// In above code we are not creating the instance of AbstractTest. We are creating the instance of anonymous subclass which is hidden here.
// It can be easily figured out by refering to below code. Here, similar code is used to create a named subclass instead of anonymous subclass.

class AbstractTestExtension extends AbstractTest{
public int getNum()
{
return 380;
}
}

AbstractTestExtension ate = new AbstractTestExtension();
System.out.println("Through named inner subclass "+ ate.getNum());

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.