Language Translator

NESTED CLASS DESCRIPTION

A class that is declared within another class or interface is called a
nested class. For example,
//a class declared within a class.
class A
{
int i=100;
//this is a nested class.
class B
{
int j=100;
}
}

//a class declared within an interface.
interface A
{
int i=100;
//this is a nested class.
class B
{
int j=100;
}
}

Q. What is a nested interface?

Ans. An interface declared within another class or interface is called a nested interface. For example,
//an interface declared within another class.
Class A
{
int i=100;
//nested interface.
interface A
{
int j=100;
void fun();
}
}

//an interface declared within another interface.
interface A
{
int i=100;
public void fun();

//nested interface.
interface B
{
int j=100;
public void fun();
}
}

Q. How many types of nested classes and interfaces are available in Java?

Ans. Nested classes and interfaces can be broadly put into two categories:
1. Inner classes
2. Non-inner classes and interfaces.

1. Inner classes comprise of
1.1 Non-static member classes.
1.2 Local classes.
1.3 Anonymous classes.

2. Non-inner classes comprise only of
2.1 Static member classes and interfaces.

Q. What is the difference between an inner class and a non-inner class?

Ans. The difference lies in the syntax of accessing inner and non-inner classes. An inner class can be accessed only via an instance of its enclosing class. For example,
class A
{
int i=100;
class B
{
int j=200;
class C
{
int k=300;
}
}
}

public class test
{
public static void main(String args[])
{
A a=new A();
System.out.println(a.i); //will print 100.

A.B b=a.new B();
System.out.println(b.j); //will print 200.


A.B.C c=b.new C();
System.out.println(c.k); //will print 300.
}
}

However, a non-inner class can be accessed directly without creating an instance of its enclosing class. For example,
class A
{
int i=100;
static class B
{
int j=200;
static class C
{
int k=300;
}
}
}

public class test
{
public static void main(String args[])
{
A a=new A();
System.out.println(a.i); //will print 100.

A.B b=new A.B();
System.out.println(b.j); //will print 200.

A.B.C c=new A.B.C();
System.out.println(c.k); //will print 300.
}
}

Q. How far can an inner class access the members of its enclosing classes?

Ans. An inner class can access all the members (including private data members) of all its enclosing classes. For example,



class A
{
private int i=100;
void fun()
{
System.out.println(i);
}

class B
{
private int j=200;
void fun()
{
System.out.println(i);
System.out.println(j);
}
class C
{
private int k=300;
void fun()
{
System.out.println(i);
System.out.println(j);
System.out.println(k);
}
}
}
}

public class test
{
public static void main(String args[])
{
A a=new A();
a.fun(); //will print 100.

A.B b=a.new B();
b.fun(); //will print 100 200

A.B.C c=b.new C();
c.fun(); //will print 100 200 300
}
}





Q. Can we declare a non-static, local or anonymous interface?

Ans. No, it is illegal to declare a non-static, local or anonymous interface. Interfaces are always declared either at the top level or as static members. For example,

Q. What is a local class?

Ans. A local class is an inner class that is defined within a block. This block could be a method body, a constructor, a local block or a static intializer. For example,

class A
{
int i=100;
void fun()
{
int j=200;
//this is a local class.
class B
{
int k=300;
}
}
}

Q. Can a nested class have the same name as that of any of its enclosing classes or interfaces?

Ans. No. For example,
class A
{
class B
{
class C
{
class A //Error here
{
}
}
}
}

Q. What is a top-level class or interface?

Ans. A top-level class or interface is one which is not nested.


Q. Nested interfaces are implicitly ______ ?

Ans. static.
For example,
class A
{
interface B
{
int j=200;
}
}

public class test
{
public static void main(String args[])
{
System.out.println(A.B.j); //will print 200
}
}

Q. Static member classes and interfaces can be nested within other ______ member or ________ classes and interfaces only.

Ans. static, top-level.
For example,
class A
{
interface B
{
}
class C
{
//not allowed inside an inner class.
interface D
{
}
}
class E
{
//not allowed inside an inner class.
static class F
{
}
}
}


Q. Can a non-static member class declare static members?

Ans. No, a non-static member class cannot declare static members. For example,
class A
{
class B
{
static int i=100; //not allowed.
static class C //not allowed.
{
}
}
}
However, a non-static member class can use static members. For example,
class A
{
static int i=100;
class B
{
int k=A.i;

void fun()
{
A.i=300;
System.out.println(A.i); //will print 300.
System.out.println(k); //will print 100.
}
}
}

public class test
{
public static void main(String args[])
{
A a=new A();
A.B b=a.new B();
b.fun();
}
}

The only exception is that, final static variables can be declared within non-static member classes. For example,
class A
{
class B
{
static final int f=500;
}

}
public class test
{
public static void main(String args[])
{
A a=new A();
A.B b=a.new B();
System.out.println(b.f); //will print 500.
}
}

Q. What access specifiers can a nested class use?

Ans. A nested class is like any other class member. Therefore it can use the following access specifiers:
1. public
2. protected
3. private.

Q. What is the scope of a local class?

Ans. A local class can be instantiated only in the block in which it is defined. Clients outside the scope of the local class cannot instantiate the local class. For example,
class A
{
void fun()
{
//this is a local class which can be instantiated only
//within the method fun().
class B
{
int i=100;
}
}
}

Q. Is it possible for a method to return an object of a local class?

Ans. Yes, a method can return an object of a local class. However, the
return type cannot be the same as the local class type, since this type is not accessible outside of the method. A supertype of the local class must be specified as the return type. This implies that in order for the objects of the local class to be useful outside the method, a local class should implement an interface or override the behavior of its supertypes. For example,

class A
{
int i=100;
public void fun()
{
}
}

class B
{
public A fun()
{
class C extends A
{
int j=200;
public void fun()
{
System.out.println(i);
System.out.println(j);
}
}
return(new C());
}
}

public class test
{
public static void main(String args[])
{
A a=new B().fun();
a.fun(); //will print 100 200.
}
}

Q. Can a local class have static members?

Ans. No, a local class cannot have static members. For example,
class A
{
public void fun()
{
class B
{
static int i=100; //not allowed.
}
}
}

Q. Can a local class have access specifiers?

Ans. No, a local class cannot use any of the access specifiers. The reason being; a local class is exists only within the block in which it is defined.

Q. Can a local class be specified with the keyword static?

Ans. No, a local class cannot be specified with the keyword static. For example,
class A
{
public void fun()
{
static class B //not allowed.
{
}
}
}

Q. What is an anonymous class?

Ans. Anonymous classes are defined at the location they are instantiated. These classes combine the process of definition and instantiation into a single step. For example,
class B
{
public void fun()
{
}
}

class A
{
int i=100;
public B fun()
{
return(new B()
{
int j=200;
public void fun()
{
System.out.println("Inside B's fun; j= "+j);
}
});
}
}

public class test
{
public static void main(String args[])
{
A a=new A();
B b=a.fun();
b.fun(); //will print 200
}
}

Q. How can an anonymous class pass arguments to its super class?

Ans. Following syntax can be used to pass arguments to its super class:
new () {member declarations}

For example,
class A
{
int i;
A(int ii)
{
i=ii;
}
void fun()
{
System.out.println("Inside A's fun; i ="+i);
}
}

class B
{
int j=200;
public A fun()
{
return(new A(10)
{
int j=20;
public void fun()
{
System.out.println("Inside A's fun; i= "+i+" and j= "+j);
}
});
}
}

public class test
{
public static void main(String args[])
{
A a=new B().fun();
a.fun();
}
}


Q. Can the static keyword be placed before an anonymous class?

Ans. No, the static keyword cannot be placed before an anonymous class.

Q. Is it possible to declare static members inside an anonymous class?

Ans. No, static members cannot be declared inside anonymous classes. However, final static variables can be declared inside an anonymous class.




























Q. What is the output of the following program?

class A
{
int i=100;
void func()
{
System.out.println("Inside A's func; i ="+i);
}

A fun()
{
class B extends A
{
int j=200;

void func()
{
i=300;
System.out.println("Inside B's func; i ="+i);
System.out.println("Inside B's func; j ="+j);
}
}
return(new B());
}
}

public class test
{
public static void main(String args[])
{
A a=new A();
a.func();

a=a.fun();
a.func();
}
}

Ans. Inside A's func; i =100
Inside B's func; i =300
Inside B's func; j =200

No comments:

Post a Comment