Friday, September 25, 2009

Notes About Inner Class - Core Java

1. Innerclass nested with other class i.e. enclosing class. They are four types:
i. Static
ii. Member class
iii. Local class
iv. Anonymous Class

N.B - Inner class is a member of enclosing class like method or field.

2. Static Member Class/Interface:
i. Static class is a static member of class like static method/field.
ii. Interfaces can be defined as static member class.
iii. A static member class is not associated with any instance of the containing class (i.e., there is no this object).
iv. A static member class has access to all static members of its containing class, including private members. The reverse is true as well: the methods of the containing class have access to all members of a static member class, including the private members. A static member class even has access to all the members of any other static member classes, including the private members of those classes.
v. A static member class cannot have the same name as any of its enclosing classes. In addition, static member classes and interfaces can be defined only within top-level classes and other static member classes and interfaces.

5. Member Class

i.Member class is a member (instance) of the enclosing class and has access to any and all methods or variables of enclosing one and even the parent's this reference.
ii. A member class cannot have the same name as any containing class or package.
iii. Member classes cannot contain any static fields, methods, or classes (with the exception of constant fields declared both static and final). The reason of it as static fields, methods, and classes are top-level constructs not associated with any particular object, while every member class is associated with an instance of its enclosing class. Defining a static top-level member within a non-top-level member
class simply promotes confusion and bad programming style, so you are required to define all static members within a top-level or static member class or interface.
iv. Interfaces cannot be defined as member classes. An interface cannot be instantiated, so there is no object to associate with an instance of the enclosing class. If you declare an interface as a member of a class, the interface is implicitly static, making it a static member class.
v. To access the this reference of the containing class use classname.this

6. Local classes are declared within a block of code and are visible only within that block, just as any other method variable.

7. An anonymous class is a local class that has no name.

8. Advantages of inner classes can be an object-oriented advantage, an organizational advantage, and a call-back advantage.

i. Lets prove the above three advantages:

Object-Oriented
---------------
Let's look at the member class. Since its instance is a member of its parent instance, it has access to every member and method in the parent. At first glance, this might not seem like much; we already have that sort of access from within a method in the parent class. However, the member class allows us to take logic out of the parent and objectify it.

Organizational
--------------

Inner classes allow us to further organize our package structure through the use of namespaces. Instead of dumping everything in a flat package, classes can be further nested within classes. Explicitly, without inner classes, we were limited to the following hierarchy structure

Callback
--------

Inner member classes and anonymous classes both provide a convenient method for defining callbacks. The most obvious example relates to GUI code.

As with anything else, you have to take the good with the bad. Inner classes have their disadvantages. From a maintenance point of view, inexperienced Java developers may find the inner class difficult to understand. The use of inner classes will also increase the total number of classes in your code. Moreover, from a development point of view, most Java tools come up a bit short on their support of inner classes.

Ref: http://docstore.mik.ua/orelly/java-ent/jnut/ch03_10.htm

Notes about Inheritance - CoreJava

1. All the classes implecitly extends from Object class as it defines and implements common behaviour to the all classes.
2. You can declare a field in the subclass with the same name as the one in the superclass, thus hiding it (not recommended).
3. You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it.
4. You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it.
5. You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super. Remember Constructors are not inheritable.
6. private member is accesible from the subclass indirectly. i.e. superclass is having public/protected method which access the private variables in generally its setter/getter method.
7. Remember a nested class has access to private members i.e. field/method of its enclosing class. Seems to be violated the encapsulation principles. Therefore, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the superclass.
8. Do the casting after checking with instanceof operator.

9. Method overriding (hiding)

The overriding method has the same name, number and type of parameters, and return type as the method it overrides. An overriding method can also return a subtype of the type returned by the overridden method. This is called a covariant return type. Remember the access specifier has to be same or widen/higher scope not lesser if so compile time error.

It is recommended that use @override, as it instructs the compiler that you intend to override a method in the superclass. If, for some reason, the compiler detects that the method does not exist in one of the superclasses, it will generate an error.

Another point to remember that the distinction between hiding (static) and overriding (instance) has important implications. The version of the overridden method that gets invoked is the one in the subclass. The version of the hidden method that gets invoked depends on whether it is invoked from the superclass or the subclass.

************************Summary*********************************
------------------------| Super Instance Method -----| Super Static Method
------------------------|-----------------------------|-----------------------------
Sub Instance Method ----| Overrides ------------------| compile-time error
------------------------|-----------------------------|-----------------------------
Subclass Static Method -| compile-time error ---------| Hides


Remember Static method calls on type whereas instance method calls on instance.

10. Java does not support (i.e. intensinally eliminated to make simple) multiple inheritance implementation (i.e. class). There is a well known problem in multiple inheritance i.e. diamond problem. like consider the following example

Animal class is having method called talk().

Two classes i.e. Frog and Dinosaur each extends (error in java) Animal and implements talk().

Another class Frogosaur extends both Dinosaur and Frog and trying to call talk on type Animal. Here is the problem like ambiguity arises.

11. Interfaces give you more polymorphism than singly inherited families of classes, because with interfaces you don't have to make everything fit into one family of classes

12. Advantage of composition over inheritance (composition is quite better one to go with if it fits for the requirement)

i. It's easier to change classes involved in a composition relationship than it is to change classes involved in an inheritance relationship
ii. Composition allows you to delay the creation of back-end objects until (and unless) they're needed. It also allows you to change the back-end objects dynamically throughout the lifetime of the front-end object. With inheritance, you get the image of the superclass in your subclass object image as soon as the subclass is created, and it remains part of the subclass object throughout the lifetime of the subclass.
iii. It's easier to add new subclasses (inheritance) than it is to add new front-end classes (composition), because inheritance comes with polymorphism. If you have a bit of code that relies only on a superclass interface, that code can work with a new subclass without change. This isn't true of composition, unless you use composition with interfaces.

13. Remember that inheritance refers to "is-a" relationship (permanent) therefore easy to understand. Whereas Composition refers to "has-a" relationship and gives more flexibility.

14. Interface provides great extend for polymerphism