CSDT BLOG

DISCOVER COLLECTIONS AND BLOGS THAT MATCH YOUR INTERESTS.




Share ⇓




Class and Object in Java Language.

Bookmark

Class and Object in Java Language.


Class and Object 

Object: - In object-oriented programming technique, we design a program using objects and classes. Object is the physical as well as logical entity whereas class is the logical entity only. Object is a real world entity which have attributes (states) and behaviours (functionality or Methods). 

Example: - chair, bike, marker, pen, table, car etc. It can be physical or logical (tangible and intangible). The example of intangible object is banking system. 

An object has three characteristics: 

State: represents data (value) of an object.
Behaviour: represents the behaviour (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.             

Example: Pen is an object. Its name is Celio, color is white etc. known as its state or attributes. It is used to 
write, so writing is its behaviour.

Object is an instance of a class. 

Class is a template or blueprint from which objects are created. So object
is the instance (result) of a class.

Class: - A class is a group of objects that has common properties or it is set off attributes and behaviours. A class can contain: Data member or attributes or states of objects or property or fields and Method or function or functionality or behaviours. 

Syntax to declare a class: 
class class_name
 { data members; 
methods; 
Simple Example of Object and Class

In this example, we have created a Student class that have two data members id and name. We are 
creating the object of the Student class by new keyword and printing the objects value. 

package student; //user define Package
import java.util.Scanner; //Predefine Package
class Student
int id; //data member (also instance variable) 
String name; //data member(also instance variable) 

public void getInfo() //Method Define
{
//Taking input from user through Scanner Predefine class 
Scanner sc=new Scanner(System.in); 
System.out.println(“Enter Id”);
Id=sc.nextInt();
System.out.println(“Enter Name”);
name=sc.nextLine();
}
public void display() //Method
{
System.out.println(“Id is”+id);
System.out.println(“Name is”+name);
public static void main(String args[])
Student s1=new Student(); //creating an object of Student 
s1.getInfo(); //Method Calling 
s1.display(); //Method Calling 

Note: - Instance variable in Java: - A variable that is created inside the class but outside the method, is known as instance variable. Instance variable doesn't get memory at compile time. It gets memory at runtime when object (instance) is created. That is why, it is known as instance variable. 
Note: - new keyword :- The new keyword is used to allocate memory at runtime. 

0

Our Recent Coment