Q1. What is static class? Why would you need a static class?
Ans. First of all, a top level class can't be static. The compiler will detect and report this error. Thus only an inner class can be declared static, which is known as nested class.
Nested top-level classes are typically used as a convenient way to group related classes without creating a new package. If your main class has a few smaller helper classes that can be used outside the class and make sense only with your main class, it's a good idea to make them nested (top-level) classes.
NOTE: A static class can have non-static members and methods.
Q2. What is the difference b/w a nested class and inner class.
Ans. First of all, a top level class can't be static. The compiler will detect and report this error. Thus only an inner class can be declared static, which is known as nested class.
Nested top-level classes are typically used as a convenient way to group related classes without creating a new package. If your main class has a few smaller helper classes that can be used outside the class and make sense only with your main class, it's a good idea to make them nested (top-level) classes.
NOTE: A static class can have non-static members and methods.
Q2. What is the difference b/w a nested class and inner class.
Ans. Static inner classes are known as nested classes and non-static inner classes are known as inner class.
Nested class:
As with class methods and variables, a static nested class is associated with its outer class. Nested class cannot refer directly to instance variables or methods defined in its enclosing class — it can use them only through an object reference. Static nested classes are accessed using the enclosing class name, i.e. OuterClass.StaticNestedClass
For example, to create an object for the static nested class, use following:
OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
Inner class:
As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.
Nested class:
As with class methods and variables, a static nested class is associated with its outer class. Nested class cannot refer directly to instance variables or methods defined in its enclosing class — it can use them only through an object reference. Static nested classes are accessed using the enclosing class name, i.e. OuterClass.StaticNestedClass
For example, to create an object for the static nested class, use following:
OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
Inner class:
As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.
Objects that are instances of an inner class exist within an instance of the outer class. Consider the following classes:class OuterClass {
...
class InnerClass { ...
}
}
To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:
OuterClass.InnerClass innerObject = outerObject.new InnerClass()
Q3. How do you get a reference to outer class in inner class and declare an object of inner class.
Ans. Using .this and .new.
Inside inner class, this would refer to inner class itself, so you access the outer class as “OuterClassName.this”
And to create an object of inner use .new i.e “o.new Iclass”
where o is object of outer class Oclass and Iclass is inner class of outer class Oclass.
Q4. What are various types of inner classes?
Ans.
1. Anonymous: Anonymous classes are declared and instantiated within the same statement. They do not have names, and they can be instantiated only once. Since an anonymous class doesn't have a normal class declaration where it's possible to use static, it cannot be declared static.
2. Local: Local classes are the same as local variables, in the sense that they're created and used inside a block. Once you declare a class within a block, it can be instantiated as many times as you wish within that block. Like local variables, local classes aren't allowed to be declared public, protected, private, or static.
3. Member: Member classes are defined within the body of a class. You can use member classes anywhere within the body of the containing class. You declare member classes when you want to use variables and methods of the containing class without explicit delegation.
The member class is the only class that you can declare static (which when declared static is known as nested class).
4. Nested top-level: A nested top-level class is a member classes with a static modifier.
Q5. How is local inner class different from anonymous class and which one you should chose?
Ans. Local inner class has a name associated (inside a method only), whereas anonymous class doesn't. Since the name of the local inner class is not accessible outside the method, the only justification for using a local inner class instead of an anonymous inner class is if you need a named constructor and/or an overloaded constructor, since an anonymous inner class can only use instance initialization.
Another reason to make a local inner class rather than an anonymous inner class is if you need to make more than one object of that class.
NOTE: Arguments passed to a method are accessible to local inner or anonymous class only if argument is being passed as final.
Q6. Anonymous inner class,
as return new Contents { ......};
Ans. What this strange syntax means is "Create an object of an anonymous class that’s inherited from Contents." The reference returned by the new expression is automatically upcast to a Contents reference.
Q7. Can inner classes be overridden?
Ans. There isn’t any extra inner-class magic going on when you inherit from the outer class. The two inner classes are completely separate entities, each in its own namespace. So, it’s still possible to explicitly inherit from the inner class.
Q8. Can you define constructors for inner classes?
Ans. Yes, but only when inner class has a name. Since anonymous classes don’t have a name, it is not possible to have a constructor. In such cases, instance initializer acts as constructor ( i.e code inside curly braces in class body). The only drawback in this case is that you can't have overloaded constructors, which is possible in case of named inner classes.
Q9. Can inner class have static members?
Ans. No
Q10. Where should you use inner classes?
Ans. Code without inner classes is more maintainable and readable. When you access private data members of the outer class, the JDK compiler creates package-access member functions in the outer class for the inner class to access the private members. This leaves a security hole. In general we should avoid using inner classes. Use inner class only when an inner class is only relevant in the context of the outer class and/or inner class can be made private so that only outer class can access it. Inner classes are used primarily to implement helper classes like Iterators, Comparators etc which are used in the context of an outer class.
Q11. Why do we need inner classes?
Ans.In case of multiple inheritance, you can inherit from only one implementation (class) and rest has to be interface.
Inner classes solve this issue, when Outer class O extends an implementation (class A) and inner class I extends other one (class B). Each inner class can independently inherit from an implementation. Thus, the inner class is not limited by whether the outer class is already inheriting from an implementation.
Interfaces solve part of the problem of multiple inheritance, but inner classes effectively allow "multiple implementation inheritance." That is, inner classes effectively allow you to inherit from more than one non-interface. Also, inner classes help us to have closure and callback functionality.
A closure is a callable object that retains information from the scope in which it was created. An inner class is an object-oriented closure, because it doesn’t just contain each piece of information from the outer-class object ("the scope in which it was created"), but it automatically holds a reference back to the whole outer-class object, where it has permission to manipulate all the members, even private ones.
With a callback, some other object is given a piece of information that allows it to call back into the originating object at some later point. One of the most compelling arguments made to include some kind of pointer mechanism in Java was to allow callbacks.
Q11. Why do we need inner classes?
Ans.In case of multiple inheritance, you can inherit from only one implementation (class) and rest has to be interface.
Inner classes solve this issue, when Outer class O extends an implementation (class A) and inner class I extends other one (class B). Each inner class can independently inherit from an implementation. Thus, the inner class is not limited by whether the outer class is already inheriting from an implementation.
Interfaces solve part of the problem of multiple inheritance, but inner classes effectively allow "multiple implementation inheritance." That is, inner classes effectively allow you to inherit from more than one non-interface. Also, inner classes help us to have closure and callback functionality.
A closure is a callable object that retains information from the scope in which it was created. An inner class is an object-oriented closure, because it doesn’t just contain each piece of information from the outer-class object ("the scope in which it was created"), but it automatically holds a reference back to the whole outer-class object, where it has permission to manipulate all the members, even private ones.
With a callback, some other object is given a piece of information that allows it to call back into the originating object at some later point. One of the most compelling arguments made to include some kind of pointer mechanism in Java was to allow callbacks.
No comments:
Post a Comment