bscodinglab

Classe and Objects

Class:-  A class is a blueprint or template for creating objects. It defines the structure, attributes (data members), and behaviors (methods) that objects of the class will have. A class can contain fields (variables) to represent the state of objects and methods to define their behavior. Classes are used to encapsulate related data and functions into a single unit.

Properties of Java Classes

  1. Class is not a real-world entity. It is just a template or blueprint or prototype from which objects are created.
  2. Class does not occupy memory.
  3. Class is a group of variables of different data types and a group of methods.
  4. A Class in Java can contain:
    • Data member
    • Method
    • Constructor
    • Nested Class
    • Interface

Components of Java Classes

 In general, class declarations can include these components, in order: 

  1. Modifiers: A class can be public or has default access (Refer thisfor details).
  2. Class keyword:class keyword is used to create a class.
  3. Class name:The name should begin with an initial letter (capitalized by convention).
  4. Superclass(if any):The name of the class’s parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent.
  5. Interfaces(if any): A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface.
  6. Body:The class body is surrounded by braces, { }.

 In Java, you define a class using the class keyword followed by the class name. For example:

Syntax :-

public class  Class-Name

      {

            //Write  Class members (fields and methods) here.

       }

Example :-

public class  Age

  {

            // Fields or instance variables

            int Age=30;

            // Methods

            void  MyMathod()

             {

                        System.out.println(“Hello, my Age is ” + Age);

              }

     }

 Object:

  • An object is an instance of a class. It is a concrete, real-world entity created from the class blueprint.
  • Objects are created based on a class definition, and they can be used to access and manipulate the data and behavior defined in the class.
  • Each object created from a class has its own set of instance variables, distinct from other objects of the same class.
  • You can create objects from a class using the new keyword, which allocates memory for the object and initializes it.

There are 3 ways to initialize object in Java.

  1. By reference variable
  2. By method
  3. By constructor

 1. Using by Reference Variable :-

  • Declare a reference variable of the appropriate class type. Replace ClassName with the name of the class of the object you want to create, and variableName with the name you want to give to your reference variable.

            ClassName variableName;

  • Create a new object using the new keyword and assign it to the reference variable.

            variableName = new ClassName();

Example :-

public class MyClass

 {

       // Fields and methods of the class

 }

public class Main

 {

       public static void main(String[] args)

            {

                 MyClass myObject; // Declare a reference variable of type MyClass

                 myObject = new MyClass(); // Create an instance of MyClass and assign it to the reference variable

            }

 }

 2. Using Factory Methods:- In some cases, you may use factory methods to create and initialize objects. Factory methods are static methods within a class that return an instance of that class. These methods can provide more flexibility in creating objects and may include custom initialization logic. Objects can access the fields and methods defined in their class.

Here’s an example:

public class MyClass

{

  private int value;

  // Factory method

  public static MyClass createInstance(int value)

      {

                 MyClass obj = new MyClass();

                obj.value = value;

                return obj;

      }

   public static void main(String[] args)

       {

 MyClass obj = MyClass.createInstance(42); // Object initialized using a factory method

       }

}

 3. Using Constructors:- The most common way to initialize an object is by using constructors. Constructors are special methods within a class that are called when you create a new instance of that class using the new keyword. You can define your own constructors in your classes to initialize the object with specific values.

 Here’s an example:-

public class MyClass

 {

                 private int value;

                 // Constructor

                public MyClass(int value)

                 {

                                this.value = value;

                }

public static void main(String[] args)

{

    MyClass obj = new MyClass(42); // Initializing with a constructor

}

 }

 

Syntax :-

            Class_Name  Object_Name = New Class_Name(); //

Example :-

public class Main

 {

    void  myMathod()

             {

                        String Name;

                        Int Age;

            }

    public static void main(String[] args)

  {

        // Create objects

        Main person1 = new Main();  // persion1 is objects.

        Main person2 = new Main();  // persion1 is objects.

         // Set data for objects

        person1.name = “Alice”;

        person1.age = 30;

        person2.name = “Bob”;

        person2.age = 25;

        // Calling methods on objects

        person1. myMathod();

        person2. myMathod();

    }

    }