Monday, 19 March 2012

Object defining and Creation


Object defining and creation:
We can define the object is the procedure of loading the contents of hard disk to the RAM dynamically at run time is done by creating an object.
Creation of object is done by using ‘new’ operator. it is a keyword in java. an important responsibility of the new operator  I to transfer the contents of .class file from the hard disk into the RAM.
Example:
Class A
{
Int i=1, j=2;
Public static void main (String args[])
{
A a=new A ();
a.i=10;
a.j=20;
}
}

This keyword


This keyword:
Whenever it is required to point an object from a functionality which is under execution because of that object, then we use the ‘this’ keyword.
          The job of this keyword is only to point.It is always points to an object that is executing the block in which ‘this’ keyword is present. In a nut-shell this keyword points to the current object.
Example:
       Class A
          {
          Int  i;
          A(int  i)
          {
This.i= i+1;
          i=i+1;
          system.out.println (i);
          }
          Void function ()
          {
int i=67;
sysyem.out.println (i);
          sysyem.out.println (this. i);
          this.i= this.i+1;
}
          Public static void main (String args[])
          {
          A  a= new A( 6);
          sysyem.out.println (a.i);
a.function ();
sysyem.out.println (a.i);
//this.i=203;
}
}
Note: this operator points to address of the instance. It will never points to the context of the class.
       We know that static functions are not executed with the address of the instance even though we call then using it. that is why ‘this’ operator works only in non-static blocks. It will not work in static blocks.
·        Constructor is a non-static block, that is why we are able to use ‘this’ operator inside a constructor.
·        We can call a constructor explicitly, without creating another object of that class only through a constructor.
·        We can’t constructor through functions.
Uses of this operator:
1.    To point a constructor of a class from the constructor of the same class.
2.    To point current object from the non-static blocks . 

Constructor


Constructor:
A constructor is a function which would be executed only at the time of creating an object.
      When the object is created the constructor will be executed.
Example:
Class A
{
int   i,j;
A ()
{
System.out.println (“inside constructor A()”);
I=5;
J=6;
}
Void fun ()
{
system.out.println (“i”);
system.out.println (“j”);
}
Public static void main (String args[])
{
A a=new A();
a.fun ();
}
}
Comparisons between a constructor and a function:
·        A normal function can be called as and when we required to call it, but we can’t call a constructor as and when we want.
·        Constructor would be executed only once on the object and that too, only at a time of object creation. A function can be called on an object as many number of times as we want.
·        Name of the function can be any name, but name of the constructor must and should be the name of the class.
·        A constructor will not have any return type, where as a function should have a return data type (at least void).
·        The keyword static can’t be used with constructors. It is applicable to functions and other variables.
·         We know that whenever jvm encounters the new operator, it creates an object of that class and loads the non-static members of the class into that object and then the address of object is assigned to a variable.
            But something other than this happens prior to this when JVM encounters the new operator, here we can see the function of constructors.
There are three types of constructors
1. Parameterized constructor
2. Copy constructor
3. Dafault constructor
Normally copy constructor does not used in java.
Parameterized constructor: the constructor which have the arguments, that in any number of arguments.
Example:
Class X
{
Int a, b;
          X(int i)
          {
A=I;
s.o.p(“inside constructor”);
}
X(int i, int j)
{
a=I;
b=j;
s.o.p(“inside constructor”);
}
Public static void main(String args[])
{
X  x= new X(6);
X x = new X(7,9);
}
}
Default constructor: the constructor which doesn’t have the arguments is known as default constructor.
Example:
    A(){}
If you don’t declare any constructor in a program it will take default constructor. We can’t see the constructor the compile will automatically execute that statement. In every constructor the first statement is super.

Basic Data Types


Basic data types:

Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.
Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables.
There are two data types available in Java:
1.    Primitive Data Types
2.    Reference/Object Data Types

Primitive Data Types:

There are eight primitive data types supported by Java. Primitive data types are predefined by the language and named by a key word. Let us now look into detail about the eight primitive data types.
·        Byte
·        Short
·        Int
·        Long
·        float
·        double
·        Boolean
·        Char

Reference Data Types:

·         Reference variables are created using defined constructors of the classes. They are used to access objects. These variables are declared to be of a specific type that cannot be changed. For example, Employee, Puppy etc.
·         Class objects, and various type of array variables come under reference data type.
·         Default value of any reference variable is null.
·         A reference variable can be used to refer to any object of the declared type or any compatible type.
·         Example : Animal animal = new Animal("giraffe");

Java Literals:

A literal is a source code representation of a fixed value. They are represented directly in the code without any computation.
Literals can be assigned to any primitive type variable.
Example:
byte a=65;
char a=’A’;

Method Overriding


Method overriding:
In a class hierarchy, when a method in a subclass has the same name and type signature as a method in its super class, then the method in the subclass is said to override the method in the super class. When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the subclass. The version of the method defined by the super class will be hidden.

Consider the following: 
// Method overriding. 
class A { 
int i, j; 
A(int a, int b) { 
i = a; 
j = b; 

// display i and j 
void show() { 
System.out.println("i and j: " + i + " " + j); 

}
class B extends A { 
int k; 
B(int a, int b, int c) { 
super(a, b); 
k = c; 
} 
// display k – this overrides show() in A 
void show() { 
System.out.println("k: " + k); 
} 
}
class Override { 
public static void main(String args[]) { 
B subOb = new B(1, 2, 3); 
subOb.show(); // this calls show() in B 
} 
}// Output:3

Method Overloading


Method overloading:

The process of defining more than one function with the same name, but with different arguments, within the same class is known as method overloading or function overloading.
     Static Polymorphism is implemented using function overloading.

Example:
 class  Poly1
{
 Void fun ()
    {
….
    }
Void fun (int x)
    {
…..
      }
}

Polymorphism


Polymorphism:

Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.
Any java object that can pass more than on IS-A test is considered to be polymorphic. In Java, all java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object.
It is important to know that the only possible way to access an object is through a reference variable. A reference variable can be of only one type. Once declared the type of a reference variable cannot be changed.

Example:
interface Vegetarian{}
class Animal{}
class Deer extends Animal implements Vegetarian{}
 
There are two types of polymorphism
1. Static polymorphism
2. Dynamic polymorphism 
 
 Examples for these two types can be explained below i.e. method overloading and method overriding.
 
Abstraction:
Abstraction refers to the ability to make a class abstract in OOP. An abstract class is one that cannot be instantiated. All other functionality of the class still exists, and its fields, methods, and constructors are all accessed in the same manner. You just cannot create an instance of the abstract class.
If a class is abstract and cannot be instantiated, the class does not have much use unless it is sub classed. This is typically how abstract classes come about during the design phase. A parent class contains the common functionality of a collection of child classes, but the parent class itself is too abstract to be used on its own.