Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

Hot Searches
struts-config.xml  web.xml  Java date format  read/write text file using javascript  ArrayList  Insert Data into Database Using Hibernat  create text file using java  insert data to text file using java  Visual Basic Date and Time Display the t  Create table and insert data by sql quer 

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

OOPs Concept

                         

Introduction:  In this section, we will discuss the OOPs concepts along with  fundamentals  used to develop the java applications and programs.

OOP means Object Oriented Programming. This is a technique used to create programs around the real world entities. In OOPs programming model, programs are developed around objects and data rather than actions and logics. In OOPs, every real life object has properties and behavior. This feature is achieved in java through the class and object creation. They contains properties (variables of some type) and behavior (methods). OOPs provides better flexibility and compatibility for developing large applications.

Class: A class defines the properties and behavior (variables and methods) that is shared by all its objects. It is a blue print for the creation of  objects.

Object:  Object is the basic entity of object oriented programming language. Class itself does nothing but the real functionality is achieved through their objects. Object is an instance of the class. It takes the properties (variables) and uses the behavior (methods) defined in the class. 

Encapsulation, Inheritance and Polymorphism are main pillars of OOPs. These have been described below :

Encapsulation: Encapsulation is the process of binding together the methods and data variables as a  single entity. This keeps both the  data and functionality code safe  from the outside world. It hides the data within the class and makes it available only through  the methods. Java provides different accessibility scopes (public, protected, private ,default) to hide the data from outside. For example, we can create a class "Check" which has a variable "amount" to store the current amount. Now to manipulate  this variable we can create methods. For example to set the value of amount create setAmount() method and to get the value of amount create getAmount() method . Here is the code for "Check" class :

class Check{
  private int amount=0;

  public int getAmount(){
     return amount;
  }
  public void setAmount(int amt){
     amount=amt;
  } 
}

public class Mainclass{
  public static void main(String[] args){
     int amt=0;
     Check obj= new Check();
     obj.setAmount(200);
     amt=obj.getAmount(); 
     System.out.println("Your current amount is :"+amt);

     }
}

Here the data variable "amount" and methods setAmount() and  getAmount() are enclosed together with in a single entity called the "Check" class. These two methods are used to manipulate  this variable i.e. set and get the current value of amount.

Inheritance: Inheritance allows a class (subclass) to acquire the properties and behavior of another class (superclass). In java, a class can inherit only one class(superclass) at a time but a class can have any number of subclasses. It helps to reuse, customize and enhance the existing code. So it helps to write a code accurately and reduce the development time. Java uses extends keyword to extend a class.

class A{
  public void fun1(int x){
     System.out.println("int in A");
  }
  
}

class extends A{
  public void fun2(int x,int y){
     fun1(6);  // prints "int in A" 

     System.out.println("int in B");  
  }
}

public class C{
  public static void main(String[] args){
     B obj= new B();
     obj.fun2(2); 
     }
}

In the above example,  class B extends class A and so acquires properties and behavior of class A. So we can call method of A in class B.

Polymorphism : Polymorphism allows one interface to be used for a set of actions i.e. one name may refer to different functionality. Polymorphism allows a object to accept different requests of a client (it then properly interprets the request like choosing appropriate method) and responds  according to the current state of the runtime system, all without bothering the user. 

There are two types of polymorphism :

  1. Compile-time polymorphism
  2. Runtime Polymorphism

In compiletime Polymorphism, method to be invoked is determined at the compile time. Compile time polymorphism is supported through the method overloading concept in java. 

Method overloading means having multiple methods with same name but with different signature (number, type and order of parameters).

class A{
  public void fun1(int x){
    System.out.println("int");
  }
  public void fun1(int x,int y){
    System.out.println("int and int");
  }
}
public class B{
  public static void main(String[] args){
    A obj= A();
           
// Here compiler decides that fun1(int)
 is to be called and "int" will be printed.

    obj.fun1(2);   

// Here compiler decides that fun1(int,int)
 is to be called and "int and int" will be printed.
 
      obj.fun1(2,3);       
  }
}

In rumtime polymorphism, the method to be invoked is determined at the run time. The example of run time polymorphism is method overriding. When a subclass contains a method with the same name and signature as in the super class then it is called as method overriding.

class A{
  public void fun1(int x){
     System.out.println("int in A");
  }
  public void fun1(int x,int y){
     System.out.println("int and int");
  }
}

class extends A{
  public void fun1(int x){
     System.out.println("int in C");
  }
}

public class D{
  public static void main(String[] args){
     A obj;
     
     obj= 
new A(); // line 1
     obj.fun1(2) // line 2 (prints "int in A")
     
     obj=
new C() // line 3
     obj.fun1(2) // line 4 (prints "int in C")
  }
}

In the above program, obj has been declared as A type. In line 1, object of class A is assigned. Now in the next line, fun1(int) of class A will be called. In line 3, obj has been assigned the object of class C so fun1(int) of class C will be invoked in line 4. Now we can understand that same name of the method invokes different functions, defined in different classes, according to the current type of variable "obj". This binding of  method code to the method call is decided at run time.

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

5 comments so far (
post your own) View All Comments Latest 10 Comments:

please tell me the concept of astraction,Arrays&Strings,ExceptionHandling,UTIL packages with programs

Posted by rajitha on Saturday, 02.9.08 @ 21:22pm | #47715

hi
Thank u very much, these concepts are very useful for me. Keep it up.......!

Posted by neelima on Friday, 01.25.08 @ 15:28pm | #46123

Hi,

It's a good place to learn new things.

Keep it up.....I appreciate your efforts.

Rgds

Posted by Apps on Sunday, 12.9.07 @ 15:20pm | #41652

hey keep it up! I like the method of teaching.

Posted by Rakesh on Sunday, 08.12.07 @ 12:23pm | #23216

These concept are very useful for me.In the interview of point.Thanks...........

Posted by Anjaneyulu on Tuesday, 05.29.07 @ 15:33pm | #17639

Tell A Friend
Your Friend Name

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.