OOP - Classes and Objects, and Constructors
Types of constructors
- Default Constructor - Parameterized Constructor
Rules to remember while creating a constructor
1. Constructor name must be the same as its class name 2. A Constructor must have no explicit return type 3. A Java constructor cannot be abstract, static, final, and synchronized
Class
A class are templates that are used to create objects, and to define object data types and methods. Class doesn't consume any space. A class can also be defined as a blueprint from which you can create an individual object.
Constructor overloading
A constructor is just like a method but without return type. It can also be overloaded like Java methods. Constructor overloading in Java is a technique of having more than one constructor with different parameter lists. They are arranged in a way that each constructor performs a different task. They are differentiated by the compiler by the number of parameters in the list and their types. Student5(int i,String n){ id = i; name = n; } Student5(int i,String n,int a){ id = i; name = n; age=a; } Student5 s1 = new Student5(111,"Karan"); Student5 s2 = new Student5(222,"Aryan",25);
Parameterized constructor
A constructor which has a specific number of parameters. The parameterized constructor is used to provide different values to the distinct objects. We can have any number of parameters in the constructor. Student4(int i,String n){ id = i; name = n; } Student4 s1 = new Student4(111,"Karan");
Object
Any entity that has state and behavior is known as an object. An Object can be defined as an instance of a class. An object contains an address and takes up some space in memory. Objects can communicate without knowing the details of each other's data or code. The only necessary thing is the type of message accepted and the type of response returned by the objects. Example: A dog is an object because it has states like color, name, breed, etc. as well as behaviors like wagging the tail, barking, eating, etc.
Constructor vs. Method
Constructor : - A constructor is used to initialize the state of an object. - A constructor must not have a return type. - The constructor is invoked implicitly. - The Java compiler provides a default constructor if you don't have any constructor in a class. - The constructor name must be same as the class name. Method : - A method is used to expose the behavior of an object. - A method must have a return type. - The method is invoked explicitly. - The method is not provided by the compiler in any case. - The method name may or may not be same as class name.
Constructor
It is called when an instance of the object is created, and memory is allocated for the object. It is a special type of method which is used to initialize the object.
Advantages of OOPs over Procedure Oriented Programming
OOPs makes development and maintenance easier whereas in a procedure-oriented programming language it is not easy to manage if code grows as project size increases. OOPs provides data hiding whereas in a procedure-oriented programming language a global data can be accessed from anywhere.
Default constructor
The default constructor is a no-args constructor that the Java compiler inserts on your behalf; it contains a default call to super(); which is the default behavior. If you implement any constructor then you no longer receive a default constructor. Note: If there is no constructor in the class, the compiler adds a default constructor.
When is a constructor called?
When an object is created, compiler makes sure that constructors for all of its subobjects (its member and inherited objects) are called. If members have default constructors or constructor without parameter then these constructors are called automatically, otherwise parameterized constructors can be called using initializer list. Note: It is called constructor because it constructs the values at the time of object creation. It is not necessary to write a constructor for a class. It is because java compiler creates a default constructor if your class doesn't have any.
No-argument constructor
public Bicycle() { gear = 1; cadence = 10; speed = 0; } Bicycle yourBike = new Bicycle(); invokes the no-argument constructor to create a new Bicycle object called yourBike.