Sunday, 22 January 2017

BASIC JAVA PROGRAMMING ELEMENTS

Java has 6 basic programming elements to develop.
1. package - It is a java folder used to group related classes, interface and enums which is also used to separate new classes from existing classes if both have the same name.
2. class
3. interface
4. enum
5. variable - It is a named memory location used to store java data, such as numbers, characters, strings etc.
6. method - It is a sub block of a class used to implement logic of an objects operation.


Why do we need an object ?
In order to load the members of the class from the byte code to the RAM in the form of executable format and makes them available to the program which is under execution. Without using the concept of object, we can't load the members of class from the byte code to the RAM at the runtime. The concept of object would be required in those technologies which are working based on the concept of dynamic loading. The concept of object wouldn't be  required in those technologies which works on the concept of static loading. Thus the concept of object wouldn't be required in structured programming language programs like C-programs.

What is an object ?
Object is an instance of ac class. The memory space which is allocated for the members of a class, dynamically at the runtime, can be called as an object. Objects could be created only for the classes, thus object's can't be created without a class.

What is an instance ?
Any dynamic memory allocation can be called as an instance i.e. a memory space allocated for anything during runtime can be called as an instance. But all dynamic memory allocations can't be called as an object. The memory space allocated (in the RAM) only for the member's of a class can be called as an object. Thus instance of anything is not an object, only the instance of a class is an object. Thus every object could be an instance,but every instance can't be an object.

Reference
Reference is a variable which represents the address of the object. Thus reference acts as a pointer to the object and also a handler to the object. Since a reference is pointing to an object, in practice people call the reference as an object. But strictly speaking, reference is not an object, it's only a handler to the object i.e. using the reference we can access the object.
Demo a1 = new Demo();

Difference between class and interface ?
Interface is fully an unimplemented class, it is used to define set object operations, where as class is fully an implemented class, which is used for implementing an object operations. Hence interface does not allow method with logic.
For example - Bank is an object created as an interface in java and ICICIBank , SBIBank are created as classes with bank operations like withdraw, deposit implementations.

Why enum ?
It was introduced in java 5 version to create set of named constants for creating menu kind of items such as restaurant, bar menu. Before java 5 this menu was created using class, since it was creating some problems, sun introduced enum with new syntax and rule.
enum Month{
jan,feb,march;
}

No comments:

Post a Comment