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