Thursday, September 25, 2014

Interface Vs Abstract class

Either preparing an exam or an interview this is a must know. Be aware and don't be the next victim. As stated in wikipedia. Starting with Java 8 default and static methods may have implementation in the interface definition. Interfaces cannot be instantiated, but rather are implemented. A class that implements an interface must implement all of the methods described in the interface, or be an abstract class.
           However an Abstract class is a class that represents a generalization and provides functionality but is only intended to be extended and instantiated. Without going too far into unecessary theories, I would like to share with you this  comprehensive comparison that I found on DURGA SOFT
INTERFACE ABSTRACT CLASS
If we have the requirement specification and we don't know anything about the implementation In case of partial implementation, here we are talking about the implementation but not completely
Inside interface every method is always public and abstract whether we are declaring or not. Hence interface is also considered as 100% pure abstract class Every methods present in abstract class need not to be public or abstract. In addition to abstract methods we can take concrete methods also.
We cannot declare interface methods with the following modifiers: private, protected, final, static, synchronized, native, strictfp There are no restriction on abstract class method modifiers
Variables are always public, static and final whether we are declaring or not. Variables need not to be public, final and static
Variables cannot be declared with the following modifiers: private, protected, volatile, transient No restrictions on abstract class variables modifiers
Variables must be initialized at declaration otherwise compile time error Variables initialization not required at declaration time
Not possible to declare instance and static blocks otherwise compile time error Instance and static blocks allowed
No constructors allowed Constructor declaration allowed and to be executed during child objects creation

About Polymorphism

One of the key aspect of OOPs is ineluctably  the Polymorphism. What is it ? How is it implemented ?
the phenomenon where the same message sent to two different objects produces two different set of actions. Polymorphism is generally divided into two parts:
  • Static polymorphism : .An entity existing in different physical forms simultaneously. Here the response to message is decided on compile-time. E.g: Overloading (methods, operator). Method overloading  creates a new method with the same name and different signature. It uses early binding.
  • Dynamic polymorphism : An entity changing its form depending on the circumstances. Here  the response to message is decided on run-time. E.g: Overriding (methods). Method overriding  is the process of giving a new definition for an existing method in its derived class. A class that declares or inherits a virtual function is called a polymorphic class.
This post is intended to be a discussion topic, feel free to drop your comment...

Classes defined with struct and union in C++

When it comes to C++ everything is pushed beyond limits, and so it the case of a class defined with struct or union keyword. But the point I would like to share with you on this post is the key difference involved.
            The concepts of class and data structure are so similar that both keywords (struct and class) can be used in C++ to declare classes (i.e. structs can also have function members in C++, not only data members). The only difference between both is that members of classes declared with the keyword struct have public access by default, while members of classes declared with the keyword class have private access. For all other purposes both keywords are equivalent.
    In a similar fashion, the concept of unions is different from that of classes declared with struct and class, since unions only store one data member at a time, but nevertheless they are also classes and can thus also hold function members. The default access in union classes is public.
Credit : Juan Soulie

Wednesday, September 24, 2014

Difference between an array and dymanic memory pointer in C++

It is known to all that the least thing any programmer would like to deal with is dealing with Pointers. May Java be blessed !
Suppose I have two declarations:

1)  int array[10] //an array of 10 integer elements

2) int  *pArray ;
    pArray = new int [10];

The major difference is that for the normal array (1) , the size of the array is constant and has to be decided before the execution. However the dynamic memory allocation (2) give you more flexibity by allowing you to assign memory during the execution of the program (runtime) using any variable or constant value as its size.

 How to access elements from our declaration:
  array[2] // accessing the third element
  pArray[2] or  *(pArray+2)  // accessing the third element ....