Sunday 31 January 2016

What is the difference between an Interface and an Abstract class?

There are quite a big difference between an interface and an abstract class, even though both look similar.
  • Interface definition begins with a keyword interface so it is of type interface
  • Abstract classes are declared with the abstract keyword so it is of type class
  • Interface has no implementation, but they have to be implemented.
  • Abstract class’s methods can have their own default implementations and they may be extended. The Abstract class’s methods could run independent of the inheriting class.
  • Interfaces can only have method declaration (implicitly public and abstract) and properties (implicitly public static)
  • Abstract class’s methods can’t have implementation only when declared abstract.
  • Interface can inherit more than one interfaces
  • Abstract class can implement more than one interfaces, but can inherit only one class
  • Abstract class must override all abstract method and may override virtual methods
  • Interface can be used when the implementation is changing
  • Abstract class can be used to provide some default behavior for a base class.
  • Interface makes implementation interchangeable
  • Interface increase security by hiding the implementation
  • Abstract class can be used when implementing framework
  • Abstract classes are an excellent way to create planned inheritance hierarchies and also to use as non-leaf classes in class hierarchies.
Abstract classes let you define some behaviors; they force your sub classes to provide others. For example, if you have an application framework, an abstract class can be used to provide the default implementation of the services and all mandatory modules such as event logging and message handling etc. This approach allows the developers to develop the application within the guided help provided by the framework.

However, in practice when you come across with some application-specific functionality that only your application can perform, such as startup and shutdown tasks etc. The abstract base class can declare virtual shutdown and startup methods. The base class knows that it needs those methods, but an abstract class lets your class admit that it doesn't know how to perform those actions; it only knows that it must initiate the actions. When it is time to start up, the abstract class can call the startup method. When the base class calls this method, it can execute the method defined by the child class.

Thursday 28 January 2016

What is an Interface?
In summary the Interface separates the implementation and defines the structure, and this concept is very useful in cases where you need the implementation to be interchangeable. Apart from that an interface is very useful when the implementation changes frequently. Some say you should define all classes in terms of interfaces, but I think recommendation seems a bit extreme.

Interface can be used to define a generic template and then one or more abstract classes to define partial implementations of the interface. Interfaces just specify the method declaration (implicitly public and abstract) and can contain properties (which are also implicitly public and abstract). Interface definition begins with the keyword interface. An interface like that of an abstract class cannot be instantiated.

If a class that implements an interface does not define all the methods of the interface, then it must be declared abstract and the method definitions must be provided by the subclass that extends the abstract class. In addition to this an interfaces can inherit other interfaces.

any one having the example in c++, java or any object oriented programming language please comment



Tuesday 19 January 2016

What is an Abstract class?

Abstract classes, which declared with the abstract keyword, cannot be instantiated. It can only be used as a super-class for other classes that extend the abstract class. Abstract class is the concept and implementation gets completed when it is being realized by a subclass. In addition to this a class can inherit only from one abstract class (but a class may implement many interfaces) and and must override all its methods/properties that are declared to be abstract and may override virtual methods/ properties. Abstract classes are ideal when implementing frameworks. 

In real world fruit is an abstract base class, no object of fruit class, having banana, apple as its derived classes are with objects, for more detail on this comment, for some example in c++/java, please wait and keep on reading....



Saturday 16 January 2016

 What is Abstraction?

Abstraction is an emphasis on the idea, qualities and properties rather than the particulars (a suppression of detail). The importance of abstraction is derived from its ability to hide irrelevant details and from the use of names to reference objects. Abstraction is essential in the construction of class. It places the emphasis on what an object is or does rather than how it is represented or how it works. Thus, it is the primary means of managing complexity in large programs.

Abstraction reduces complexity by hiding irrelevant detail. Now let us think of an example of abstraction. any idea. if any, please give it as comment for the next post from my side.........

Tuesday 12 January 2016

What is Encapsulation ?


The encapsulation is the inclusion-within a program object-of all the resources needed for the object to function, basically, the methods and the data. In OOP the encapsulation is mainly achieved by creating classes, the classes expose public methods and properties. A class is kind of a container or capsule or a cell, which encapsulate a set of methods, attribute and properties to provide its indented functionalities to other classes. In that sense, encapsulation also allows a class to change its internal implementation without hurting the overall functioning of the system. That idea of encapsulation is to hide how a class does its business, while allowing other classes to make requests of it.

Sunday 10 January 2016

How to identify and design a Class?

This is an art; each designer uses different techniques to identify classes. However according to Object Oriented Design Principles, there are five principles that you must follow when design a class,
  • SRP - The Single Responsibility Principle -
    A class should have one, and only one, reason to change.

  • OCP - The Open Closed Principle -
    Should be able to extend any classes' behaviors, without modifying the classes..

  • LSP - The Liskov Substitution Principle-
    Derived classes must be substitutable for their base classes.

  • DIP - The Dependency Inversion Principle-
    Depend on abstractions, not on concretions.

  • ISP - The Interface Segregation Principle-
    Make fine grained interfaces that are client specific.


Additionally to identify a class correctly, you need to identify the full list of leaf-level functions or operations of the system (granular level use cases of the system). Then you can proceed to group each function to form classes (classes will group same types of functions or operations). However a well-defined class must be a meaningful grouping of a set of functions and should support the reusability, while increasing expandability or maintainability, of the overall system.
In software world the concept of dividing and conquering is always recommended, if you start analyzing a full system at the start, you will find it harder to manage. So the better approach is to identify the module of the system first and then dig deep in to each module separately to seek out classes.
A software system may consist of many classes. When you have many classes, it needs to be managed. Think of a big organization, with its work force exceeding several thousand employees (let’s take one employee as one class). In order to manage such a work force, you need to have proper management policies in place. Same technique can be applied to manage classes of your software system. In order to manage the classes of a software system, and to reduce the complexity, system designers use several techniques, which can be grouped under four main concepts named
1. Encapsulation
2. Abstraction
3. Inheritance
4. Polymorphism.

These concepts are the four main gods of OOP world and in software term, they are called four main Object Oriented Programming (OOP) Concepts.

Friday 8 January 2016