Showing posts with label class. Show all posts
Showing posts with label class. 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());
}
}


.

Sunday, 18 August 2013

Simplifying Encapsulation

Encapsulation

                Encapsulation in very simple terms means data/information hiding. Encapsulation is a process or mechanism which binds the data and methods together and keeps them safe from outside interference, misuse and access. It is about not allowing arbitrary access to the code written in one module from other modules. The access to these codes is provided through a well defined interface.

Let’s take a real world example to understand the concept.

In a vehicle, there are several parts being used in the engine like shaft, valves, piston, heater etc. But if we see, we are not affecting any of these components and their functionality.We are given just one interface to use them is to start the engine. In this way the internal details are kept unaffected from external impact and similarly the engine parts are not affecting anything outside.

                Coming to Java, the encapsulation is achieved by a class. A class is a blue print for a real time entity defining its structure and behavior. The data defined in a class are known as member variables/instance variables and the code which operates/manipulates the data are known as member methods.

Below image explains the use of encapsulation:

Encapsulation explained in pictorial view 



                   Private methods/private variables are accessed only by the same class. Public variables/methods are exposed to outside world to access the private data.

                  To hide the complexity, methods and variables are declared with the access specifiers. Typically variable are declared as private and the methods as public.

Private: accessible within the class.

Public: accessible from anywhere.

Default: accessible within the package.

Protected: accessible from all the sub classes within same/different packages.


A sample class code would look like as below:

A sample class code


Here the private member variable cannot be accessed from outside this class, so public getter and setter methods are declared to access from outside only through these methods.