What is object


Object

1. Object is a real Entity word.

2. Object contains two things :

i. State

ii. Behaviour

i. State

a. State that can be change.

b. State is also called attributes or variables.

c. State is used to represent type of data or state of an Object.

For Example:

Student is an Object where roll is a state and roll type is int.

Student is an Object where name is a state and name type is String.

Student is an Object where mark is a state and marks type is float.

ii. Behaviour

a. Behaviour of an Object can never change.

b. Behaviour of an Object can represents as a method.

For Example:

Student is an Object where study or learn is a behaviour.

Pen is an Object where write is a behaviour.

AC is an Object where on,off,swing etc are the behaviour.

Object Example

    //this is a template or blueprint of an Object.
        //this class contains information about Student.
        //But you dont know the state of student thats why this type of class is called Template or Blueprint.
        class Student{
            String name;
            int roll;
        }
        //this class is used for testing.
        class Test{
            //this is an entry point from where your program is started.
            public static void main(String [] args){
                //this is an object or instance
                //this object initialize the state with default values.
                //where new is an operator or keyword which is used to create a blank or new memory.
                //where Student() is a constructor which is used to initialize the values.
                //after the complete statement it always return the same type of address where your data is allocated.
                //where std works as a refrence variable who point the address of your default values.
                Student std=new Student();
                //if you want to change default values to the actual values then you need to write below code.
                std.name="suresh singh";
                std.roll=123;
                //if you want to access data from that location then you need to write below code. 
                System.out.println(std.name);
                System.out.println(std.roll);
            }
        }
    

Output is:


No comments: