# Class and Object Basics - Class: It is a blueprint. Example: Car. - Object : It is real life instance of a class. Example : Honda city is a real life object of class Car. Note: We can create multiples objects of a class ### Class and Objects creation : A Class has: 1. Attributes / Parameter : This is information that we store. 2. Functions / Methods : Action items we perform. Syntax for a class: Code1 : ```java class Class_Name{ // Code } ``` Explanation : ``` A class with name Class_Name is created ``` Code2 : ```java class Car{ // attributes String modelName; String owner; int regNumber; // methods void drive() { System.out.println("Groom groom"); } void accelerate(){ System.out.println("Car is speeding up"); } ``` Explanation : ``` -> In above code we are creating a class with name Car. -> Attributes: modelName, owner and regNumber -> Methods : drive and accelerate. ``` Code for object : Code1 : ```java class Main { public static void main(String[] args) { Car c1 = new Car(); c1.modelName = "Ciaz"; c1.owner = "Rahul Janghu"; c1.regNumber = 3456; System.out.println(c1.owner); } ``` Output : ``` Rahul Janghu ``` Explanation : ``` Here we initialized the attribute values. Owner name is : Rahul Janghu ``` Calling methods using an object : Code2 : ```java class Main { public static void main(String[] args) { Car c1 = new Car(); c1.drive(); } } ``` Output : ``` Groom groom ``` Explanation : ``` Here drive method is being called using c1 object. ``` Note : We can pass arguments inside our methods as well. ### Constructor : > Previously we had to initialize our attributes one by one after creating an object from the class. > Using constructor we can initialize our attributes directly at the time of new object creation itself. And these attributes are attached with the current object itself. Rules : 1. Constructor name should be same as ClassName. 2. It is similar to functions but without return type. 3. Constructor is invoked by default at time of object creation. 4. If we haven't created any constructor then it will create a by default constructor without any arguments. 5. If we create a new constructor then Java will not create any by default constructor. Code : ```java class Main { public static void main(String[] args) { Car c1 = new Car("Honda", "Rahul Janghu"); System.out.println(c1.owner); } } class Car{ // declaration of attributes String modelName; String owner; int regNumber; Car(String a, String b) { modelName = a; owner = b; } // methods void drive() { System.out.println("Groom groom"); } void accelerate(){ System.out.println("Car is speeding up"); } } ``` Output : ``` Rahul Janghu ``` Explanation : ``` Here we are using a constructor which is helping us to initiate modelName and owner at the time of object creation. We initialized the owner and modelName with object creation. Hence it printed owner name. ```