Tuesday, December 2, 2014

Life As I Know It - Cheerio '14

As this year is almost over, I want to share some thoughts with you Jedis. Don't take failures as a stumbling block in your life. It's ok if you got things messed up, it's ok if you were rejected like hell, it's ok if you made yourself ashamed and ridiculed of yourself, it's ok if a low point came in your life and it's ok to allow yourself some time to heal before you take that next step. But remember that life is not a staircase, it's a maze. You can modify and alter your journey along the way. Be flexible and open to alternative pathways. It may lead you down some wonderful roads. Keep up the fighting spirit and know that it's not over until it's over!

And now to my '14: 
My sweetheart '14 before we say Goodbye, I want you to know something, even though you know it already my brave Lady. You will always stay loved and remembered. Here something dedicated to you.

                                                                 Happy new year!!

Thursday, November 20, 2014

Inheritance – “A type of” relation NOT “contains” relation

Many times people confuse inheritance with “contains” relationship, which is not correct at all. Inheritance is “A type of” relationship not “contains” relationship. So a child is a superset of Parent’s abilities.

Example: Each fox is an Animal. But the other way is not always true (Not every Animal is Fox). Hence, Fox is a type of Animal.

class  Animal{

                                    1.   Ability of walking

}

class Fox extends Animal{

                         1.   Inherited abilities: Ability of Animal (walking)

                         2.    It’s own abilities (like hunting)

}

Now let's try to digest few concepts here.
When I write this :  Animal  a = new Fox(). What does it mean?
It means Fox is also an Animal or  every Fox is a type of Animal which makes sense too.

What about :   Fox  f = new Animal() 
It means Animal is a type of Fox or every Animal is a Fox, which is absolutely incorrect.

So "Fox is an Animal" is fine but the reverse doesn't make sense. For example, Apple is a Fruit, Car is a Vehicle etc. Remember inheritance is uni-directional. For example, House is a Building. But Building is not a House.

Let's formulate our finding in terms of coding: A reference to child class object can't refer to the parent class object. Whereas a reference to parent class object can refer to child class object.

Example 1: 
       Animal a =  new Fox();     //True
       Fox  f = new Animal();      //False (Compilation error)

Example 2:
       Animal a = new Animal();
       Fox f = new Fox();
       a = f;               //True
       f = a;               //False (Compilation error)

So we learned few things here: 
A child class object is inherently a parent class object. In simple terms, objects/reference of parent class can hold objects of child class but the reverse is not true.

Example 3:
public class TestMain {

       public static void main(String[] args) {

              Animal a = new Fox();

              a.walk();

              ((Fox)a).hunt(); //need to typecast because reference "a" doesn't hold "hunt" method

       }

}



class Animal{

       void walk(){

              System.out.println("Animal can walk");

       }

}



class Fox extends Animal{

       void hunt(){

              System.out.println("Fox can hunt");

       }
} 

Output:
Animal can walk
Fox can hunt


Few points to mention here:
1.  A subclass can't inherit private members of Superclass obviously.
2. Try to keep all variables private or protected and use public accessor(set/get) methods to access them.
3. On a light note: Initially don't try to learn this concept with "child" and "parent" example as you might get confused. Playing with a dog or a cat is safe and easier. :)

Tuesday, November 18, 2014

Difference between method overloading and method overriding

Method overloading in Java occurs when two or more methods in the same class have the exact same name but different parameters.  Overloading is a compile time (static) phenomenon because the compiler is aware of exactly which method you are calling based on the number/type of parameters.
 
class Animal{
       void test(){
              System.out.println("Inside Animal class with no aruguments");
       }
      
       void test(int a){
              System.out.println("Inside Animal class with int aruguments");
       }
}

public class TestMain {
       public static void main(String[] args) {
              Animal a = new Animal();
              a.test();
              a.test(5);
       }

Output:
Inside Animal class with no aruguments
Inside Animal class with int aruguments

In this case, the compiler knows exactly which “test” method we are calling, based on the number/type of parameters.

Overriding methods is completely different from overloading methods. If a derived class requires a different definition for an inherited method, then that method can be redefined in the derived class. This would be considered overriding. An overridden method would have the exact same method name, return type, number of parameters, and types of parameters as the method in the parent class, and the only difference would be the definition of the method. Overriding is an example of dynamic (run-time) polymorphism. This is due to the fact that the compiler doesn't necessarily know what type of object is being passed in at compile-time.

class Animal{
       void test(){
              System.out.println("Inside Animal class with no aruguments");
       }
      
       void test(int a){
              System.out.println("Inside Animal class with int aruguments");
       }
}

class Dog extends Animal{
       void test(){
              System.out.println("Inside Dog class with no arguments");
       }
}

public class TestMain {
       public static void main(String[] args) {
              Animal a = new Dog();
              a.test();
              a.test(5);
       }

}

Output:
Inside Dog class with no aruguments
Inside Animal class with int aruguments

Here, method dispatching is performed in two steps:
  • The first one is done at compile time with the static information available, the compiler will emit a call for the signature that matches best your current method parameters among the list of overloaded methods in the declared type of the object the method is invoked upon.
  • The second step is performed at run time, given the method signature that should be called (previous step), the JVM will dispatch it to the most concrete overridden version in the actual type of receiver object.

Friday, June 20, 2014

Built-in VNC in MAC

I recently noticed there is a built in VNC in mac with name “Screen Sharing” app. (tasted with MAC 10.9.3)

Location of the app: “/System/Library/CoreServices” [Open Finder > Choose “GO” > "Go to Folder"]

Find the “Screen Sharing” app and drag it to dock bar or Desktop for easy access.
Features of this app: 
  • Multiple VNCs can be opened. 
  • Recent VNC info and password saving. This feature is missing in RealVNC application available in App Store.
  • Simple and less crashes.
  • Free!
While connecting to any VNC we have to give absolute port number, which starts with 59 always. 
For ex: bgl-mitp-013:01 vnc can be accessed with bgl-mitp-013:5901

Other option to launch this application is to use the “Connect to Server” keyboard shortcut that is available in the OS X Finder (just hit Command + K) or in web browser, type “vnc://” in the URL bar. Hit Return and the Screen Sharing app will be launched immediately.

Sunday, June 1, 2014

Some Basic How Mathematics

• Why does zero factorial (0!) equal 1 ?
   Because there is exactly one way to do nothing.

• How (a+b)2 = a2  + 2ab + b2 ?
   We can prove it geometrically.

Step 1: Draw a line of length (a + b)
             a              b
           –––|––––––––––

Step 2: Let's draw a square having sides of length (a + b). Area of this square will calculate the value of
(a+b)2




This is a square consisting two rectangular and two square parts.








Area of square part 1 = a2
Area of rectangular part 2 = ab
Area of rectangular part 3 = ab
Area of square part 4 = b2
So, Area of square of length (a+b) = (a+b)2 = a2 + 2ab + b2
i.e. (a+b)2 = a2 + 2ab + b2
Hence Proved.

Thursday, April 3, 2014

A note on iPython

In iPython, there is an Input/output caching system that offers numbered prompts (In/Out). All input is saved and can be retrieved as variables (besides the usual arrow key recall).

  The following GLOBAL variables always exist (so don't overwrite them!):
  _i: stores previous input.
  _ii: next previous.
  _iii: next-next previous.
  _ih : a list of all input _ih[n] is the input from line n.

Anywhere in code, don't use these variables, otherwise it would make very messy situation while debugging the code.

Tuesday, January 21, 2014

Discontinuous subnet masks

The subnet mask for a particular network is 255.255.31.0. Which of the following pairs of IP addressed could belong to this network? (GATE 2003)

A: 172.57.88.62 & 172.56.87.233
B: 10.35.28.2 & 10.35.29.4
C: 191.203.31.87 & 192.234.31.88
D: 128.8.129.43 & 128.8.161.55

Now here a lot of people get suspicious about the validity of this given subnet mask (31 = 00011111 ).
As according to the definition of subnet mask (from many renowned books, personally read 3 of them), this is a 32-bit number(for ipv4) where all the bits of the network part are represented as “1”and all the bits of the host part are represented as “0”. That means all 0’s should be following 1’s in a subnet mask.

Then that means this given subnet mask is wrong?
The answer is No. This subnet mask is completely fine. This is an example of a discontinuous subnet mask, which is no longer supported on most routers. Discontinuous subnet masks are still supported on some network access control devices for features like ACLs. (Sometimes, notably on Cisco routers, the subnet masks are written inverted, in this case, so you'll see 0.0.0.255, for example, to mean a /24). But practically these are not being used a lot nowadays. So Many people are unaware of this.

Those authors are also not wrong in their definition of a subnet mask. They are only referring to the latest trend being followed in Networking.

Now back to the question. The principle is the same: convert the dotted-quad IP addresses and mask to 32-bit unsigned integers and AND each address with the mask. If the results are the same, they're in the same subnet. 

NM = 255.255.31.0
• Hence, first, two parts of the n/w have to be same. Last part(third & fourth) can be anything.  This removes the possibility for A and C options.
• Now for B, consider the third part:
28 = 0001 1100
31 = 0001 1111
----------------------------
        0001 1100
 
29 = 0001 1101
31 = 0001 1111
----------------------------
        0001 1101
Last 5 bits are different. So they are not on the same network. 


• Now for D, consider the third part:
129 = 1000 0001
31  =  0001 1111
---------------------------
          1000 0001 


161 = 1010 0001
31   = 0001 1111
--------------------------
          1000 0001
Last 5 bits(network part) are same. Hence, the correct answer is D.

Friday, January 3, 2014

One Sided Love Stories

Some love stories start when they meet each other...
But then most love stories stop the moment they get to know each other...

As time passes by it feels as though the best love stories with fondest memories,
will always be the ones which were one sided love stories....
No promises were made or broken nor were there any bad memories,
those will always be the days of the beautiful one sided love stories....

The mystery and aura surrounding the angel entices you,
but you still wonder if cupid actually managed to hit you...
Her ever changing facebook profile captivates you,
but you wont send her a request fearing "who are you"...

You feel happy and you blush when you look at her...
You slowly turn around not to attract attention, but manage to take a peak at her...
The way she brushes her hair aside becomes your most awaited moment while staring at her,
but the moment she passes in front of you, you act as if you never looked at her....

With no expressions and expectations comes no rejections or dejections or depressions...
She'll always be "the one" rather than ending up being just another one....

One of the most treasured love stories will always be the one sided love stories....

#a poetry in diary.
#courtesy: prsnnaik