Program based on Object and Class with Explanation.

Class - class is use to create real time or user defined data type  or class is a collection of objects which have common properties . If we define syntactically, class is a collection of  member data and member method. 
class implements encapsulation  and abstraction. 


Declaration of class:

A class is declared using "class" keyword. A class contains:
methods,
methods,
constructor and
destructor.

but  java does not  have destructor like c, c++. 

A class is declared according to rules of data structure. A data structure contains two section as:

  1. Data :The data members declared inside the class behave as data of data structure.
  2. Operation on Data: The methods, constructor behave as operation on data of data structure.


Syntax of class:

class ClassName
{
Data members;
Member method;
constructors;
}

Objects:
Objects are instance of class. Store data and invoke methods.
When we allocate memory  of class type variable  is term as object and follow all oops rules.
example: suppose i am declaring a variable of int type
like this:
int x;
here x is a variable and its is of int type.
same if I  declare a variable of any class, its called object. like:
String s1;
here s1 is a reference  variable of String class. 
String s2 = new String("prabhakar");
here s2 is an object of String class.
Same we can also declare a reference variable or object of user define class. for example:
Person obj;
here 
Person is the user define class name and obj is the object of Person class.
Create Object:
ClassName obj;
// object declaration does not allocate memory.
Therefore “new” keyword is used to allocate memory for an object.
Syntax:
obj= new ClassName(arguments for constructor if any);
The use of “new” keyword is termed as instantiation or initialization or memory allocation.

The new keyword allocates new/separate memory according to the class type and binds all the members of class type into the memory. The memory is also termed as dynamic object. "new" keyword also returns the ADDRESS of the memory allocated (Dynamic Object).
In this way the object is a pointer variable that point to the dynamic object[created in hardware].
The object of the class type encapsulates [binds] all the members of the class in a separate memory termed as dynamic object. The object accesses its members by DOT (.) operator and follows its visibility mode rules.

example of class object:
class Person{
     //data member (also instance variable)
int age;    
   //data member(also instance variable)          
String name;                   
public static void main(String args[]){
     //declaration of an object of Person class
Person s1;        
 /* memory allocation via "new" keyword. here new keyword will create memory in hardware called dynamic memory and new keyword also will return the address to s1 object. now s1 is a pointer variable witch point the dynamic memory.*/              
s1 = new Person();  
 //0 [default value of int]                          
System.out.println(s1.age);   
     
 //null [default value of string]
System.out.println(s1.name);                  
}
}
output:
0
null
here
0 [default value of int ]
null [default value of String ]

Another Example of Object, class, methods and variables:
class PersonInfo
{
int age;
String firstName;
String lastName;
float salary;
void setRecords()
{
age = 24;
firstName = "Prabhakar";           //double qoutes[" "] literal for String.
lastName = "Jha";                      //double qoutes[" "] literal for String.
salary = 60000f;                       // f is a float literal
}
void printRecords()
{
System.out.println("Information: ") ;                         // wirte msg in double qoutes
System.out.println(firstName);                 /* not need to write variable name in double qoutes, now it will print value of variable*/
System.out.println(lastName);
System.out.println("Age is: "+age); /* you can print msg and value both. here + operator concatenate msg and variable*/
System.out.println("My salary is "+salary);
}
public static void main(String args[])
{
PersonInfo obj= new PersonInfo();             // object creation and memory allocation.
obj.setRecords();                 // object is calling methods by dot operator.
obj.printRecords();              // it will call the printRecord() method and print all data.
} //end of main method
} //end of class

output:

Information:
Prabhakar
Jha
Age is: 24
My salary is 60000.0



If any doubts you are free to ask in comment.
SHARE

About prabhakar jha

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment