Showing posts with label object oriented concepts. Show all posts
Showing posts with label object oriented concepts. Show all posts

Wednesday, 2 October 2013

Classes and Objects


Class :
          Class is the blue print of the real objects to be created. The class gives the skeleton for an entity to be created for real time processing. Class is a software template that defines the methods and variables to be included in a particular kind of Object.
A class may contain member variables, methods, constructors and static blocks which can be used to define and manupulate state and behavious of an object.

Object :
           The java is all about objects. An Object is the real entity in the world. Every object will have its state and behavior. Concept of Object can be easily understood taking any really time entity around us say Dog, television, Fridge or car etc.  A car may have states like number of wheels, type of engine etc and behvior like way of start, speedup etc.

Below is the syntax for the class.

Access specifier Class ClassName
{
                Member variables --- defines state of an object
                Methods --- defines the behaviour of the object
}

Object declaration will be like below:

ClassName object = new ClassName()

Example:
        We can define a class Vehicle to have an skeleton for all types of vehicles by preparing set of properties and type of processes required to use those vehicle. After that we can create real time object such as car, truck or tempo depending upon the states(properties).

Declaring a class : Vehicle
public class Vehicle
{
            //member variables which will define the state of a vehicle
private int wheels;
            private String sengineType;

            //setter method to set number of wheels
public void setWheels(int wheels)
{
                this.wheels = wheels;
}

//getter method to retrieve the number of wheels
public int getWheels()
{
                return this.wheels;
}             
}

See below, how will we use this class to instantiate any real object.
public class Demo
{
            //main method
            Public static void main(String args[])
{
            //calling default constructor to create an object.
Vehicle car = new Vehicle();       
Vehicle tempo = new Vehicle();

//calling setter method to set the states of the car object
            car.setWheels(4);
            tempo.setWheels(3);

            //calling getter method to access the object states
            System.out.println(“ Car : No. of wheels :”+car.getWheels());
System.out.println(“ Tempo : No. of wheels :”+tempo.getWheels());
}
}


.

Thursday, 15 August 2013

Features of JAVA - Reasons to use java over other languages

Java - Features

Java was developed by James Gosling, Chris Warth, Patrick Naughton, Ed Frank and Mike Sheridan at Sun Microsystems Inc. in 1991 which was initially termed as 'Oak' and later renamed to 'Java' in 1995. The fundamental necessities which led to evolution of java were portability and security.
There are several reasons on why to use java as the programming language over other languages. Few of them are as listed below:

Salient features of JAVA: 

Platform Independent
It is also termed as Write Once, Run Anywhere, any time, forever . A Java program that is written and compiled in one platform can run on any other platform without any recompilation or modification. This was achieved with the creation of Java Virtual Machine(JVM). The JVM is an interpreter which is platform dependent. All the JVMs accept the same input i.e. byte code and interpret it into machine language format of the corresponding platform. the basic flow is as given in below diagram.

basic flow in java programming

Simple
Java is designed in such a way that a professional programmer can learn it very easily and effectively.It is even better for a person to learn it if he understands the basic concepts of object oriented programming or has experience in C/C++.

Secured
It is one of the most innovative aspect of Java. It is achieved by confining an applet to the Java execution environment and not allowing access to other parts of the computer where the applet is downloaded. This is ensured by the use of JVM which is in control, can contain program  and prevent it from generating the side effects.

Potability
Portability becomes very important feature when we move to internet as many different types of computers and operating systems are connected to it. One single java program must be running on different kinds of CPU's, operating systems or browsers. this is achieved when we have the byte code of the java program which can be run on variety of environments as only JVM needs to be implemented there on platform.

Object Oriented
Java is one of the most powerful OOP languages.The Object model in java is simple and very easy extend. Java provides ways to implement all OOP concepts like Inheritance, Polymorphysm, Abstraction, Encapsulation and many more.

Robust
When it comes to a multi-platformed environment, reliability becomes the most demanding aspect of a program. Java forces you to find your mistakes early in the programming. Compile time and run time checks implemented in java helps to be free of worries about very common causes of errors. two very important feature to be considered for robustness in java are Memory management and Exception handling.

Multithreaded
java supports multithreaded programming which allows a programmer to write programs that can perform different actions simultaneously. In this feature, multiple light weighed process can run concurrently to use resources more effectively.

Distributed
Java handles TCP/IP protocols thus can be used for programs in distributed environment. Java also supports Remote Method Invocation (RMI) which makes it possible to call method from one program across the network.

High Performance
Just-in-time compiler plays the big role in making java with high performance. this converts the byte code in the native machine code for efficient outputs.