Monday 16 February 2015

Introduction to Java

       Java is an Object Oriented ,Platform Independent Programing Language .Object Oriented Programming (OOP) is a programming methodology based on objects ,Instead of just functions and procedures. These objects are organized into Classes which allow individual objects to be grouped together.

Object: An object is an entity that keeps together state and behaviors. An Object can be a variable, data structure or a function and it refers to the particular instance of a class.
Eg: A Dog has a states like color, name and behavior like barking,eating 

Class: A class is a blueprint or template or set of instructions to build a specific type of object. It defines the characteristics of the objects such as attributes and actions or behaviors
Eg: A Dog is a class which defines the states and behavior but no physical existence. 

Method: A method is same as a procedure, function , or routine in procedural programming languages. The only difference is that in object-oriented programming, a method is always associated with a class. 
Eg: The behavior of a Dog i.e barking will be stored in the method . and the states i.e color/name will be stored in a field.
 public class Dog{ //class 
                                String name; 
                                 int age; //Field 
                                String color; 
                                  void barking(){ } //Method 
                                   void eating(){ }
                                  }} 

Principles of Object Oriented Programming: 
There are mainly 4 OOP Principles 
            1)Encapsulation 
             2)Polymorphism 
             3) Inheritance 
             4)Abstraction 

1) Encapsulation: Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse or in other words it is a concept of hiding data to prevent the access from outside. 
Java supports Keywords like public, private, protected to implement encapsulation.

 2) Polymorphism: Polymorphism means many forms . It is an OOP concept that refers to the ability of a variable,function or object to take on multiple forms.
Eg: If we take an example of a car , we can get many offers like power/normal steering . 4/6/8-Cylinder engines and different types of braking system on same car . Still you have to press the same break padel to stop ,turn the steering wheel to change direction. 

3) Inheritance: Inheritance is the process by which one object acquires the property of another object. 
Eg: Mammal and Reptile is a class which is under the class Animal. Mammal might have some other unique properties with all the properties acquired from the the class animal

 4) Abstraction : Abstraction is the act of representing essential features without including the background details or explanations. 
 Eg : Even though a car consists 1000 of parts people consider it as whole . They can drive the car without being overwhelmed by the complexity of the parts that form the car. 

How Java code will be executed? 
       As java is platform Independent language the source code will be converted to intermediate code called bytecode. Bytecode is the highly optimized set of instructions designed to be executed by Java runtime system i.e Java Vertual Machine (JVM). JVM uses Just-In-Time(JIT) compiler to execute the bytecode. 

JIT compiler will convert only the selected portion of bytecode to executable code in real time on a piece by piece or demand basis instead of compile an entire Java program into executable code all at once. Hence it will not effect on performance even though there is intermediate bytecode conversion. 

Reference: Java The Complete Reference  By Herbert Schild

No comments: