Saturday, September 8, 2012

Abstractness

First of all lets explore it non-technically !

What does 'abstract' mean ?

well abstract is something on your mind ! you dream of a house, its abstract. You build a house, its concrete.

Now its techies' dinner time !


Please declare abstract before you present your dream.

abstract class MyDreamHouse{

}


now you want workers to build ? You are given workers ! Now tell them methods !

wait ! you dont know how to implement them or want others to do it for you ? leave them abstract  and put a semicolon(;) after the prototype !


abstract class MyDreamHouse{
    abstract void makeLift(Object o);
}


Hmm  ! you know something ! you can paint your gate ! :P ok do it yourself ! ofcourse you cant declare it abstract ! seems legit !

abstract class MyDreamHouse{
    abstract void makeLift(Object o);
    void paintGate(Object o){
       
//your painting code goes here          
    }

   
  //rest of the works are straight forward; you declare abstract methods, you define concrete methods
}



Let your friend extend your dream !




class HulkHouse extends MyDreamHouse{

    @Override
    void makeLift(Object o) {
        //hulk builds lift
    }
  
}


He might also doesn't know how to makeLift ! so he dont dare to implement it ! Poor Hulk ! he must declare his class abstract too !



abstract class HulkHouse extends MyDreamHouse{

    void buildGarrage(Object o){
                 
    }
  
    abstract void attachDoors(Object o);
  
}


almost done !

we know there is a restriction on implementing interfaces. We must implement all the methods declared in the interface ! okay ! if we dont want to ? We get permissions paying respect to our friend 'abstract' ! just put him before the class declaration ! or get error ! :P



abstract class BlaBla implements Runnable{

}


ok you might expect your son to implement the method u must implement !




class SonBlaBla extends BlaBla{

    public void run() {
       
    }
   
}


or you should do as below to avoid error



class BlaBla implements Runnable{    
 public void run() {
       
    }

}


One very important thing ! you cannot instantiate abstract classes !

like : MyDreamHouse myhouse1=new MyDreamHouse();  //compilation error




Sad thing ! Dont cry ! Dont panic ! You still can use ur class type in main or  other relevant places.

Let me show how !

A complete code listing:


abstract class MyDreamHouse{
    abstract void makeLift(Object o);
    void paintGate(Object o){
        //your painting code goes here
    }
}

abstract class HulkHouse extends MyDreamHouse{

    void buildGarrage(Object o){

    }

    abstract void attachDoors(Object o);

}

class RealHouse extends HulkHouse{

    @Override
    void attachDoors(Object o) {

    }

    @Override
    void makeLift(Object o) {

    }

}

class abstractTester{
    public static void main(String[] args){

        MyDreamHouse myHouse1; // its ok ! coz no instances is created!

        HulkHouse hulkHouse1; // its ok ! coz no instances is created!

        RealHouse realHouse1=new RealHouse();
        RealHouse realHouse2=new RealHouse();

        myHouse1=realHouse1;
        myHouse1.makeLift("Lift 1"); //calls its child realHouse1's method

        hulkHouse1=realHouse2;
        hulkHouse1.makeLift("Lift 2");

    }
}

code Link : http://pastebin.com/LXByEK7u