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. 

Tuesday, 24 June 2014

Difference Between Array and Collection

Difference Between Array and Collection



Array

Collection
 1. Size of arrays are fixed.  1. Size of collection are growable in nature we can increase or decrease the size as our requirement.
2. Memory point of view array concept not recommended to use.2. Memory point of view collection concept highly recommended to use.
3. Arrays can hold only homogeneous elements. 3. Collection can hold both homogeneous and heterogeneous objects.
4. Arrays can hold both primitives and objects type elements. 4. Collection can hold only objects.


Sunday, 22 June 2014

Difference Between Method Overloading and Overriding

Difference Between Method Overloading and Overriding


Method Overloading

Method Overriding
1. Method name must be same. 1. Method name must be same.
2. Method argument must be different. 2. Method arguments must be same.
3. Method signature must be different. 3. Method signature must be same.
4. Return type can be changed. 4. Return type must be same until 1.4v but after 1.5v co-variant type allowed.
5. Private , static and final method can be overloaded. 5. Private , static and final method can not be override.
6. Any type of access modifier is allowed(public ,  protected , private). 6. We can not decrease the scope modifier.
e.g. public method can not override as  protected.
7. There is no restriction in exception handling. 7. Child class should be handle exception same as the parent class or the child exception.

e.g. P: Exception
C: Exception or IOException
8. Method resolution takes by the compiler. 8. Method resolution takes by the JVM based on the object reference.
9. Method overloading also known as static polymorphism, compile-time polymorphism or early binding 9. Method overriding also known as dynamic polymorphism, run-time polymorphism or late binding




Saturday, 21 June 2014

Difference Between Abstract class and Interface

Difference Between Abstract class and Interface.

Abstract Class Interface
1. If we are talking about implementation but not completely (partially implementation) then go for abstract class. 1. If we does know anything about the implementation then we should go for the interface.
2. Every method present in abstract class need not be abstract ,it can be not abstract also. 2. Every method present inside interface is by default abstract.
3. Every method present in abstract class need not be public. 3. Every method present inside interface is by default public.
4. We can use any modifier for the method. 4. The following modifiers are not allowed for the interface methods:
Strictfp , protected , final , native , static , private , synchronized .
5. No restriction for abstract class variables. 5. Every attribute present inside the interface is by default public , static and final.
6. No restriction for the abstract class variables. 6. Following modifiers cannot declares for the interface variables
Private , protected , transient , volatile .
7. No restriction for the initialization of abstract class variables. 7. Compulsory initialize the variables whenever declaration.
8. Inside abstract class we can take instance and static block. 8. Inside interface we cannot take instance and static block.
9. Inside abstract class we can create constructor. 9. Inside interface we can not create constructor.


Difference Between C++ and Java

Difference Between C++ and Java

C++ Java
1. C++ is an extension of C with Object oriented behavior , C++ is not complete object oriented language as Java. 1. Java is complete object oriented language.
2. C++ supports pointer concept. 2. Java does not support pointer.
3. At compilation stage C++ source code converted into machine level code. 3. At compilation stage Java source code converted into byte code.
4. C++ is platform dependent , you have to compile and run program for different platform. 4. Java is platform independent language so you can compile once and run any where.
5. C++ uses only compiler. 5. Java uses both compiler and interpreter.
6. C++ supports operator overloading. 6. Java does not supports operator overloading.
7. C++ supports multiple inheritance. 7. Java does not supports multiple inheritance.
8. Goto statements available in C++. 8. There no goto statements concept in Java.
9. C++ supports :: scope resolution operator. 9. No :: scope resolution operator.
10. C++ does not have garbage collector concept. 10. Garbage collector is most useful feature of Java
11. C++ does not support final keyword. 11. Java supports final keyword.
12. C++ supports constant keyword. 12. Java does not supports constant keyword.
13. C++ uses #include to include different files. 13. Java uses import keyword to include different classes.




Difference Betwee C and C++

Difference Between C and C++


C

C++
1. C is Procedural Language. 1. C++ is non Procedural i.e Object oriented Language.
2. In C, Polymorphism is not possible. 2. The concept of polymorphism is used in C++.Polymorphism is the most Important Feature of OOPS.
3. Inheritance is not possible in C. 3. Inheritance is possible in C++.
4. Virtual Functions are not present in C. 4. The concept of virtual Functions are used in C++.
5. Operator overloading is not possible in C. 5. Operator overloading is one of the greatest Feature of C++.
6. Top-down approach is used in Program Design. 6. Bottom-up approach adopted in Program Design.
7. Multiple Declaration of global variables are allowed. 7. Multiple Declaration of global varioables are not allowed.
8. scanf() Function used for Input.
printf() Function used for output.
8. Cin>> Function used for Input.
Cout<< Function used for output.
9. Namespace Feature is not present in C. 9. Namespace Feature is present in C++ for avoiding Name collision.
10. In C, we can call main() Function through other Functions 10. In C++, we cannot call main() Function through other functions.
11. C requires all the variables to be defined at the starting of a scope. 11. C++ allows the declaration of variable anywhere in the scope i.e at time of its First use.
12. In C, malloc() and calloc() Functions are used for Memory Allocation and free() function for memory Deallocating. 12. In C++, new and delete operators are used for Memory Allocating and Deallocating.
13. C supports built-in and primitive data types. 13. C++ support both built-in and user define data types.
14. Mapping between Data and Function is difficult and complicated. 14. Mapping between Data and Function can be used using Objects
15. In C, Exception Handling is not present. 15. In C++, Exception Handling feature us available.






Wednesday, 18 June 2014

Java Classes And Objects

In object-oriented programming technique, we design a program using objects and classes.
Object is the physical as well as logical entity whereas class is the logical entity only.

Object in Java:

Object is the real world entity that has state and behavior.
e.g. laptop, bike, dog , pen, table, car etc. It can be physical or logical.

 Car is an object. Its name is Honda City, color is Blue etc. known as its state. 
Object is an instance of a class. Class is a template or blueprint from which objects are created. So object is the instance(result) of a class.

Introduction to Java Classes:

A class is nothing but a blueprint or a template for creating different objects which defines its properties and behaviors.
A class can contain fields and methods to describe the behavior of an object.
Methods are nothing but members of a class that provide a service for an object or perform some business logic. 
Java fields and member functions names are case sensitive.
Current states of a class’s corresponding object are stored in the object’s instance variables.
Methods define the operations that can be performed in java programming.

A class has the following general syntax:

class ClassName
{
         //class body
}

Example of class

public class Rectangle
{
     int length;
     int breadth;
     int area;

     public int getArea()
     {
       area=length*breadth;
       return area;
     }
}

A class in java can contain:

data member
method
constructor
block
class and interface

How do you reference a data member/method ?
This is done by stating the name of the object reference, followed by a period (dot), followed by the name of the member inside the object.

For Example:
RectangleObject.length=20;
RectangleObject.breadth=10;
RectangleObject.getArea();

Class Variables(Static Variables)
We use class variables also know as Static variables when we want to share characteristics across all objects within a class. 
When you declare a variable as static only single copy is created among all the objects.
Hence when one object changes the value of a class variable, it affects all objects of the class. 
We can access a class variable by using the name of the class.
Static variables can be accessed even though no objects of that class exist. It is declared using static keyword.

Class Methods(Static Methods)
Class methods, similar to Class variables can be invoked without having an instance of the class.
You cannot call non-static methods from inside a static method.

Instance Variables
Each Object would have its own copy of the variable. 
When you change the value of the instance variables , it does not affects the other objects.

Top level class modifier
The modifier applicable to top level class are:
1. public
2. <default>
3. abstract
4. final

How to create Object of a class ?
By new key word we can create object of a class.New keyword occupy memory for the objects.
public class Area
{
   int length;
   int breadth;
   int area;
   public int area()
   {
      area=length*breadth;
      return area;
   }
   public static void main(String[] args)
   {
        Area a=new Area(); //object creation
        a.length=20;
        a.breadth=10;
        System.out.println("Area = " + a.area());
   }
}
OUTPUT
Area = 200



Friday, 30 May 2014

for Loop in C

There may be a situation, when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.
Programming languages provide various control structures that allow for more complicated execution paths.
A For loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a For loop statement :

Synatax:

for(initial expression; test expression; update expression) 
{ 
body of loop; 
}

Lets take an Simple Example:
#include<stdio.h>
int main()
{
        int i;
        for(i=0;i<=5;i++)
              printf("Hello World");
         return 0;
}

Output:
it will print five times Hello World
Explanation of For Loop :

1.Firstly the for loop executes the initialize statement in which the subscript variable will be initialized
with the initial value.

2.After the initialize statement the condition part of the loop will be executed if the condition becomes true then body of the loop will be executed otherwise the loop will be terminated

3.If the loop condition becomes true then body of the loop will be executed. After the execution of the for loop body the control goes to the third part of the loop statement i.e Expression Updation

4.After updating subscript variable control again goes to execute condition statement.



for more understanding the flow of loop plz go here....


Different ways of using For loop :

In order to do certain actions multiple times , we use loop control statements. For loop can be implemented in different verities of using for loop -

1. Single Statement inside For Loop
2. Multiple Statements inside For Loop
3.  No Statement inside For Loop
4.  Semicolon at the end of For Loop
5.  Multiple Initialization Statement  inside For Loop
6.  Missing Initialization in For Loop
7.  Missing Increment /Decrement  Statement
8.  Infinite For Loop
9.  Condition with no Conditional Operator.

Way 1 : Single Statement inside For Loop

for(i=0;i<5;i++)
     printf("Hello");
1. Above code snippet will print Hello word 5 times.
2. We have single statement inside for loop body.
3. No need to wrap printf inside opening and closing curly block.
4. Curly Block is Optional.

Way 2 : Multiple Statements inside For Loop
for(i=0;i<5;i++) 
{ 
        printf("Statement 1"); 
        printf("Statement 2"); 
        printf("Statement 3"); 
        if(condition)     
        {     
              --------     
              --------     
          } 
  }
If we have block of code that is to be executed multiple times then we can use curly braces to wrap multiple statement in for loop.

Way 3 : No Statement inside For Loop
for(i=0;i<5;i++) 
{   }
1.This is bodyless for loop. It is used to increment value of “i”.
2.This verity of for loop is not used generally.
3.At the end of above for loop value of i will be 5


Way 4 : Semicolon at the end of For Loop

for(i=0;i<5;i++);
1. Generally beginners thought that , we will get compile error if we write semicolon at the end of for loop.
2. This is perfectly legal statement in C Programming.
 3. This statement is similar to bodyless for loop. (Way 3)

Way 5 : Multiple Initialization Statement inside For
for(i=0,j=0;i<5;i++) 
{ 
       statement1; 
       statement2;  
       statement3; 

}
Multiple initialization statements must be seperated by Comma in for loop.


Way 6 : Missing Increment/Decrement Statement
for(i=0;i<5;) 
{ 
        statement1; 
        statement2; 
        statement3; 
         i++; 
}
however we have to explicitly alter the value i in the loop body.

Way 7 : Missing Initialization in For Loop
i = 0;
for(;i<5;i++) 
{ 
       statement1; 
       statement2;
       statement3;
 }
we have to set value of ‘i’ before entering in the loop otherwise it will take garbage value of ‘i’.

Way 8 : Infinite For Loop
i = 0;
for(;;) 
{ 
       statement1; 
       statement2; 
       statement3; 
       if(breaking condition)    
              break; 
        i++; 
}
Infinite for loop must have breaking condition in order to break for loop. otherwise it will cause overflow of stack.


Understanding of For loop

#include<stdio.h>
int main()
{
        int i;
        for(i=0;i<=5;i++)
              printf("Hello World");
         return 0;
}
Lets start with step by step and what will happen when condition is true:
Seq.No        Statement Flow                Explanation
01    Flow No 1 will be executed            i = 0
02    Flow No 2 will be executed            Condition Checking
03    Flow No 3 will be executed        True Condition and print Hello World
04    Flow No 4 will be executed              -
05    Flow No 5 will be executed            i = 1
06    Flow No 3 will be executed        True Condition and print Hello World
07    Flow No 4 will be executed              -
08    Flow No 5 will be executed            i = 2
09    Flow No 3 will be executed        True Condition and print Hello World
10    Flow No 4 will be executed              -
11    Flow No 5 will be executed            i = 3
12    Flow No 3 will be executed        True Condition and print Hello World
13    Flow No 4 will be executed              -
14    Flow No 5 will be executed            i = 4
15    Flow No 3 will be executed        True Condition and print Hello World
16    Flow No 4 will be executed              -
17    Flow No 5 will be executed            i = 5
18    Flow No 3 will be executed            False Condition

Therefore it gives five times Hello World . Ok Guys i think u will understand of whole scenario.

Return Statement In Java

Return statement

The return statement is used to explicitly return from a method.That is it transfer the control to the caller of method.
And most important , return statement is always be the last statement of the current block.

Simple Program for Return statement:

package org.modi;
public class Test
{
    public static void main(String[] args)
    {
       System.out.println("Main method begin");
       if(true)
       {
          return; //it is last statement of current (if) block
       }
       System.out.println("Main method end");
    }
}
OUTPUT
Main method begin
From this program we can see that , when return statement encountered the execution of method terminated.