Tuesday, 19 July 2016

Class and object in Java with example program

java classes and objects are the basics building blocks they provide a way how to implement an object oriented program. Here in this page you can learn how to create java class and object with an example programs. You can also see how to declare class members and how to access them.

Object in Java

      An object is a real world entity that has state and behavior. All the things what your are seeing our daily life like car, pen, chair all are objects. In object oriented terminology, on object is a collection of fields and data members. The fields stores data and methods performs some actions / task.
 An object is a blue print of a class i.e., what is available in class all are available in object also.

Class in Java

                A class is a collection of object of same type i.e., which have common properties. It is a template used to create objects. We can also create new class from the already existing class. The General syntax of a class is

class specifier class ClassName extends SuperclassName
{
instance variable; 
instance methods;
constructors;
blocks;
  

In the above code the class is a keyword used to create a class and extends is another keyword used in the concept of inheritance. The class body contains variables, methods m constructors, blocks.

Instance Variable

                           The Variables which are declared inside a class but outside of the method are known as instance variables.  Memory for instance variable are not allocated at compile time , it will allocate at run time when the objects (instances) are created to that class. So, the variable defined inside the class are known as instance variable.

For example:


Class Student
{
int sId;
String sname[30];
String course[];
}
In the above code sId, sname, course are instance variables.

Instance Methods.

                            Generally variable present inside a class are used by the methods in that class. The methods which uses the instance variable of a class are known as instance methods or the method which are invoked with object are known as instance methods.

Accessing class member of a class.

                                             As mentioned earlier, a class contain variables, methods, constructors, and blocks. To access all the class members first you need to create an object to that class using the following syntax.

ClassName objectName = new ClassName();

Example: Student  S1= new Student();

The you can access class members as

ClassName.VariableName=value;
ClassName.MethodName( Parameter list);

Here is a example program how to access class member


    class Student{  
     int id;//data member (also instance variable)  
     String name;//data member(also instance variable)  
      
     public static void main(String args[]){  
      Student s1=new Student();//creating an object of Student
      s1.id=500;
      s1.name="Hari";  
      System.out.println(s1.id);  
      System.out.println(s1.name);  
     }  
    }  

The above program gives out out as 500 and hari respectively.

0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top