This article can introduce  the idea of class and object to the newbies.If you’re not aware of class then initial question can arise in your mind that what’s class!The class is at core of java.It defines the form and nature of object.We can say it’s one kind of data type through that we are able to create the object and might use the features of object oriented programming language!




Abstract read at class



When you defining a class,You actually defining new data type through that the object can be created for additional requirements.New class can be defined by the word “class” .Inside the class you’ll be able to define different variables and method having property of accessibility.





In java when we aren’t specifying access property it’s by default as public.So that outside that class ,object can access class property easily.

Syntax for defining category

class class_name

You can say that class is the combination of methods and instance variable.If you wish to define a class named “box” then you’ll be able to define it like this

class box

Inside it you’ll be able to simply defines its variable and methods.

Sample of class

class box{
int width;
int depth;
int height;
}

Closer look at objects



As we’ve got mentioned that once you create a class it usually creating one data type  through which the object of specific data type are often defined.In easy words objects are instance of class ,Lets have a glance syntax to declare objects

Syntax to declare objects


Class_name object_name=new class_name();

This is the direct way through that you’ll be able to simply create object using new operator.Suppose we’ve got class named box that we’ve got used above.Then we are able to create object like this

box obj=new box();

To access the variable named width  of box class then we can do it by 

obj.width;

Consider that when you are creating a class then you’re assigning all the property to that specific object’s memory location.It may be interesting when you will execute your first java program having oop concept of class-object.


Problem–Execute a java program having class-object idea



Code– 

class box{
int width;
int height;
int depth;
}
class boxdemo{
public static void main(String args[])
{
box obj=new box();
obj.width=10;
obj.height=20;
obj.depth=30;
int volume;
volume=(obj.width*obj.height*obj.depth);
System.out.println(“Volume of box=  “+volume);
}
}

Output

Volume of box=6000

We have simply accessed all variable of box category and then calculated volume of that box using object.If you’ve got any question relating to this subject then contact us or mention it in comment box!

By jigar

Leave a Reply

Your email address will not be published. Required fields are marked *