Hello lets we are going to learn about classes and objects its the very important topic in JAVA.We know that we cannot create a program without a class in java because JAVA is purely object oriented.
Object is an instance of a class.we can access the function and members of a class only by using the objects.
CLASS:
A class is a blue print from which individual objects are created.
Syntax for class declaration:
~~~~~~~~~~~~~~~~~~~~
class class name //class declaration
{
method1(parameter list ])
{
Statements;
}
method2(parameter list ])
{
Statements;
}
}
class ->keyword
class name ->user defined name
Note::filename should be same as that of class
A class can contain any of the following variable types.
- Local variables . variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.
- Instance variables . Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.
- Class variables . Class variables are variables declared with in a class, outside any method, with the static keyword.
Creating an Object:
As mentioned previously a class provides the blueprints for objects. So basically an object is created from a class. In java the new key word is used to create new objects.
There are three steps when creating an object from a class:
- Declaration . A variable declaration with a variable name with an object type.
- Instantiation . The 'new' key word is used to create the object.
- Initialization . The 'new' keyword is followed by a call to a constructor. This call initializes the new object.
Example of creating an object is given below:
public class Puppy{ public Puppy(String name){ // This constructor has one parameter, name. System.out.println("Passed Name is :" + name ); } public static void main(String []args){ // Following statement would create an object myPuppy Puppy myPuppy = new Puppy( "tommy" ); } }
If we compile and run the above program then it would produce following result:
Passed Name is :tommy
Accessing Instance Variables and Methods:
Instance variables and methods are accessed via created objects. To access an instance variable the fully qualified path should be as follows:
/* First create an object */ ObjectReference = new Constructor(); /* Now call a variable as follows */ ObjectReference.variableName; /* Now you can call a class method as follows */ ObjectReference.MethodName();
Example:
This example explains how to access instance variables and methods of a class:
public class Puppy{ int puppyAge; public Puppy(String name){ // This constructor has one parameter, name. System.out.println("Passed Name is :" + name ); } public void setAge( int age ){ puppyAge = age; } public int getAge( ){ System.out.println("Puppy's age is :" + puppyAge ); return puppyAge; } public static void main(String []args){ /* Object creation */ Puppy myPuppy = new Puppy( "tommy" ); /* Call class method to set puppy's age */ myPuppy.setAge( 2 ); /* Call another class method to get puppy's age */ myPuppy.getAge( ); /* You can access instance variable as follows as well */ System.out.println("Variable Value :" + myPuppy.puppyAge ); } }
If we compile and run the above program then it would produce following result:
Passed Name is :tommy Puppy's age is :2 Variable Value :2
PROGRAMS:
~~~~~~~~~~
class room
{
double width;
double length;
double area( )
{
return(width*length);
}
}
class room1
{
public static void main(String a[ ])
{
room r=new room( );
room r1=new room( )
r.width=10; //assigning values to the variables
r.length=20;
r1.width=15; //assigning the values to the variables
r1.length=25;
result=r.area( );
System.out.println("AREA IS: "+result);// calling method from another class using object
System.out.println(:AREA IS: "+r1.area( ));
}
}
OUTPUT:
````````````
AREA IS:200 //by first object
AREA IS:375 //by second object
````````````
AREA IS:200 //by first object
AREA IS:375 //by second object
No comments:
Post a Comment