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.

Inheritance


 Inheritance:
 It is the concept of getting the properties of one object of a class to another object of a class.                                                                                                                                                                                                                                                                                                                                                       ‘          Java supports only multilevel inheritance.

Inheritance types:
1.Single level inheritance: It is the concept of extending the features of one class to another class.
Example:
     class X
{
Int I;
Void fun1(){}
class Y extends X
{
Int j;
Void fun2(){}
}
2.Multilevel inheritance: One class extending another class as in a hierarchical structure is termed as multilevel inheritance.
Example:
     class X
{
Int I;
Void fun1(){}
class Y extends X
{
Int j;
Void fun2(){}
}
class Z extends Y
{
}
3. Multiple inheritance: One class extending two another classes simultaneously is known as multiple inheritance.
    C++supports multiple inheritance.
     class X
{
Int I;
Void fun1(){}
class Y extends X
{
Int j;
Void fun2(){}
}
class Z extends X, Y
{
}
Java will not support multiple inheritance.
4. Cyclic inheritance: One class extends the next class and so on and the last class extends the first class forming a circle.

Encapsulation


Encapsulation:

Encapsulation is the concept of binding data along with its corresponding functionalities.
It came into existence in order to provide security for the data present inside the program. Any object oriented programming language file looks like a group of classes. Everything is encapsulated, nothing is outside the class. Encapsulation is the back bone of oop language.
Example is class.

Class Encapsulation
{       
   // the Bicycle class has
    // three fields
    int cadence;
    int gear;
    int speed;
    void setGear()
    void applyBrake()
}


Sunday, 18 March 2012

OOPS Concepts


Oops concepts
This tutorial will help you to understand about Java OOPS concepts with examples. Java is a Object Oriented Programming Language (OOPS). Lets we discuss about what are the features of OOPS. There are four main features of OOPS.
1) Encapsulation
2) Inheritance
3) Polymorphism
4) Abstraction
Lets we discuss about the about features in details.
Object:
An object is a software bundle of related state and behavior. Software objects are often used to model the real-world objects that you find in everyday life. This lesson explains how state and behavior are represented within an object, introduces the concept of data encapsulation, and explains the benefits of designing your software in this manner.

Class:
A class is a blueprint or prototype from which objects are created. This section defines a class that models the state and behavior of a real-world object. It intentionally focuses on the basics, showing how even simple classes can cleanly model state and behavior.
Example:
 class A
{
Int a;
Int b;
Void fun1()
Int fun2()
}



Friday, 16 March 2012

Introduction to java


Sun has defined and supports four editions of Java targeting different application environments and segmented many of its APIs so that they belong to one of the platforms. The platforms are:
§  Java Card for smartcards.
Every edition will help to its own application development. Normally java concepts are on java SE, here we given concepts for that……
Introduction to java
Java is an object-oriented programming language with a built-in application programming interface (API) that can handle graphics and user interfaces and that can be used to create applications or applets. Because of its rich set of API's, similar to Macintosh and Windows, and its platform independence, Java can also be thought of as a platform in itself. Java also has standard libraries for doing mathematics.
Much of the syntax of Java is the same as C and C++. One major difference is that Java does not have pointers. However, the biggest difference is that you must write object oriented code in Java. Procedural pieces of code can only be embedded in objects. In the following we assume that the reader has some familiarity with a programming language. In particular, some familiarity with the syntax of C/C++ is useful.
In Java we distinguish between applications, which are programs that perform the same functions as those written in other programming languages, and applets, which are programs that can be embedded in a Web page and accessed over the Internet. Our initial focus will be on writing applications. When a program is compiled, a byte code is produced that can be read and executed by any platform that can run Java.
Advantages of java
JAVA offers a number of advantages to developers.

Java is simple: Java was designed to be easy to use and is therefore easy to write, compile, debug, and learn than other programming languages. The reason that why Java is much simpler than C++ is because Java uses automatic memory allocation and garbage collection where else C++ requires the programmer to allocate memory and to collect garbage. 

Java is object-oriented: Java is object-oriented because programming in Java is centered on creating objects, manipulating objects, and making objects work together. This allows you to create modular programs and reusable code.

Java is platform-independent: One of the most significant advantages of Java is its ability to move easily from one computer system to another.
The ability to run the same program on many different systems is crucial to World Wide Web software, and Java succeeds at this by being platform-independent at both the source and binary levels.

Java is distributed: Distributed computing involves several computers on a network working together. Java is designed to make distributed computing easy with the networking capability that is inherently integrated into it.
Writing network programs in Java is like sending and receiving data to and from a file. For example, the diagram below shows three programs running on three different systems, communicating with each other to perform a joint task.

Java is interpreted: An interpreter is needed in order to run Java programs. The programs are compiled into Java Virtual Machine code called byte code.
The byte code is machine independent and is able to run on any machine that has a Java interpreter. With Java, the program need only be compiled once, and the byte code generated by the Java compiler can run on any platform.

Java is secure: Java is one of the first programming languages to consider security as part of its design. The Java language, compiler, interpreter, and runtime environment were each developed with security in mind. 

Java is robust: Robust means reliable and no programming language can really assure reliability. Java puts a lot of emphasis on early checking for possible errors, as Java compilers are able to detect many problems that would first show up during execution time in other languages. 

Java is multithreaded: Multithreaded is the capability for a program to perform several tasks simultaneously within a program. In Java, multithreaded programming has been smoothly integrated into it, while in other languages, operating system-specific procedures have to be called in order to enable multithreading. Multithreading is a necessity in visual and network programming.