Blogger templates

Your Ad Here

Video

Blogroll

Blogger news

Object Oriented Programming - Introduction

This tutorial can be used by a beginner irrespective of the knowledge in any programming language. Here I present only the basic idea about OOPS concept.
Classes, Objects, and Methods are the most basic concepts for object oriented programming. Besides, Inheritance, Abstraction, Polymorphism, Event and Encapsulation are the other concepts that OOPs features.


Object
An Object is a representation of some real-world thing (i.e person, place) or event. Objects can have both attributes and behaviors.

Class
A class is a blueprint or prototype from which objects are created. This section defines a class that models the state and behavior of a real-world object. It intentionally focuses on the basics, showing how even a simple class can cleanly model state and behavior.

Main OOPS Concepts are Encapsulation, Abstraction, Inheritance and Polymorphism.

1) Encapsulation: Wrapping up of data and methods in to a single unit is called as encapsulation.
     A car is can be used as a example for Encapsulation. Because it protected some unwanted parts or functions to user.

2) Abstraction: This will be explained with an example for easier understanding.
     Even without the hardware knowledge you can e-mail or do other jobs on the computer. Or a person diving a car should not necessary to know how the engine works.


3) Inheritance: Process of acquiring properties from one object to another without changes. It is the process by which one object acquires the properties of another object.
    For example. Son will aquire the behaviours from his father.

4) Polymorphism: Process of acquiring properties from one object to another with changes.
    poly=many
    morphism=forms

There are two types of polymorphisms.
1.Compile-time polymorphism: what object will be assigned to the present variable. This will be evaluated at compile time.
This is known as compile-time polymorphism.
2.Run-time polymorphism:what object will be assigned to the present variable. This will be evaluated at runtime depending
on condition. This is known as Run-time polymorphism.

How this be used in a program is explained below using a simple example.

String getTaste(Fruit fruit){
      //this method will return the taste of the fruit regarding the argument
}
 
We can dynamically(in runtime) get the "taste of fruit" as per the change in argument.

getTaste(apple);
getTaste(lemon);

Here the argument object changes, but the call is going into the same method.


 

Most Reading

Stats