Showing posts with label oop. Show all posts
Showing posts with label oop. Show all posts

Tuesday, September 23, 2014

What is the difference between identity and equality in OOP?

  • identity: a variable holds the same instance as another variable.
  • equality: two distinct objects can be used interchangeably. they often have the same id.
For example:
Integer a = new Integer(1);
Integer b = a;
a is identical to b.
Integer c =  new Integer(1);
Integer d = new Integer(1);
c is equal but not identical to d.
Of course, two identical variables are always equal.
In Java, equality is defined by the equals method. Keep in mind, if you implement equals you must also implement hashCode.

Wednesday, June 4, 2014

What is the difference between Composition and Association relationship?

COMPOSITION
Imagine a software firm that is composed of different Business Units (or departments) like Storage BU, Networking BU. Automobile BU. The life time of these Business Units is governed by the lifetime of the organization. In other words, these Business Units cannot exist independently without the firm. This is COMPOSITION. (ie the firm is COMPOSED OF business units)
ASSOCIATION
The software firm may have external caterers serving food to the employees. These caterers are NOT PART OF the firm. However, they are ASSOCIATED with the firm. The caterers can exist even if our software firm is closed down. They may serve another firm! Thus the lifetime of caterers is not governed by the lifetime of the software firm. This is typical ASSOCIATION
AGGREGATION
Consider a Car manufacturing unit. We can think of Car as a whole entity and Car Wheel as part of the Car. (at this point, it may look like composition..hold on) The wheel can be created weeks ahead of time, and it can sit in a warehouse before being placed on a car during assembly. In this example, the Wheel class's instance clearly lives independently of the Car class's instance. Thus, unlike composition, in aggregation, life cycles of the objects involved are not tightly coupled.

Friday, May 16, 2014

What is the difference between identity and equality in OOP?

basically,
  • identity -> a variable holds the SAME instance as another variable.
  • equality -> two (distinct) objects can be used interchangeably. they often have the same id.
for example
Integer a =  new Integer(1);
Integer b = new Integer(1);
a is equal but not identical to b.
Integer x = new Integer(1);
Integer y = x;
x is identical to y.
of course, two identical objects are always equal.
in java, equality is defined by the equals method. keep in mind, if you implement equals you must also implementhashCode.

Thursday, May 9, 2013

Compile time polymorphism

Hi Rajan,
Thats perfectly right. You need to *understand* the polymorphism concepts first.

Poly - many, morphism - faces.

The ability to have more than one version of the executable piece of code in the same name to invoke is called POLYMORPHISM.

view plaincopy to clipboardprint?
  1. Actually speaking the compiler would create a binding between the place of calling and the method definition.  


The ability to take a decision and thereby creating the binding is what gives you the name. 

The Compile time polymorphism comes in such a way that the compiler is able to differentiate the exact version of code to invoke during the compile time itself. Rather, the compiler *can take a decision of which exact method to invoke* during the compile time itself. 

Whereas in Runtime Polymorphism, the compiler is unable to take such a decision during compilation. Moreover, compiler-is-left-free-for-that-sake until runtime with which it can decide and invoke the appropriate version of the method.

Only when Inheritance is in picture, you can very well play around with the Polymorphism as you are given the rights to assign the child class objects to any of its parent/super class references. 

I hope this clears your doubt!

Saturday, May 4, 2013

What does it mean to “program to an interface”?


There are some wonderful answers on here to this questions that get into all sorts of great detail about interfaces and loosely coupling code, inversion of control and so on. There are some fairly heady discussions, so I'd like to take the opportunity to break things down a bit for understanding why an interface is useful.
When I first started getting exposed to interfaces, I too was confused about their relevance. I didn't understand why you needed them. If we're using a language like Java or C#, we already have inheritance and I viewed interfaces as a weaker form of inheritance and thought, "why bother?" In a sense I was right, you can think of interfaces as sort of a weak form of inheritance, but beyond that I finally understood their use as a language construct by thinking of them as means of classifying common traits or behaviors that were exhibited by potentially many non-related classes of objects.
For example -- say you have a SIM game and have the following classes:
 class HouseFly inherits Insect {
   void FlyAroundYourHead();
   void LandOnThings();
 }

 class Telemarketer inherits Person {
   void CallDuringDinner();
   void ContinueTalkingWhenYouSayNo();
 }
Clearly, these two objects have nothing in common in terms of direct inheritance. But, you could say they are both annoying.
Let's say our game needs to have some sort of random thing that annoys the game player when they eat dinner. This could be a HouseFly or a Telemarketer or both -- but how do you allow for both with a single function? And how do you ask each different type of object to "do their annoying thing" in the same way?
The key to realize is that both a Telemarketer and HouseFly share a common loosely interpreted behavior even though they are nothing alike in terms of modeling them. So, let's make an interface that both can implement:
 interface IPest {
    void BeAnnoying();
 }


 class HouseFly inherits Insect implements IPest {
   void FlyAroundYourHead();
   void LandOnThings();

   void BeAnnoying() {
     FlyAroundYourHead();
     LandOnThings();
   }
 }

 class Telemarketer inherits Person implements IPest {
   void CallDuringDinner();
   void ContinueTalkingWhenYouSayNo();

   void BeAnnoying() {
      CallDuringDinner();
      ContinueTalkingWhenYouSayNo();
   }
 }
We now have two classes that can each be annoying in their own way. And they do not need to derive from the same base class and share common inherent characteristics -- they simply need to satisfy the contract of IPest -- that contract is simple. You just have to BeAnnoying. In this regard, we can model the following:
 class DiningRoom {


   DiningRoom(Person[] diningPeople, IPest[] pests) { ... }

   void ServeDinner() {
     when diningPeople are eating,

       foreach pest in pests
         pest.BeAnnoying();
   }
 }
Here we have a dining room that accepts a number of diners and a number of pests -- note the use of the interface. This means that in our little world, a memeber of the pests array could actually be a Telemarketer object or a HouseFly object.
The ServeDinner method is called when dinner is served and our people in the dining room are supposed to eat. In our little game, that's when our pests do their work -- each pest is instructed to be annoying by way of the IPest interface. In this way, we can easily have both Telemarketers and HouseFlys be annoying in each of their own ways -- we care only that we have something in the DiningRoom object that is a pest, we don't really care what it is and they could have nothing in common with other.
This very contrived pseudo-code example (that dragged on a lot longer than I anticipated) is simply meant to illustrate the kind of thing that finally turned the light on for me in terms of when we might use an interface. I apologize in advance for the silliness of the example, but hope that it helps in your understanding. And, to be sure, the other posted answers you've received here really cover the gamut of the use of interfaces today in design patterns and development methodologies.