Friday, September 25, 2009

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

No comments:

Post a Comment