Saturday 28 June 2014

Oops Concept in Java

1. Data Hiding :

Hiding of the data , so that outside class can not access that data.By using the private modifier we can implement the data hiding.
The main advantage of data hiding is we can achieve the security.

Example of data hiding:
public class Account
{
 private double balance=20000; //Can not available outside the class
}

2. Abstraction :

By abstraction we can hide internal implementation details and just highlight the set of services what are offering is known as the abstraction.
For example At ATM machine , bank people will highlight the set of services what they are offering without highlighting the internal implementation details this is simply known as the abstraction.
By using interface and abstract class we can achieve the abstraction.

3. Encapsulation :

Wrapping data and corresponding method into a single module is known as "Encapsulation". 
E.g. A capsule , it wrapped with different medicines.
Encapsulated class follows both Data Hiding and Abstraction.
The main advantage of the encapsulation are:
1. We can achieve security.
2. Improve modularity  of the application,

4. Inheritance :

It is also known as the IS-A relationship.The main advantage of the inheritance is re-usability.
In inheritance one class acquired the properties of the other class.Using inheritance , you can create general class that defines the set of related items.This class can be inherited by the other class.The class that is inherited is known as "super" class and the class that does inheriting known as the "subclass" .Subclass inherits all of the instance variables and methods defined by the super class and adds its own members.
By extends keyword we can implement the inheritance concept.

A simple example of inheritance :
class Parent
{

   public void test()
   {
 System.out.println("Parent class");
   }
}

class Child extends Parent
{
    public void test()    //Reuse the method of parent class
    {
 System.out.println("Child class"); 
    }
 
    public void test1()   //Additional method of child class
    {
 System.out.println("Additional method of child class");
    }
}


5.HAS-A-RELATIONSHIP :

Has-a-relationship also known as the "aggregation or composition".When a class depend on the other class known as the Has-a-relationship.The main advantage of the has-a-relationship is reuseability.
But the main dis-advantage of Has-a -relationship is it increases the dependency between the class. so it is the difficult to maintain the relationship between them.

Simple program of Has-a-relationship:
public class Address 
{
     int houseNo;
     String city;
     String state;
}
 
public class Person 
{
     String name;
     long mobNo;
     Address address;  //Has-a-relationship

}

Composition:

  • In the case of composition whenever the container object is destroyed all the contained objects will be destroyed automatically.
  • Without existing of container there is no chance of existing of contained object.
  • In composition there will be strong association .

Aggregation:

  • In the case of aggregation whenever the container object is destroyed there is no guarantee of destruction of contained object.
  • Without existing the container object there may be chance of existence of the contained object .
  • Container object just maintain the reference of the contained objects .
  • This type of  relationship also known as the "weak association" .   



6.  Polymorphism :

  • Poly means many and morphs means form , So polymorphism means many forms.
  • We can use same name to represent multiple forms in polymorphism . 
  • For example addition operation between  to integer gives the integer number while addition of two strings gives the concatenation . This is nothing but the polymorphic form of addition operation .
  • In java there are two types of the polymorphism .
  1. Compile time polymorphism.
  2. Run time polymorphism. 

0 comments :

Post a Comment