28 December, 2010

Class and Object

Question: What is a Class?
Ans: A class is a template that defines the member property and methods which are common to all objects of a certain kind.

Question: What is an object?
Ans: An object is an instance of a class.

Question: What are the feature of OOP: 
1)Abstraction :The process of extracting the essential information and hiding the irrelevant details.
2)Encapsulation: The process of binding code and data together in a class.
3)Inheritance: The feature by which one class acquires the properties and functionalists of super class. 
4)Polymorphism: It means one name many form.This allows the same method to be used for a general set of actions.

24 December, 2010

Access Modifier

Access Modifiers

1. private
2. protected
3. default
4. public

public access modifier:
Fields, methods and constructors declared public within a public class are visible to any class in the Java program, whether these classes are in the same package or in another package.
Ex: public String name;


private access modifier:
they cannot be accesses by anywhere outside the enclosing class. A standard design strategy is to make all fields private and provide public getter and setter methods for them.

Ex: private int balance;

protected access modifier:
Fields, methods and constructors declared protected in a class(say superClass) is accessed in same class,same package and subclass deriving the class(superClass)

Ex: protected float age;

default access modifier:
Any class, field, method or constructor that has no declared access modifier is accessible only by classes in the same package. 

Ex:  String interest


Same Class
Same Package
Subclass
Other packages
public
Y
Y
Y
Y
protected
Y
Y
Y
N
default
Y
Y
N
N
private
Y
N
N
N
               

01 December, 2010

Java Basics

Java is an object oriented programming language. Java is a trademark of Sun Microsystems, Inc. Java started in 1991. It is a platform-independent programming language.A platform is a place to launch software. It means that programmer need not be concerned about the hardware or operating system platform. It is different from C as C compiler produce Machine code or native code which is platform dependent.Machine code or machine language is a system of instructions and data executed directly by a computer's central processing unit(CPU) . Every processor or processor family has its own machine code instruction set. The instruction set is thus specific to a class of processors


               C compiler                      CPU

C Prog. ----------------->m/c code-------------->  o/p


m/c code looks like : mov cx,0x0004

Thus C is platform dependent.

The Java compiler does not produce native executable code for a particular machine like a C
compiler would. Instead it produces a special format called byte code.Java byte code written in
hexadecimal, byte by byte,looks like this:
CA FE BA BE 00 03 00 2D 00 3E 08 00 3B 08 00 01 08 00 20 08

                 JavaC compiler                     JVM                                          CPU
Java Prog--------------------> bytecode----------------> native language------------------> o/p      

Thus Java is platform dependent.   




Difference between Java and C
Java C
Object oriented prog. Procedure oriented prog
No Pointer Uses pointer
automatically garbage collection manually garbage collection using malloc() and free
variables can be declared anywhere variable should be declared in the beginning
Compiled to bytecode Compiled to machine code
Plateform independent Plateform dependent