What is a class and object in Java

what is a class and object in java

Java is a very popular programming language and widely used in android development, web app field. When you are going to start learning java the first term you read is class and object. What is a class and object in java? Why java has class and object and C does not have these. So, we learn class and object in this post.

Class in Java

Basic Definition:

Class is a blueprint of an object. It is the basic concept of Object-oriented programming. The class represents the set of properties, the method defined by the user. Secret: When you write a class you are describing how the JVM should make an object of that type. Do you know, Every java application needs at least one application and one main method

Creating Class in Java

public class GeeksToCode
{  
  public int number;
  public String name;
  
  void GeeksToCode(int num,String name)
   {
      this.number = num;
      this.name = name;
   }
   public int getNumber()
    {
     return number;
    }
}

The above code represents a class and it consists of many components which we are going to discuss now, First, we discuss the outer body of the class.

Components of class

1. Access Modifier: while creating a class, method, variable you need to define the scope of it such as public, private, protected

2. Class Name: The class name must start with a capital letter. Ex- GeeksToCode

3. Class Body: Class body starts with “{” and ends with “}”. Class does not require “;” at the end

Access Modifier class ClassName
{
//Body
}
Example:
public class GeeksToCode
{

}

Now we discuss some inner components of the class.

Variables: This is used to store information

Constructor: These are used to initialize the variable at the time of creating the object.

Method: These are collections of statements that perform some specific task.

Access Modifier class ClassName
{
Variable 1;
Variable 2;
void ClassName()
{
}
Methods();
}

Example:

public class GeeksToCode
{  
  public int number;   // variables
  public String name;
  
  void GeeksToCode(int num,String name) // constructor
   {
      this.number = num;
      this.name = name;
   }
   public int getNumber()  // methods
    {
     return number;// task in methods
    }
}

Objects in Java

In very simple words, Objects are an instance of the class. Whenever you create a class you need an object to use class properties. So, to create an object what do you need? Yes, you need 2 classes to create an object. How First class is whose object you want to create and the second class is where you create your object.

public class GeeksToCode         // class whose object you want to create
{  
  public int number;   // variables
  public String name;
  
  void GeeksToCode(int num,String name) // constructor
   {
      this.number = num;
      this.name = name;
   }
   public int getNumber()  // methods
    {
     return number;// task in methods
    }
}
public class Main{ // class where you create object
   public static void main(String args[])
   {
    GeeksToCode object = new GeeksToCode(1,"Coding");  // New Object Created
   }
}

Component of Object

State: Every object has a state which represents a variable of the class whose object is created.

Behavior: Methods of the object represent the behavior.

public class GeeksToCode         // class whose object you want to create
{  
  public int number;   // variables
  public String name;
  
  void GeeksToCode(int num,String name) // constructor
   {
      this.number = num;
      this.name = name;
   }
   public int getNumber()  // methods
    {
     return number;// task in methods
    }
    public int getName()  // methods
    {
     return name;// task in methods
    }
}
public class Main{ // class where you create object
   public static void main(String args[])
   {
    GeeksToCode object = new GeeksToCode(1,"Coding");  // New Object Created
    System.out.println(object.getNumber());  // print 1
    System.out.println(object.getName());   //print Coding
   }
}
Tagged : /

Leave a Reply

Your email address will not be published. Required fields are marked *