Sunday, 1 July 2012

Java : Inheritance and Interfaces , A magic or what ?

In Java usually we see there are event listeners ( say OnClickListener, OnTouch, OnDataArrived  or any other ) , what we do is simply pass reference of our object to required method and it automatically calls
appropriate methods of our class based on events. Most of people think it as magic or do not think it at all. Now we are going to see after all how does it work !

All this thing is based on mainly two imoprtant properties of Java : Inheritance and Polymorphism.

To understand how the main stuff happens, you must have clear idea of these two properties. Let us dive into both one by one.

Inheritance :

Inheritance can simply be defined as inheriting properties of a class by another class. It is important to note we can inherit only  " public ,default and protected members " in same package and
only " public and protected members " in other packages. Take an example :

package first;               

public class Animal {
int defaultInt;
protected int protectedInt;
protected int age;

public void printAge(){
System.out.println("printing Animal's age");

public void animalSound(){
System.out.println("animal sound @@@@");


public class Dog extends Animal {

public void checkDefault( ) {

defaultInt = 9;                                                    // valid because Dog inhertis Animal and because of same package


public void checkProtected() {

protectedInt = 9;                                                            // valid because Dog inhertis Animal and because of same package


public void printAge(){
System.out.println("printing dog's age");

public void sound(){
System.out.println("bhow bhow");



package second;
import first.Animal;

public class Cat  extends  Animal {

public void checkDefault( ) {

defaultInt = 9;                                                    // not valid because of different package


public void checkProtected() {

protectedInt = 9;                                                            // valid because Cat  inhertis Animal


public void printAge(){
System.out.println("printing cat's age");

public void sound(){
System.out.println("meaau meaau");


Java allows us to define an object by :

ClassName object = new ClassName();


SuperClassName object = new SubClassName();                                    // valid , but we are restricting our object to call only methods and properties present in class SuperClassName


SubClassName object = new SuperClassName();                                    // invalid



Now coming to Polymorphism :

Simplest Definition will be : one name , multiple behavior.

Like we can have more than one same name function in a class, only diefference we  make is between their parameters.

These functions are called overloaded functions.

But there is one more term called overriding, it come into existance during inheritance. (I am not gonna explain overriding in detail here)

Simply Overriding mean having same signature of a function in both superclass and subclass with rules specified by Java.

As we saw above Java allows us :

SuperClassName object = new SubClassName();

considering above Animal example:

Animal animal = new Cat();

but now we can not call any cat specific function, since our reference is through Animal. We are restricted to call only functions present in Animal.

i.e :   animal.sound()                              // it is invalid

            animal.printAge()                       // is valid

            animal.animalSound()                // is valid

as expected
            animal.animalSound() will print "animal sound @@@@"

            but what do you expect on calling :

            animal.printAge();                      // printing animal's age   ??

but here polymorphism comes into picture

and output is :   printing cat's age

it mean in Java at compile time we can call only those methods which are present in Reference class, but at runtime if Actual class of which object was initialized ( using new )
overrides function, Java calls overridden function.

And that is how EventListener works in Java.

As you might have noticed almost all EventListeners are interfaces which incorporates some abstract functions that you might be interested if that event happens. So what actually happens is when we implement any
such interfacewe do implement all those functions. and I assume you are familiar with following term:

 ( YourClass impements OnClickListener )

button.setOnClickListener(this);              // assuming you are having any button in your class and want to know when its click event happens, this way you register your class and binds

Now have you ever wondered what magic happens at the other end (In Button class) , how button class knows about your object and passes proper events to your implemented function

Answer is polymorphism that we saw just in above example (Animal)

In Button class , It do nor need your object but it needs OnClickListener Object and since your class is implementing the same, so it becomes the required

Now in Button Class any whenever event happens, it just simply calls


and due to polymorphism, your class function gets called.

Any question , you can email me at :


1 comment:

  1. What is polymorphism?

    Ability of an java object to take more than one form is polymorphism.

    What does that mean?

    Let's take an practical example to understand this

    Say we have an living being called animal who is a horse .Now I can say

    Horse is running
    Animal is running

    both are correct .Thus single living being is taking two form here

    1)Horse 2)Animal

    Let's understand this by code

    public class Animal{


    public class Horse extends Animal{

    Let us understand more on polymorphism here

    Polymorphism Simplified