Classes and Objects in Java

Classes and Objects in Java

Class

  • Class is the assortment of comparative kind of objects which have some regular properties.
  • Class is an idea which is executed by the object.
  • Class is an outline of the java program from which we make an object, at that point object, is an example of the class.
  • We can likewise say that class is an assortment of factors and techniques.
Class
{
         Methods: Represent Property
Variables: Represent
Behavior
}

Object

  • Any certifiable element which has physical presence either living or non-living is called an object.
  • It is a runtime memory region. The object goes to load.
Classes and Objects in Java

How to Create an Object?

  • In Java object of any class is made utilizing new catchphrase.
  • new catchphrase holds the memory for an object in-store and creates a verifiable reference id which is put away in a reference variable.
class Emp
{
                String name;
                int salary;
                void get(String n1,int s1)
                {
                                name=n1;
                                salary=s1;
                }
                void show()
                {
                                System.out.println(name);
                                System.out.println(salary);
                }
                public static void
main(String…s)
                {
                                Emp e1=new
Emp();
                                e1.get(“aa”,101);
                                e1.show();
                                new
Emp().get(“bb”,102);            //Anonymous
Object
                                new
Emp().show();                         //Anonymous
Object
                }
}

Note: Reference factors and neighbourhood factors go to stack and strategies goes to technique zone.

Classes and Objects in Java
Classes and Objects in Java

In the above model all out three objects are made. Right off the bat, an object is made utilizing new administrator and its reference id is put away in reference variable e1. At that point utilizing e1 we call get() and appear() techniques. After that, we are making two mysterious objects. You can see that we are getting unique yields in three objects.

  • An unknown object is an object without the name.
  • We can store one reference id into more than one reference variable.

Example:

Emp e1=new Emp();
Emp e2=e1;

Leave a Comment

error: Alert: Content is protected!!