Wednesday, January 6, 2016

What is class And Object in Java with examples

It is very Important for Beginners or Learners who want to know about java"class in java".
Since who knows little bit java they are familiar with java is purely object oriented programming language that means everything we do things in java as object.So you should have awareness of java class before entered into java world.Because while we designing or develop program in the java project everything  writes by using class and object only.In this article we will learn about java objects and  class.

class in java:
class is a model or template for creating object.That means the properties and actions of the objects are written in the class. Here properties are represented by variables and actions of the objects are represented by methods. The same variables and methods also available in the object because  they are created from class.

Definition: 
class is group of  common properties that are variables and methods and does not exist physically. It is also called blueprint for creating object.

Syntax:

class <classname>
{
data member;
method;
}

we can create class in java by using keyword called as class and fallowed by name of the class.Then class body start with curly braces {} in between these braces all the things related to class such as variables and methods will come here.

Ex:
class person
{
//properties of a class person- variables
String name;
int age;
//actions done by person-methods
void speak()
{
System.out.println("man can speak");
}
void eat()
{
}
}

How can we access the Class:
There two ways we can access class in java. They are

public: in any package we can accessible  class objects code.

Default: class object code are accessible only with in the package

Object in java 
An object is nothing but any thing that really exist in the world. It is an entity contains state and behavior is known as object. 

Examples: a car, a dog, a cat, a pen,a ball etc..

State means data of object and behavior means functionality of an object

Every object has variables and methods can perform actions. Let us take a person whose name is "lucky".Lucky is an object because he exists physically but person is a class does not exists physically.



Difference between class and object in java:

A class is a model for crating object and does not exist physically. An object is any thing that exists physically.But both the class and objects contains variables and methods.



Simple Example on class and object:

Ex:

class Person{
String name;//variable (instance variable)
int age;//variable (instance variable)

public static void main(String args[])
{
Person p=new Person();//here we are creating object for person class using new operator
System.out.println(p.name);
System.out.println(p.age);
}
}

Result:
null 0;

Here new keyword in java is used to allocate memory at run time.


Check the post : How to Set Path in Java









No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...