Friday, July 17, 2015

Prefer Composition over Inheritance

In Inheritance, a new class, which wants to reuse code, inherit an existing class, known as super class. This new class is then known as sub class. On composition, a class, which desire to use functionality of an existing class, doesn't inherit, instead it holds a reference of that class in a member variable, that’s why the name composition.

Inheritance - IS-A relation
Composition -Has-A relation

Java dosent support multiple inheritance. So, to use multiple functionality from different classes and having them as members variables. This is in a way composition. Implement classes using interfaces and use these class instances in the needed class is the best way to go.

Composition offers best test-ability of a class than inheritance.

Best examples to prove favors for composition over inheritance is Strategy design pattern and Decorator design pattern. In all these examples we do not extend to add functionality instead use member variables to do that job.

When inheritance is used the biggest drawback is that if there is any change in the super class there is high chance of the subclass to be broken in its functionality.

By using composition over inheritance one important advantage is that at any time a better implementation can be added on needed bases since a new version be added and passes the reference of this new version to the base class and it would take this new implementation.

More detailed explanation on this topic is given at link: http://javarevisited.blogspot.com/2013/06/why-favor-composition-over-inheritance-java-oops-design.html 

Thursday, April 23, 2015

Android

What happens when an app is launched:


In java the entry point of an application is static void main(String args[])method. JVM locates this method after loading classes into JVM.

In the case of Android, the JVM locates the main method in the  ActivityThread. Calling this main method will invoke your application.

Static Methods

Important Info On Static Methods from Java Documentation taken from link: 
https://docs.oracle.com/javase/tutorial/java/IandI/override.html

If a subclass defines a static method with the same signature as a static method in the superclass, then the method in the subclass hides the one in the superclass.
The distinction between hiding a static method and overriding an instance method has important implications:
  • The version of the overridden instance method that gets invoked is the one in the subclass.
  • The version of the hidden static method that gets invoked depends on whether it is invoked from the superclass or the subclass.
Consider an example that contains two classes. The first is Animal, which contains one instance method and one static method:

public class Animal {
    public static void testClassMethod() {
        System.out.println("The static method in Animal");
    }
    public void testInstanceMethod() {
        System.out.println("The instance method in Animal");
    }
}

The second class, a subclass of Animal, is called Cat:
public class Cat extends Animal {
    public static void testClassMethod() {
        System.out.println("The static method in Cat");
    }
    public void testInstanceMethod() {
        System.out.println("The instance method in Cat");
    }

    public static void main(String[] args) {
        Cat myCat = new Cat();
        Animal myAnimal = myCat;
        Animal.testClassMethod();
        myAnimal.testInstanceMethod();
    }
}
The Cat class overrides the instance method in Animal and hides the static method in Animal. The main method in this class creates an instance of Cat and invokes testClassMethod()on the class and testInstanceMethod() on the instance.
The output from this program is as follows:
The static method in Animal
The instance method in Cat
As promised, the version of the hidden static method that gets invoked is the one in the superclass, and the version of the overridden instance method that gets invoked is the one in the subclass.