what is class in java

what is class in java

what is class in java

Hello friends! In today’s post, we will read in detail about Class and Objects in Java (What is Class and Object in Java?). You read this completely. You will understand this easily. So let’s start:-

Class and Object in Java

Java is an object-oriented programming language. In Java, the program is designed using classes and objects. Object is a physical entity as well as a logical entity whereas class is only a logical entity.

For example:- In real life, Car is an object. Car has attributes like:- color and size. And it has methods such as drive and brake.

So let us now know about them in detail. First of all we will read about class.

Class in Java

Class is a prototype defined by a user from which objects are created.

In other words, a class is a group of objects that have similar properties. This is a blueprint from which objects are created. It is a logical entity. It can’t be physical.”

java class rules

Generally the following things are followed while declaring a class in java.

Modifiers – A class can have only public or default access.

Class Keyword – The class keyword is used to create a class.

Class name – Its name should start with a capital letter.

Superclass (parent class) – A class can extend only one parent class. The extends keyword is used for this.

Body – The body of the class is inside the curlybraces {}. There are variables and methods inside curlybraces.

A class can contain fields, methods, constructors and blocks.

Java class syntax –

class ClassName {

  // fields

    // methods

    }

A simple example of a class –

Suppose Student is a class and student’s name, roll number, and age are its fields and info() is a method.

class Student

{

 String name;

  int rollno;

   int age;

   void info(){

    // some code

    }

    }

Object in Java

An Object is an instance of a class. The object represents the class.

In other words, “An entity that has state and behavior is called an object. For example- car, pen, table, chair, bike and mobile etc. It can be physical or logical.”

An object has the following characteristics:-

State – It is represented by the attributes of the object. It also presents the properties of the object.

Behavior – It is presented by the methods of the object.

Identity – It gives a unique name to the object and enables an object to interact with other objects.

In the real world, mobile phone is an object and its properties like color, cost, and name etc., while its behavior is:- calling, chatting etc. So therefore we can say that object is a real world entity.

Definition of Object

•An object is a real world entity.

•An object is a run time entity.

•Object is an entity which has state and behaviour.

•An object is an instance of a class.

creating an object

As I told you earlier, an object is created from a class. In Java, the new keyword is used to create objects.

There are following three steps to create an object from a class:-

Declaration – First of all the variable is declared.

Instantiation – The new keyword is used to create the object.

Initialization – Finally, new is followed by the class name and parenthesis. Which means- calling the class’s constructor to initialize the object.

Java object syntax –

className variable_name = new className();

Example – 

Creating Class and Object

Below you have been given an example of creating an object and a class.

public class Student{      

String name;

int rollno;

int age;

void info(){

System.out.println("Name: "+name);

System.out.println("Roll Number: "+rollno);

System.out.println("Age: "+age);

 }  

public static void main(String[] args) {

Student std = new Student();

// Accessing and property value

std.name = "Sohail";

std.rollno = 33;

std.age = 24;

// Calling method

std.info();

}

}


Output:-

Name: Sohail

Roll number: 33

Age: 24

In the above example, we have created a class named Student. There are three variables inside this student class – name, rollno, and age. And it has an info() method. Here we have used the main() method to create the objects of the Student class.

In how many ways objects can be created in Java?

Objects in Java can be created in the following ways:-

•new keyword

•newInstance() method

•clone() method

•Deserialization

•factory method

Anonymous object

Anonymous means – one who does not have a name. The object which does not have any reference is called anonymous object.

•They are used for immediate method calling.

•These get destroyed after calling the method.

•They are used in many libraries such as – AWT library.

example :-

class Calculation{  

void fact(int n){  

int fact=1;  

for(int i=1;i<=n;i++){  

fact=fact*i;  

}  

System.out.println("factorial is "+fact);  

}  

public static void main(String args[]){  

new Calculation().fact(5);  

}  

}


Output:-

factorial is 120.


Request:- If this post has been useful for you, then definitely share it with your friends and classmates and if you have any question related to java, then tell through comment below. Thank you.

Previous articleoops concepts in c++ in hindi
Next articledata manipulation in python examples

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here