Saturday, 31 October 2015

Object

2. OBJECT:
An Object represents state (instance variables) and behaviour (methods).
Each Object made from a class can have its own values for the instance variables of that class.        We can create n number of objects for same class.
Creating your first object:
Reqt:  
a.       a class for the type of object u want to use.
b.      Another class to test your new class where you put main().
Tester class:
Tries out the methods and variables of your new object class type.
Let’s start with  a popular Example specified in Head First Java.
The Guessing Game:
It involves a game object,3 player object.
Game generates a random number between 0-9.The three players try to guess it.
Logic:
1.GameLauncherClass --- where the appn starts.It has main()
2. In main(), a GuessGame object is created and its startGame() called
3. startGame() --- where the entire game playsout.
It creates three players, then thinks of random number. It then asks each player to guess, checks the result and either prints out the information about the winner or ask them to guess again.
 
Implementation:
Below are the  screen shots of the above example.
In below screens u may observer this
Math.random()*10 – aimed to provide random numbers


 
player class:
 
GameLauncher class:
Output:
 
Over here we have created three objects for player class(p1,p2,p3) and one object for GuessGame class(game).
 
This ends the basics of object. In My next post we’ll discuss on Encapsulation.
 

Saturday, 24 October 2015

OBJECT ORIENTED PROGRAMMING


OBJECT ORIENTED PROGRAMMING

In this a main program is divided into small objects depending on the problem. Functions of object are linked with object using message passing. Data and functions of each individual object act like a single unit.

Data gets more importance than function in the program.

Each object controls its own data.

Data hiding is possible in OOP which prevents illegal access of function from outside of it.

More data and function can be added to program if required. It follows Bottom-up approach for program design. Popular Example: c++,java

 

From the above para we can infer that an object is an important part over here.So what an object is?

It has many definitions.Lets get to the basic one thatis  “it is a real world entity”.It could be a desk,T.V, bicycle etc.

These objects share two characteristic:

1.       State

2.       Behaviour

AN example would be :

A desktop lamp which has 2 states – (on and off) and 2 behavior (turn on && turn off).

An object stores its state in fields (variables) and exposes its behaviour through methods(functions).

Methods operate on an objects internal state and serve as the primary mechanism for object-object communication.

 

Features of OOP:

1.class

2.object

3.encapsulation

4.abstraction

5.inheritance

6.message passing

7.polymorphism.

Lets discuss each in detail.

1.       Class

Can be said as  a blueprint to create objects.This blueprint has attributes and methods that all created object share.

A class can be a person,place or thing.Fundamentally it encapsulates state andbehavior

Syntax of class:

         class  class_name{

                properties(variables);

                Action(methods);

}

 

Variables inside a class are called instance variables(Things that object knows about itself).

Variables inside a method are called method variables.

Methods are things an object can do.

When we create a class think about the objects that  will be created from that class type.
Example program for class explanation:



 
 
The below program describes the importance of objects .
 

This is the output: