Java Interview Questions - 2 - OOP

If you are preparing for Java Interview, get yourself a concept of OOP...

Java is Object Oriented Programming language. Well, to be true Java is not purely Object Oriented.

WHY SO ?
Cause Java uses primitives as data type. A pure Object Oriented technology will have the den full of Objects scattered everywhere in memory.
Nothing else matters...

Well, what is an Object ?
Objects are memory representation of your program written in any of Object Oriented language. Or in other ways we can define Object as abstraction of processing. Object is the basic unit of any OOP technology.
If you want to refresh your Object funda refreshed, you can take a look here.

So, can we define OOP as a concept ?
Yes, we can perfectly define OOP as a concept, which many software platform follows nowadays.

Name some of the OOP technology.
C++, C#, Java, Ruby, Python, PHP, Scala and many more. Full list is available here.

What is the difference between Java and JavaScript ?
Java is Object Oriented language while JavaScript is Object Based technology.

Object Based ?
This is a special type of language group which is more like Object Oriented Programming but lacks the Inheritance feature.

What is Inheritance ?
Inheritance is not a simple on liner concept to state. Take some time to read this and find out the best you can deliver in front of interviewer. Keep things simple short and pointed.

From here, the discussion can bifurcate. So take some time before you answer the question.

What are the way, you can achieve Inheritance in Java ?
We have two ways available to use the powerful concept of Inheritance,

Java does not support multiple inheritance. Is it true ?
Absolutely true.

Why so ?
If multiple inheritance was supported, we were more likely to face Deadly Diamond of Death.

We get Diamond while working on Java ?
We get Diamond of Death...
The whole discussion is here. Take some time to read and understand the situation. You will answer this question pretty well.

Give me a real world situation where all concept of OOP is there.
This one I got after having a discussion on OOP. Although I will share how I dealt with this one, I suggest you to find your most comfortable object to prove it as an object.

Well, my answer was on top of a Cigarette.
Cigarette follows A PIE
I will discuss this point by point.
Abstraction: Cigarette is made of Tobacco, that we know. But how the tobacco is filled inside, what other contents are mixed with it, we really don't care. We know we have to smoke, to do that we light a Cigarette.

So, Ideally we can think of this as following,

 /**  
  *   
  * @author palash kanti kundu  
  *   
  */  
 class Cigarette {  
      private Tobacco tobaccoContent;  
      private Other otherContent;  
      private Filter filter;  
      /**  
       * Light Cigarette with CigarLighter 
       */  
      public static void light(CigarLighter lighter) {  
           // Algorithm to light the cigarette  
      }  
      public void smoke() {  
           // Algorithm to smoke Cigarette  
      }  
 }  
We really now don't care of the private attributes and what Cigarette does with them. We just care for lighting it and smoking it.

Polymorphism: We all know what polymorphism means and how do you get that in your code. For Cigarettes, this was a bit tough to explain but I did it in an awkward way. Cause I did not find a better way to do it.
Polymorphism for both the classes and methods are shown below.

 /**  
  *   
  * @author palash kanti kundu  
  *   
  */  
 class Cigarette {  
      private Tobacco tobaccoContent;  
      private Other otherContent;  
      private Filter filter;  
      /**  
       * Light Cigarette with CigarLighter  
       */  
      public static void light(CigarLighter lighter) {  
           // Algorithm to light the cigarette  
      }  
      /**  
       * Smoke implementation  
       */  
      public void smoke() {  
           // Algorithm to smoke Cigarette  
      }  
      /**  
       * Default filter implementation  
       *   
       * @return  
       */  
      public Filter getFilter() {  
           return this.filter;  
      }  
 }  
 class Flake extends Cigarette {  
 }  
 /**  
  *   
  * @author palash kanti kundu  
  *   
  */  
 class GoldFlake extends Cigarette {  
      private TippedFilter tippedFilter;  
      /*  
       * (non-Javadoc)  
       *   
       * @see main.Cigarette#getFilter()  
       */  
      public Filter getFilter() {  
           super.getFilter();  
      }  
      /**  
       * Overloading performed  
       *   
       * @param size  
       * @return  
       */  
      public Filter getFilter(Size size) {  
           switch (size) {  
           case Size.Small:  
                return getFilter();  
           case Size.King:  
                return this.tippedFilter;  
           }  
      }  
 }  
 /**  
  * Test polymorphism on object  
  *   
  * @author palash kanti kundu  
  *   
  */  
 class CigaretteTest {  
      public static void main(String args[]) {  
           Cigarette cigarette = new Flake();  
           Cigarette cigarette2 = new GoldFlake();
           /*
            *Power of Inheritance. It promotes code reusability
            */
           cigarette.smoke();
           cigarette2.smoke();
      }  
 }  

Inheritance: The power of Inheritance is also shown in the earlier example. Flake does not even define any method. But as it is child of Cigarette, it is by default having the behavior of Cigarette.

Encapsulation: This part is more conceptual. So, we can say, that everything is encapsulated in the wrapper of Cigarette. Lots of things goes under the hood, which we really don't need to take care of. When we call a Cigarette, we are getting everything in a single wrapper, we just use the require to get the particular part out of it without much bothering about what other things are there in the wrapper.

So, Abstraction and Encapsulation deals with information hiding. What is the difference ?
Abstraction purely deals with the interface of your class. This solves the problem in design side. On the other hand, Encapsulation deals with the internal implementation. So, in a nutshell, we can tell that, Encapsulation is the implementation of Abstraction.

That's all for now. Keep watching this space for more...

Prev Next
Palash Kanti Kundu

No comments:

Post a Comment