Java Programming - Inner Classes - Discussion

Discussion Forum : 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());
    }
}
57 22
45 38
45 57
An exception occurs at runtime.
Answer: Option
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.

Discussion:
8 comments Page 1 of 1.

Aparajita Kulkarni said:   2 years ago
If we execute the code as it is not working. It says;

At least one public class is required in the main file;
Then did small modifications it gave me the answer.

abstract class AbstractTest
{
public int getNum()
{
return 45;
}
public abstract class Bar
{
public int getNum()
{
return 38;
}
}
}
public class AbstractMethodOverriding{
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());
}
}

S Kumar said:   8 years ago
@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());

Sudev said:   9 years ago
An object of abstract class never be created.

Abhinit said:   10 years ago
We can't create abstract class object.

Piyali said:   1 decade ago
We can't create instances of abstract class. It should be extended.

Sohi said:   1 decade ago
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.

Mahanthesh A V said:   1 decade ago
Can we create objects of abstract class without extending them?

Mariselvam said:   1 decade ago
Abstract can have object?

Post your comments here:

Your comments will be displayed after verification.