Monday, 24 March 2014

JAVA OOPS CONCEPT

Posted By: Unknown - Monday, March 24, 2014


Welcome to Java OOPS Concept 
OOPS means object Oriented Programming skills. Everything is around object. Object holds data and data is crucial. Free access of data is not available and it is restricted. OOPS is core to Java. Every java program have some oops concept in it.
OOPS have two basic advantages
1. Code reusability
2. Maintenance of code
OOPS have following three basic features
1. Encapsulation
2. Inheritance
3. Polymorphism
4. Abstraction
1. Encapsulation : Encapsulation means putting your code and data inside a capsule. It is locked and can be opened by only object. Object is an entity and other code can access only if this capsule ( class ) provides permission. One class can be changed and if its exposed method did not change then calling class do not require any change.
Simple eample of encapsulation and its advantage given below :
public class EncapsulationExample
{

   private String name;
   private int age;
   private String address;

   public String getName(){
      return name;
   }

   public int getAge()
   {
      return age;
   }

   public String getAddress()
   {
      return address;
   }

   public void setName(String name1)
   {
      name = name1;
   }

   public void setAge( int age1)
   {
      age = age1;
   }

   public void setAddress( String address1)
   {
      address = address1;
   }

   public static void main(String args[])
   {
      EncapsulationExample enEx = new EncapsulationExample();
      enEx.setName("Shaffin");
      enEx.setAge(5);
      enEx.setAddress("Java World");

      System.out.print("Name " + enEx.getName()+ " Age : "+ enEx.getAge() + " Address : "+ enEx.getAddress());
    }

}
Output of the program :
D:\>java EncapsulationExample
Name Shaffin Age : 5 Address : Java World
This is an example of getter and setter. No one is allowed to access data directly since it is declared private. Variables can be accessed with getter methods and can be set by setter methods.
Advantage of Encapsulation :
I). Complete control on properties and method
II). Other users need to know internal of all methods. Other code can know only exposed methods.
2. Inheritence : Inheritence means inheriting property. Inhertience provides feature for a object to inherit from other object. It is an excellent way of code reuseability. Java uses extends keyword for inheritence.
A practical life example of inheritence is Animal-Dog and Animal-Cat. Some bheavious and properties are same in all animals. These behaviour can be formed in parent class. Dog and Cat related behaviour like sounds, size …etc can be added in Dog and Cat.
class Animal
{
//common stuff
}
class Dog extends Animal
{
// sounds …
}
class Cat extends Animal
{
//sounds and size …etc
}
Java supports three types of inheritences.
I). Single level inheritence
II). Multi Level Inhertience
III). Multiple Inheritence
I) Single Level Inheritence :
One class extends another class called single level inheritence
class Parent
{
 int parentVariable = 10;
 public void parentMethod()
 {
  System.out.println("parent method");
 }
}

class Child extends Parent
{
 int childVariable = 20;
 public void childMethod()
 {
  System.out.println("child method");
 }
 public static void main(String args[])
 {
  Child obj = new Child();

  obj.parentMethod();
  System.out.println(obj.parentVariable);

  obj.childMethod();
  System.out.println(obj.childVariable);
 }
}
Output of the example :
D:\>java Child
parent method
10
child method
20
II) Multi Level Inheritence :
One class extends other class and extended class extended by another class.
Example is given below :
class Parent
{
 int parentVariable = 10;
 public void parentMethod()
 {
  System.out.println("parent method");
 }
}

class Child extends Parent
{
 int childVariable = 20;
 public void childMethod()
 {
  System.out.println("child method");
 }
}

class GrandChild extends Child
{
 int grandChildVariable = 20;
 public void grandChildMethod()
 {
  System.out.println("grand child method");
 }
 public static void main(String args[])
 {
  GrandChild obj = new GrandChild();

  obj.parentMethod();
  System.out.println(obj.parentVariable);

  obj.childMethod();
  System.out.println(obj.childVariable);

  obj.grandChildMethod();
  System.out.println(obj.grandChildVariable);
 }
}
Output of the program :
D:\>java GrandChild
parent method
10
child method
20
grand child method
20
III). Multiple Inheritence : It is not allwed due to secuirty.
3. Polymorphism :
Polymorphism means many shapes or different forms. Same name of method and construct is overloaded by different arguments. Java have two types of polymorphism.
I) Compile time (Static) Polymorphism
II) Runtime (dynamic) Polymorphism
I) Compile time (Static) Polymorphism : Method and constructor overloading is good examples of static polymorphism. Method name and construct name is overloaded with different arguments.
Eample of Method Overloading :
class MethodOverloading
{
 public void method()
 {
  System.out.println("method without arguments");
 }
 public void method(String name)
 {
  System.out.println("method with one arguments");
 }
 public void method(int i, int j)
 {
  System.out.println("method with 2 arguments");
 }
 public static void main(String args[])
 {
  MethodOverloading obj = new MethodOverloading();
  obj.method(); 
  obj.method("Shaffin"); 
  obj.method(11,12);
 }
}
Output of method overloading program:
D:\>java MethodOverloading
method without arguments
method with one arguments
method with 2 arguments
Eample of Constructor overloading :
class ConstructorOverloading
{
 String name;
 int age;

 public ConstructorOverloading()
 {
  System.out.println("Empty Constructor");
 }
 public ConstructorOverloading(String name1)
 {
  name = name1;
 }
 public ConstructorOverloading(String name1, int age1)
 {
  name = name1;
  age = age1;
 }
 public static void main(String args[])
 {
  ConstructorOverloading obj = new ConstructorOverloading();
  System.out.println("Name "+obj.name+"  age "+obj.age); 

  ConstructorOverloading obj1 = new ConstructorOverloading("Shaffin");
  System.out.println("Name "+obj1.name+"  age "+obj1.age); 

  ConstructorOverloading obj2 = new ConstructorOverloading("Shaffin", 5);
  System.out.println("Name "+obj2.name+"  age "+obj2.age); 
 }
}
Output of constuctor overloading :
D:\>java ConstructorOverloading
Empty Constructor
Name null  age 0
Name Shaffin  age 0

Name Shaffin  age 5
4. Abstraction :
Please refer following links for details in abstraction in java abstract classes in 

0 comments:

Post a Comment

this blog is helpful or not

International

Auto News

Translate

Pages

Popular Posts

Popular Posts

Designed By Templatezy / Sb Game Hacker Apk / MyBloggerThemes