Friday, August 11, 2017

Introducing Classes and Object


Class

Class is a blue print which is containing only list of variables and method and no memory is allocated for them. A class is a group of objects that has common properties.

A class contains

      Data Member

      Method

      Constructor

      Block

      Class and Interface



Object

 Object is an instance of class.

An Object has three characteristics

      State

      Behavior

      Identity

State: Represents data of an object.

Behavior: Represents the behavior (functionality) of an object such as deposit, withdraw etc.

Identity: Object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. But, it is used internally by the JVM to identify each object uniquely.


Difference between Class and Object
















Syntax to declare a Class

class Class_Name

{

data member;

 method;

}

Syntax to create an Object

Class_name ObjectName=new Class_name();



Example

class Employee

{

 int eid; // data member (or instance variable)

 String ename; // data member (or instance variable)

eid=101;

ename=“ram";

public static void main(String args[])

{

Employee e=new Employee(); // Creating an object of class Employee

System.out.println("Employee ID: "+e.eid);

System.out.println("Name: "+e.ename);
 }
}


No comments:

Post a Comment