Tuesday, 28 April 2020

What is class and objects in java? | What is the difference between class and object?


What is class in java?

Class is a blueprint. In Java everything is encapsulated under classes.  A class is used to bind data as well as methods together as a single unit. Class will have:
  • ·    Attributes (instance Variables)
  • ·    Methods (Functions)

the instance variables and methods are known as class members.

What is Object?

An object is the instance of the class, which helps programmers to use (access) variables and methods inside the class. Given a class you can create how much ever objects you want.
The objects (also called instances) are created using new keyword.

Syntax for creating an object of the class:

ClassName objectName = new className();
Here, we are using the constructor (className()) to create the object. Don’t get bothered about constructor. We will learn the constructor in next class.

How to access members?

Objects are used to access members of the class. We can access members of the class (methods and access instance variables) by using the . operator. We will see in the example.

Object and Class Example: main( ) within the class

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

//Java Program to illustrate how to define a class and fields 

//Defining a Student class. 
class Student{ 
 //defining members of the class
 int id; //field or data member or instance variable 
 String name; 
 //creating main method inside the Student class  
 public static void main(String args[]){ 
  //Creating an object or instance 
  Student s1=new Student();//creating an object of Student 
  //Printing values of the object 
  System.out.println(s1.id);//accessing member through object 
  System.out.println(s1.name); 
 } 
} 

Output:

0            // because we have not yet assigned any value.
null

Object and Class Example: main outside the class

In real time development, we create classes and use it from another class. It is a better approach than previous one. Let's see a simple example, where we are having main() method in another class.
We can have multiple classes in different Java files or single Java file. If you define multiple classes in a single Java source file, it is a good idea to save the file name with the class name which has main() method.

//Java Program to demonstrate having the main method in  another class 

//Creating Student class. 
class Student{ 
 int id; 
 String name; 
} 
//Creating another class TestStudent1 which contains the main method 
class TestStudent1{ 
 public static void main(String args[]){ 
  Student s1=new Student(); 
  s1.id=5;
s1.name=’’hi”;
System.out.println(s1.id); 
 System.out.println(s1.name); 
 } 
} 

Output:

5
hi


No comments:

Post a comment

All about journals and research paper | What is impact factor? | how the impact factor is calculated? | who calculate the impact factor? Scopous journals

Figure 1: One of my Research Papers When the Scholars are in their Master or PhD or in any research field. They are supposed to writ...