Everything in the real world can be called as an Object. A Student is an Object, a Car, an Employee or a Guitar is a real world Object.

Can you point out any one real world thing which cannot be called as an Object !!!!?

What about human emotions !? or speed !!? Can we call them as Objects ?

No right 🙂

If something can not be called as an Object then they will be either a property or a behaviour an Object. For example emotions are the behaviour of a Human and speed is a property of a Car.

Java developers has given us a facility to represent real world objects in a program i.e a Class.

So Let’s define a Class

What is a Class ?

A class is a blue print of an Object which describes the properties and behaviours of an Object.

A class is a description of an Object.

A class can be called as a Template, Prototype of an Object.

A class is a user defined data type.

Let’s represent an real world object Car in Java 🙂

Example !!!

Notes Continued:

We just described a real world object in Java program but it’s merely a Blueprint not the Object itself !!
We need to instantiate this blueprint. We can create N number of instances of a Class. In Java we use new keyword to create an instance.


Instantiating a Class :

What is an Object ? What is an Instance ?

An object is an Instance of class. When we say instance is created it means that memory is allocated for all the members of a class and then the data members  gets Initialized.

See below two videos to understand 🙂

 

Object Creation Process :

In the below example I created 3 Instances of class Car in another class called CarTest and accessed the data members as well as member methods of the class car through the respective objects using .(dot) operator.

Example !!!

Object Creation Process :

Example !!!

Instantiation :

In this phase memory get allocated to all the members of a class.

Initialization :

In this phase that data members are initialized. What exactly they get initialized with ?  As we seen in previous example speed got 0 but to understand why this is happening like this we need to understand the concept called constructor. In the above diagram we seen a method called Car() has been called which we never created in the class Car isn’t it ? Don’t you think we should get compile time error but we don’t, the reason is because it basically a constructor. It’s a default constructor.


Constructor :

video

Notes:

Creating an Object can be interpreted as constructing an object. To construct the object every class should have a Constructor.

Constructor :

A Constructor is a special method whose name is exactly same as a class name but doesn’t contain any return type, not even void. The purpose of constructor is to initialize the data members.

Default Constructor :

Because it is mandatory that there should be a constructor to construct an object it doesn’t mean that you should create one, compiler itself builds one for you which can be called as default constructor.  default constructor is an implicit no-arg constructor which initializes the data members with default values i.e 0 from numeric types, false for boolean, ‘\u0000’ for char type and null for user defined data type.

Let’s first create a class Employee and instantiate like we did with Car

Example :

class Employee {

	int id;
	String name;
	double salary;


	public void doSomething(){
		System.out.println("Employee is doing something");
	}

}
class EmployeeTest {

	public static void main(String[] felight) {

		Employee emp1 = new Employee();
		emp1.id = 1001;
		emp1.name = "Captain Jack Sparrow";
		emp1.salary = 606060;

		System.out.println(emp1.id);
		System.out.println(emp1.name);
		System.out.println(emp1.salary);
		
	}
}

Lets create another instance of class Employee to understand the default constructor with this example

EmployeeTest V2:

class EmployeeTest {

	public static void main(String[] felight) {


		Employee emp2 = new Employee();
				// ^ here Employee is instantiated using
		/*default constructor.
		In Employee class there is no explicit constructor is defined
		so we can say that default constructor is in action which
		initializes that data member with default value
		so when we print the properties of emp2 then 
		we get the output as
		0 for id, null for name & 0.0 for salary
		*/
		
		System.out.println(emp2.id);
		System.out.println(emp2.name);
		System.out.println(emp2.salary);
		
	}
}

As we can see in the above example, the default constructor is in action.

When does the compiler generate default constructor and when don’t ?

Compiler generates the default constructor for us only when there is no explicit constructor is defined. For example when you compile the below program

class Employee {

}

Compiler first regenerates the program as below:

class Employee {

        Employee(){
           super(); // We will learn about super() later
        }
}

default constructor gets the same access modifier(we will study this chapter later) as the class.

For example for the below code

public class Employee {

       
}

compiler generate the default constructor as below:

public class Employee {

        public Employee(){
           super(); // We will learn about super() later
        }
}

What if we are not happy with the default values assigned by default constructor !!

Suppose if we wish to assign our own ( User Defined) default values to data members then we should create a  constructor explicitly.

Example:

Employee class v2

Class & Objects Interview QuestionsWhat is the use of this keyword in Java ?

  1. What is the difference between Object and Object reference ?
  2. What is the difference between an instance field / instance variable / field variable and local variable, an input parameter, an argument, a class field or static field or class variable ?
  3. What is the difference between instance methods and static methods ?
  4. Why return type is not allowed for Constructor ?
  5. Why constructor name should be same name as class name ?
  6. What is the restriction of method overloading ?
  7. What is the difference between of method overloading and method overriding and runtime polymorphism ?
  8. What is Operator overloading and why java do not support Operator overloading ?
  9. What are ways we can create objects in Java other than new keyword ?
  10. What is read only variable ? and what are the ways to assign values to it ?
  11. What is task of new keyword how it works ( behind the scenes ) ?
  12. What would happen if you say this = null ?
  13. What is biggest advantage and disadvantage of Java ?
  14. How many times of memory areas are allocated by JVM ? What gets stored where ?
  15. Is empty.java is a valid filename ?
  16. What is the purpose of Runtime class ? ( move to memory chapter )
  17. What is constructor chaining ?
  18. What is difference between CLASS_PATH and PATH variable ? ( wait till advanced Java )
  19. Can an unreachable object can be reachable again ?
  20. Is the anything called constructor overloading ?
  21. Why we are not using destructor in Java ?
  22. Explain what null means in Java, why this special value is necessary ?
  23. What is formal and actual parameters ?
  24. Why main method is static ? Can we have overloaded main method or can we have more than one main method in same class ?
  25. What is difference between abstraction and encapsulation ?
  26. Can constructor perform other tasks other than initialization ?
  27. What is copy constructor ?
  28. What is polymorphism ?