Tuesday, 29 April 2014

OOPs CONCEPT IN A EASY WAY

OOPS CONCEPT in an easy way

OOPS Concept with Real Life Example
This is the most asked Question in a technical interview in any IT domains. OOPs Concept is very important. OOPs concept with real Life Example that will help you to understand OOPS concept.
Objects: Object is the basic unit of object-oriented programming. Objects are identified by its unique name. An object represents a particular instance of a class. There can be more than one instance of an object. Each instance of an object can hold its own relevant data.
An Object is a collection of data members and associated member functions also known as methods.
Classes: Classes are data types based on which objects are created. Objects with similar properties and methods are grouped together to form a Class. Thus a Class represent a set of individual objects. Characteristics of an object are represented in a class as Properties. The actions that can be performed by objects becomes functions of the class and is referred to as Methods.
Example #1:
For example consider we have a Class of Cars under which Santro Xing, Alto and WaganR represents individual Objects. In this context each Car Object will have its own, Model, Year of Manufacture, Color, Top Speed, Engine Power etc. which form Properties of the Car class and the associated actions i.e., object functions like Start, Move, and Stop form the Methods of Car Class.No memory is allocated when a class is created. Memory is
allocated only when an object is created, i.e., when an instance of a class is created.

Example #2:
An architect will have the blueprints for a house....those blueprints will be plans that explain exactly what properties the house will have and how they are all layout.  However it is just the blueprint, you can't live in it.  Builders will look at the blueprints and use those blueprints to make a physical house.  They can use the same blueprint to make as many houses as they want....each house will have the same layout and properties.  Each house can accommodate its own families...so one house might have the Smiths live in it, one house might have the Jones live in it.

The blueprint is the class...the house is the object.  The people living in the house are data stored in the object's properties.

Abstraction: Abstraction means showing essential features and hiding non-essential features to the user.

For Eg.  Yahoo Mail...

When you provide the user name and password and click on submit button. It will show Compose,Inbox,Outbox,Sentmails. so and so when you click on compose it will open...but user doesn't
know what are the actions performed internally....It just Opens....that is essential; User doesn't know internal actions ...that is non-essential things...

For Eg. TV Remote.
Remote is an interface between user and TVright. Which has buttons like 0 to 10, on /offetc. but we don’t know circuits inside remote...User does not need to know. Just he is using essential thing that is remote.
 
Encapsulation: Encapsulation means which binds the data and code (or) writing operations and methods in single unit (class).
For Example:
A car is having multiple parts. Like steering,wheels, engine. Etc.which binds together to form a single object that is car. So, here multiple parts of cars encapsulates itself together to form a single object that is Car.

In real time we are using Encapsulation for security purpose...
Encapsulation = Abstraction + Data Hiding.
Inheritance: Deriving a new class from the existing class, is called Inheritance.
Derived (sub class) class is getting all the features from Existing (super class\base class) class and also incorporating some new features to the sub class.
For Eg.,
class Address
{
String name;
String H.no;
String Street name;
}
class Latest Address extends Address
{
String City;
String State;
String Country;
}
public class Vishal
{
{
Latest Address la = new Latest Address();
//Assign variable accordingly...
}
}

In the above Example class Latest Address getting all features from the Address class.
In the Latest Address class we have total 6 properties..3 are inherited from Address class and 3 properties are
incorporated. So In the class Vishal we are declaring the object of class Latest Address and then assign new variables using the properties of the previous base classes... So this is a nice example of inheritance.
Polymorphism:
Polymorphism means ability to take more than one form that an operation can exhibit different behavior at different instance depend upon the data passed in the operation.

1>we behave differently in front of elders, and friends. A single person is behaving differently at different time.

2> A software engineer can perform different task at different instance of time depending on the task assigned  to him .He can done coding , testing , analysis and designing depending on the task assign and the requirement.

3> Consider the stadium of common wealth games. Single stadium but it perform multiple task like swimming, lawn tennis etc.

4> If a girl is married and mother of 2 children doing teaching job then she is a women first, teacher in a school when she is in school, wife of someone at home, mother of her children,, and obvious daughter of someone & may be girl friend of someone (just kidding) means a woman plays different roles at different times dates the polymorphism (many forms).
Summary:
OOPs have following features:
1. Object             - Instance of Class
2. Class               - Blue print of Object
3. Encapsulation    - Protecting our Data
4. Polymorphism   - Different behaviors at different instances
5. Abstraction        - Hiding our irrelevant Data
6. Inheritance        - One property of object is acquiring to another property of object


More Practical Example


Preface
 These tutorials will provide you a beginner's perspective of Java programming. The first few tutorials would be irrespective of programming language, but at the latter half, I would be stressing more on Web application development in Java. I am preparing these tutorials on the basis of my own experience of learning at TCS. Your suggestions and queries would help me build on this tutorial

Intro
 OOP - A term that should flow in your blood if you have to be a good programmer. It is an essential perspective, visualization and an art to understand the concept as a whole. OOP concepts redefine our thoughts (in programming paradigm) and the way we code. The concepts are very easy in theory, but a bit difficult to grasp in real world scenarios. And once you master this perspective, coding in any language would be a piece of cake. So lets get to the basics first(Mind you, the theory is really simple. Its gets confusing when you try to implement these concepts)

OOP
 I guess its high time that I mention what OOP is all about. OOP - Object Oriented Programming, is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. - Wikipedia
 Got any idea? Bet you haven't. Let me put it into simple words. Object Oriented Programming is
·                     a way to code, 
·                     its an approach towards coding, 
·                     its an approach to problem solving,
·                     is a real life simulation of programming methodology
Ring any bells? Well, you will be the end of this tutorial (Or atleast the next!)

Now that I have said OOP is an approach to programming, there are a few core concepts that make up this methodology. I am not listing them down at one go, instead I'll be explaining it one by one.
Please note that these concepts are applicable for most of the programming languages. This tutorial explains these concepts with respect to real life scenario.



Polymorphism
 Don't get strangled by the name, its a simple concept. It's a concept which we can interpret a function in more than one way. To give an example, suppose a function to calculate the Area of a geometric figure(Circle, Square etc.). Ideally, one would create separate functions with different names for the figures. Using the concept of Polymorphism, you can have the same name for the function, yet do different functions with it. You will get used to this concept at a later point of time, so I'm not going into its depth now.

Messaging
  Messaging is the core concept of communication among classes. To give an idea of it, let me put up a scenario. Suppose we have two classes, Driver and Car. The Driver has to communicate with the Car to do functions like accelerate, brake etc. So how is this interaction done? Yes, Car has a function accelerate, but some message should be sent from Driver to Car right? That's exactly what messaging is all about. Once you get into this, you would be doing messaging every now and then.

Conclusion
  Finally! Make sure that you are used to all these concepts. In the upcoming tutorials, you would be seeing these concepts implemented in a real life scenario. Before ending up, let me note down the points to remember.
·                     OOP Concepts
·                     Classes and Objects
·                     Data abstraction
·                     Data encapsulation
·                     Inheritance
·                     Polymorphism
·                     Messaging


Monday, 28 April 2014

What is programming?


Computer programming (often shortened to programming) is a process that leads from an original formulation of a computing problem to executable programs. It involves activities such as analysis, understanding, and generically solving such problems resulting in an algorithm, verification of requirements of the algorithm including its correctness and its resource consumption, implementation (commonly referred to as coding) of the algorithm in a target programming language, testing, debugging, and maintaining the source code, implementation of the build system and management of derived artefacts such as machine code of computer programs. The algorithm is often only represented in human-parsable form and reasoned about using logic. Source code is written in one or more programming languages (such as C, C++, C#, Java, Python, Smalltalk, JavaScript, etc.). The purpose of programming is to find a sequence of instructions that will automate performing a specific task or solve a given problem. The process of programming thus often requires expertise in many different subjects, including knowledge of the application domain, specialized algorithms and formal logic.