Saturday, 29 December 2012

Abstract Class in Java

There are situations in which you will want to define a superclass that declares the structure of a given abstraction without providing a complete implementation of every method. That is, sometimes you will want to create a superclass that only defines a generalized form that will be shared by all of its subclasses, leaving it to each subclass to fill in the details. Such a class determines the nature of the methods that the subclasses must implement. One way this situation can occur is when a superclass is unable to create a meaningful implementation for a method. This is the case with the class Figure used in the preceding example. The definition of area( ) is simply a placeholder. It will not compute and display the area of any type of object.
As you will see as you create your own class libraries, it is not uncommon for a method to have no meaningful definition in the context of its superclass. You can handle this situation two ways. One way, as shown in the previous example, is to simply have it report a warning message. While this approach can be useful in certain situations—such as debugging—it is not usually appropriate. You may have methods which must be overridden by the subclass in order for the subclass to have any meaning. Consider the class Triangle. It has no meaning if area( ) is not defined. In this case, you want some way to ensure that a subclass does, indeed, override all necessary methods. Java's
solution to this problem is the abstract method.
You can require that certain methods be overridden by subclasses by specifying the abstract type modifier. These methods are sometimes referred to as subclasser responsibility because they have no implementation specified in the superclass. Thus, a subclass must override them—it cannot simply use the version defined in the superclass. To declare an abstract method, use this general form:
abstract type name(parameter-list);
As you can see, no method body is present. Any class that contains one or more abstract methods must also be declared abstract. To declare a class abstract, you simply use the abstract keyword in front of the class keyword at the beginning of the class declaration. There can be no objects of an abstract class. That is, an abstract class cannot be directly instantiated with the new operator. Such objects would be useless, because an abstract class is not fully defined. Also, you cannot declare abstract constructors, or abstract static methods. Any subclass of an abstract class must either implement all of the abstract methods in the superclass, or be itself declared abstract.
Here is a simple example of a class with an abstract method, followed by a class which implements that method:
 

// A Simple demonstration of abstract.
abstract class A {
abstract void callme();
// concrete methods are still allowed in abstract classes
void callmetoo() {
System.out.println("This is a concrete method.");
}
}

class B extends A {
void callme() {
System.out.println("B's implementation of callme.");
}
}
class AbstractDemo {
public static void main(String args[]) {
B b = new B();
b.callme();
b.callmetoo();
}
}


Notice that no objects of class A are declared in the program. As mentioned, it is not possible to instantiate an abstract class. One other point: class A implements a concrete method called callmetoo( ). This is perfectly acceptable. Abstract classes can include as much implementation as they see fit.
Although abstract classes cannot be used to instantiate objects, they can be used to create object references, because Java's approach to run-time polymorphism is implemented through the use of superclass references. Thus, it must be possible to create a reference to an abstract class so that it can be used to point to a subclass object. You will see this feature put to use in the next example. Using an abstract class, you can improve the Figure class shown earlier. Since there is no meaningful concept of area for an undefined two-dimensional figure, the following version of the program declares area( ) as abstract inside Figure. This, of course, means that all classes derived from Figure must override area( ).
 

// Using abstract methods and classes.
abstract class Figure {
double dim1;
double dim2;
Figure(double a, double b) {
dim1 = a;
dim2 = b;
}
// area is now an abstract method
abstract double area();
}

class Rectangle extends Figure {
Rectangle(double a, double b) {
super(a, b);
}
// override area for rectangle
double area() {
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2;
}
}
class Triangle extends Figure {
Triangle(double a, double b) {
super(a, b);
}
// override area for right triangle
double area() {
System.out.println("Inside Area for Triangle.");
return dim1 * dim2 / 2;
}
}
class AbstractAreas {
public static void main(String args[]) {
// Figure f = new Figure(10, 10); // illegal now
Rectangle r = new Rectangle(9, 5);
Triangle t = new Triangle(10, 8);
Figure figref; // this is OK, no object is created
figref = r;
System.out.println("Area is " + figref.area());
figref = t;
System.out.println("Area is " + figref.area());
}
}


As the comment inside main( ) indicates, it is no longer possible to declare objects of type Figure, since it is now abstract. And, all subclasses of Figure must override area( ). To prove this to yourself, try creating a subclass that does not override area( ). You will receive a compile-time error.
Although it is not possible to create an object of type Figure, you can create a reference variable of type Figure. The variable figref is declared as a reference to Figure, which means that it can be used to refer to an object of any class derived from Figure. As explained, it is through superclass reference variables that overridden methods are resolved at run time.

Monday, 24 December 2012

Keywords in Java

 There are 50 keywords in Java

1.abstract
2.assert
3.boolean
4.break
5.byte
6.case
7.catch
8.char
9.class
10.const
11.continue
12.default
13.do
14.double
15.else
16.enum
17.extends
18.final
19.finally
20. float
21.for
22.goto
23.if
24.implements
25.import
26.instanceof
27.int
28.interface
29.long
30.native
31.new
32.package
33.public
34.private
35.protected
36.return
37.short
38.static
39.strictfp
40.super
41.switch
42.synchronized
43.this
44.try
45.throw
46.throws
47.transient
48.void
49.volatile
50.while

Functions


What is a Function?

A function is a block of code that has a name and it has a property that it is reusable i.e. it can be executed from as many different points in a C Program as required.
Function groups a number of program statements into a unit and gives it a name. This unit can be invoked from other parts of a program. A computer program cannot handle all the tasks by it self. Instead its requests other program like entities – called functions in C – to get its tasks done. A function is a self contained block of statements that perform a coherent task of same kind
The name of the function is unique in a C Program and is Global. It neams that a function can be accessed from any location with in a C Program. We pass information to the function called arguments specified when the function is called. And the function either returns some value to the point it was called from or returns nothing.
We can divide a long C program into small blocks which can perform a certain task. A function is a self contained block of statements that perform a coherent task of same kind.

Structure of a Function

There are two main parts of the function. The function header and the function body.
int sum(int x, int y)
{
 int ans = 0;  //holds the answer that will be returned
 ans = x + y; //calculate the sum
 return ans  //return the answer
}

Function Header

In the first line of the above code
int sum(int x, int y)
It has three main parts
  1. The name of the function i.e. sum
  2. The parameters of the function enclosed in paranthesis
  3. Return value type i.e. int

Function Body

What ever is written with in { } in the above example is the body of the function.

Function Prototypes

The prototype of a function provides the basic information about a function which tells the compiler that the function is used correctly or not. It contains the same information as the function header contains. The prototype of the function in the above example would be like
int sum (int x, int y);
The only difference between the header and the prototype is the semicolon ; there must the a semicolon at the end of the prototype.

Structure of a Function

There are two main parts of the function. The function header and the function body.
int sum(int x, int y)
{
 int ans = 0;  //holds the answer that will be returned
 ans = x + y; //calculate the sum
 return ans  //return the answer
}

Function Header

In the first line of the above code
int sum(int x, int y)
It has three main parts
  1. The name of the function i.e. sum
  2. The parameters of the function enclosed in paranthesis
  3. Return value type i.e. int

Function Body

What ever is written with in { } in the above example is the body of the function.

Function Prototypes

The prototype of a function provides the basic information about a function which tells the compiler that the function is used correctly or not. It contains the same information as the function header contains. The prototype of the function in the above example would be like
int sum (int x, int y);
The only difference between the header and the prototype is the semicolon ; there must the a semicolon at the end of the prototype.

Types Of Instructions in C


Types Of Instructions in C :

                      Basically three types of instructions in C:

Type Declaration Instruction  --To declare the type of variables used in a C program.

Arithmetic instruction             -- To perform arithmetic operations between con-stants and variables.

Control Instruction           --      To control the sequence of execution of various state-ments in a C program. 



Type Declaration Instruction :                  Arithmetic Instruction :    


          EX:          int bas ;                                         EX: int i=i+1;
                         float rs, grosssal ;
                         char name, code ;

Control Instructions in C program :

                          4 Types:
 They are:

(a) Sequence Control Instruction  --->Example of line by line execution without Interruption
(b) Selection or Decision Control Instruction- if,if-else,switch-case
(c) Repetition or Loop Control Instruction. -->while,do-while and for 





Variables & Operators:


Variables & Operators:

               Variable is nothing but Memory Location.When using a variable you refer to the memory address of the computer .

Ex:     int a;// Declaration
          int a=10; // Initialization(Here 10 is called constant or Literals).

Operators in C

                      
 Operators are:
1. Arithmetical operators
2. Logical operators
3. Relational operators
4. Bitwise operators
5. Shift operators


Arithmetic operators:

                  These operators are used for the for mathematical expression like (Addition ‘+’, Subtraction ’-’ ,  Multiplication ‘*’ , Division ’/’, Modulus ‘%’)     addition is used to two or more than two expression, same as subtraction is used to find the difference, etc. But there is difference  between division and  modulo i.e., modulo always gives the remainder.
for example: 7%2 then it gives modulo 1 because remainder is 1.


Logical Operators : 

      &&-------------Logical And (a>b&& x==10)
      ||-------------logical OR (a<m||a<n)
      !---------------Logical Not (! (x>=y)  Not exp.evaluates values of x neither greater than or equal to y)




  Assignment operator:    (x=a+b;) means value of (a+b) is stores in x.
     
 Bitwise operator:           (a: 100101 ~a: 011010)
      
 Shift operator:       These are of two types –
                                              a) Right shift(a>>L)
                                              b) Left shift (a<<L)

Data Types in C



Data Types in C  Language

             2 types :

 1.Simple or Primitive :  
                         integers, floats, characters, pointers.
        2.Compound or derived or Structured :
                           Derived data is made up of one or more simple data items.such as arrays, structures, unions.

I wanna give Just-an-Outline about them :

        Integers in C:

                 Integers stores numeric value without a decimal point in the system memory.

Memory Allocation :1Short int      ----:>     2 Bytes
                             2. Int                            ----:>     4 Bytes
                             3.Long int                    ----:>    4 Bytes


        Floats in C :
             
                        It stores numeric values with decimal point in the system memory.

Memory Allocation :  1. Float            ----:>          4 Bytes
                               2.Double                       ----:>          8 Bytes
                               3.Long double              ----:>          10 Bytes


       Characters in C :

                Characters is data type which stores an element of machine character set.
     The character set is used is usually ASCII set, it is denoted by char. It takes only one byte. Also signed and unsigned both occupy one byte having different  ranges.
     The primary data type themselves could be of several types. for example Character char could be Unsigned char. or Signed char. The    values  stores in the given integer variables will always be be positive. for example: we can declare a variables to be unsigned.
   unsigned int num_student,
    The range of integer values is -32768 to +32767 values for a 16 bit OS to range 0 to 65535.char ch =A; where ASCII value of A is 65.
                           


Characters Types, Size in Bytes and Range
                                                     Type Name             Bytes           Range
                                                            ------------- 16 bit system -------------
                                                       
                                                             char                          1           -128 to 127
                     
                                                          signed char               1             -128 to 127
                      
                                                       unsigned char            1               0 to 255
                     
                                                         short                           2             -32,768 to 32,767
                      
                                                      unsigned short          2              0 to 65,535
                     
                                                             int                            2            -32,768 to 32,767
                    
                                                       unsigned int              2             0 to 65,535
                   
                                                         long                         4             -2,147,483,648 to 2,147,483,647
                      
                                                       unsigned long           4              0 to 4,294,967,295
                      
                                                           float                         4            3.4E+/-38 (7 digits)
                     
                                                          double                     8             1.7E+/-308 (15 digits)
                      
                                                        long double            10              1.2E+/-4932 (19 digits)





                                  ------------ 32 bit system -------------
                                Type Name             Bytes           Range

                          char                      1           -128 to 127
                      

                           signed char           1           - 128 to 127
                      

                            unsigned char       1           0 to 255
                      

                           short                    2           -32,768 to 32,767
                     

                      unsigned short            2           0 to 65,535
                    

                         int                      4           -2,147,483,648 to 2,147,483,647
                     

                         unsigned int          4           0 to 4,294,967,295
                      

                        long                     4           -2,147,483,648 to 2,147,483,647
                      

                          unsigned long       4           0 to 4,294,967,295
                     

                          float                     4           3.4E+/-38 (7 digits)
                      

                           double                 8          1.7E+/-308 (15 digits)
                     

                            long double        10         1.2E+/-4932 (19 digits)

History of C


when u come into Java . U need the basics of C and C++ . so I myself writing this blog for my reference and your reference.

History of C:
    
     C is programming language developed in the  at AT and Tee Bell Laboratories of USA  in 1972's. It was designed and developed by    man named  Dennis Richte. C is so popular  because it reliable and  simple and easy to use, with the help of C language new languages are born known as C++, Java, C#  and many other languages
   
      Before C the Language Introduced called B ,which is Interpreter based and Read the Source code line by line and It's performance lacking with B. 

   So Dennis Ritchie developed language known as C in 1972’s. And it was famous as Mother Language of the computer system. It was based on compiler.

   Strong Point as compare to B 
                  
                    In C language Compiler (Translator) compiles the whole source code to Machine code where as interpreter read the source code line by line or step by step.

   
  Features of C  :
   
                   C is called a structured programming language because to solve a large problem, C programming language divides the individual small responsibilities to smaller modules called functions or procedures.


A sample of C Program
     
      #include <stdio.h>     // Preprocessor
        main ()                // main function
      {                      // opening the brackets
      Printf(“Hello”);
       }                      // close the brackets. 
    Advantages:

         1. c is a small ,flexible,efficient and powerful programming language.
         2.LINUX is written in C.
         3.Many languages borrows the syntax of c like c#,C++,Java.
         4.It has Libraries.
         5.C is standardized, making it more portable compare to other languages.

    Disadvantages :

        1.C is not supporting Multi-threading and oops Concepts.
        2.C was not able to automatic checking while comparing to other languages.