Friday, February 26, 2016

Java - Program Structure ( Class, Object and Method) for beginners



Java is an Object Oriented (OO) programming language.
That is
Software objects/entities like Employee, Product, Customer etc are 
conceptualized as objects in Java.
To understand that, let us see how we deal with objects in our life.
In our daily life
We identify objects by their attributes/properties and working methods
 / behavior.
A fridge is identified by its features(attributes) and how it is used (methods).
So are animals, their features( 4 legs, tail etc) and 
how they behave (barks, mews or runs etc ).
Similarly
If we take a software object like product’ ,
 its name, price can be its Attributes and 
formula for calculating depreciation/discount can be its Methods. 
Based on these , a product object is conceptualized.
Important point is,
An object is combination of  Attributes and Methods.
They are not separate but stated together. 
Let us take a simple example of the geometrical figure Rectangle.
Length and Breath are its attributes.
formula to calculate Area is a Method. 
We can have  many methods depending on the context.
When we say Rectangle Object, it includes length,breath and

the formula for area.

We can write it this way,
Rectangle
Attributes
Length (Integer Type)
Breath (Integer Type)
Method
Area (Integer Type)        = Length*Breath

Now,

When  the above object structure is translated into Java, 

it is called as Class.

Because it is only a structural definition of an object, 

not an object itself in Java.

Using this class as template, real objects are created in Java.

The basic difference between Java Class and Object  is,

objects are allocated memory during run time and they carry data.

There could be many  Rectangle Objects with different length and breath

but there is only one Rectangle Class based on which they are created.
So, in Java, Class of Rectangle have the following details;
Class Rectangle
Attributes
Length (Integer Type)
Breath (Integer Type)
Method
Area (Integer Type)        = Length*Breath
If we use the Java language syntax it will be like this
   class Rectangle{ 
        int breath ;
        int length;   
    void circumference (){       
        int area = breath*length;
                       }
               }
Entire program would have more statements and information  as below.

Though this post is only about concepts and not about Java coding,

explanation for each statement is given as comments in the following

example for understanding. 

Program:


package clsobjmthd; // package name

/* Coding of the Rectangle Class */
 class Rectangle{ 
             int breath =10;
             int length = 15;
   
     void FindArea (){ //method is void because it does not return value
       
        int area = breath*length;
        System.out.println("Area : " + area); // print statement to display result
     
    }

/* End of Rectangle Class */

public class ClsObjMthd { 

    public static void main(String[] args) { // Java runs the Main first
       
        Rectangle RectObj; // Creating a object using the class

        RectObj = new Rectangle(); // Creating an instance -allocates memory

        RectObj.FindArea(); // calling the method,

                                        //Method is qualified by the object to which it 
                                          belongs by '.'
       
    } 


      
Output:

run:

Area : 150

Initially for a beginner, the structure may appear bit odd or confusing 

but one will be able  appreciate the OO structure as one gain experience.

Bottom line is, in Java, one has to think in terms of Class, Object

and Method.

Let us look at this statement as example 
A customer talks about discount on a product with a salesman.
Here, we can identify objects and methods.
Objects - Customer, Salesman, Product
Method - Discount Procedure and it could be associated with Product.
Then, we can move into detail and club attributes and methods of an object .
By working on various examples, one can get hold on this concept.

Rest is, how we translate it into Java language.




No comments:

Post a Comment