Saturday, October 2, 2010

Fundamentals of Object Oriented Programming in Java

I remember when I was doing my engineering , OOPS concept was in first semester. Back then it looks like some alien things to me, because I have never done programming and even the smallest program we get to write e.g. checking whether number is palindrome or not , or printing Fibonacci series , we never really needed to use OOPS. When needed, I just memorize things and written down in exam :). That worked, I passed the exam but effectively I didn't learn OOPS or Object Oriented Programming on those days.

It's when, I started learning java (an almost truly object oriented programming language) and introduced to concept like abstract classes, interface, and method overriding; some thing started to going in my mind. This blog post is very basic in nature, but I really hope it could help beginners on understanding things like class, objects, abstraction , polymorphism, inheritance ,encapsulation , composition , cohesion , coupling , interface programming etc.


What is object oriented programming in JavaI am planning to write clear and concise post on each of these, because I really don't like too much information in one shot. It never worked with me , I believe learning is gradual process and always start with most simple things, which eventually turned out to be most important one.

So What is Object Oriented programming ?
Simple programming which is oriented on objects :) means treat everything surrounding you as an
object.

Now question comes, what is Object?
An Object is a particular concrete instance of Class, and what is class? A Class is a blueprint from
which object gets created which has attributes and behavior , we call attribute as members and behavior as methods.

lets see one example : how about representing a classroom using OOPS?
A classroom is a room , which has certain no of chairs , desks , blackboard , teacher , students etc.

Now in order to represent that in code, we need to write a program. In Java we will write like this.

public class ClassRoom {

//members
int noOfChairs;
int noOfDesks;
Teacher currentTeacher;
Students [] students;
Blackboard blackboard;


//methods
public int getNoOfChairs(){

}

public Teacher getNoOfTeacher(){

}

.....

}

I have omitted most of the methods, just to focus on concept rather than implementation. So now we know that class is blueprint, which has member variable and methods and used to create object. Now what is object here, any classroom in which any lecture is going on , your first lecture would be one instance, second lecture would be another instance.

See the point, every object of same class has same no of variables but there value gets changed e.g. lecture 1 was attended by 15 students by teacher called Mr. Gupta , lecture 2 was attended by 18 students which was by Miss Amrita.

Any thing which you see around can be represented in class and object e.g. this blog is an instance of Blog class, a particular movie can be instance of Movie class e.g. Titanic is an instance of Movie where actor/director are different than other movie.

That's all on fundamentals of object oriented programming in Java. I hope you got the idea of representing real world things on class and objects, by doing this we try to create applications which solves real world problems. Visualizing and understanding in terms of object also makes programming easy to write, read and maintain.

Related Java and OOPS programming tutorials.
Difference between static and dynamic binding
Difference between overloading and overriding 
How to run Java HelloWorld program from command line
How to find middle element of linked list in one pass
How to swap two numbers without using third variable

2 comments :

Anonymous said...

Explaining in easy and good way OOPS. Great Sir....

chetan pandey said...

The most simpler way of explaining OOP's concept I have ever seen

Post a Comment