
In this post, you will see detail about Classes, Constructors, and Methods in Java.
1. Class:
i) A class is the collection of data members and member functions.
ii) It is a blueprint from which objects can be constructed. It is just a logical construct.
iii) A class can contain fields and methods to describe the state and behavior of an object respectively.
iv) Methods are the members of a class that provide a service for an object or perform some business logic.
v) The current states of a class’s corresponding object are stored in the object’s instance variables.
vi) Methods define the operations that can be performed in java programming.
vii) Java is a case-sensitive language. That is Java fields and member function names are case sensitive.
A sample of a class is given below:
public class Rectangle { int length; int breadth; public void area() { // code } public void perimeter() { // code } } |
class Rectangle represents the states and behavior of Rectangle.
2. What is an Object in Java?
Object:
i) The object is known as an instance of the class.
ii) Objects actually have their own states and behavior. For example, Rectangle objects have their own length, breadth, area, and perimeter.
iii) Hence the length and breadth are known as instance variables and methods area and perimeter are known as instance methods.
iv) Classes are just a logical construct but Objects are actually loaded into memory. Objects are physically present.
In java, objects are created dynamically using a new keyword.
There are three steps when creating an object from a class:
- Declaration: A variable declaration with a variable name with an object type.
- Instantiation: The 'new' keyword is used to create the object.
- Initialization: The 'new' keyword is followed by a call to a constructor. This call initializes the new object.
An example of creating an object is given below:
public class Rectangle { int length; int breadth;
public Rectangle() { length = 34; breadth = 35; }
public Rectangle(int length, int breadth) { this.length = length; this.breadth = breadth; }
public static void main(String args[]) { Rectangle
rect = new Rectangle(); // call will go to zero constructor Rectangle
rect1 = new Rectangle(23, 56); // call will go to parameterized constructor System.out.println("rect object length and breath is: " + rect.length + " " + rect.breadth); System.out.println("rect1 object length and breath is: " + rect1.length + " " + rect1.breadth); } } |
In the above program, when a new statement is get executed, the call goes to the constructor and is returns the object by initializing it. Thus memory to the objects is dynamically allocated at the time of execution of byte code. By using multiple new statements, we can create multiple objects of the same class.
3. What is Constructor in Java?
Constructor:
i) Every class has a constructor.
ii) Constructors are required to create objects for a class.
iii) Constructors are used to initialize the instance variables of an object. It has the same name as a class name.
iv) Each time a new object is created, at least one constructor will be invoked.
v) If we do not explicitly write a constructor for a class the java compiler builds a default constructor for that class.
vi) A class can have more than one constructor.
vii) In java, constructors are implicitly invoked at the time of object creation using a new keyword. It constructs the values i.e. provides data for the object that is why it is known as a constructor.
An example of a constructor is given below:
public class Rectangle { int length; int breadth;
public Rectangle() { // code for initialization }
public Rectangle(int length) { // This constructor has one
parameter, length // code for initialization } } |
2. 1 Properties of Constructor:
• Constructor name must be same as its class name
• Constructors used to initialize the data members
• Constructor must have no explicit return type as it implicitly returns an object
• Constructors can be declared private. In this case, we can not access this from outside of class.
• Constructors can be overloaded.
2.2 Types of java constructors There are two types of constructors:
• Non-parameterized Constructor also know as Zero constructor
• Parameterized Constructor
1. Zero constructors:
A constructor that has no parameter is known as a zero or non parameterized constructor.
Example :
In this example, we are creating the no-arg constructor in the Bike class. It will be invoked at the time of object creation.
public class Rectangle { public Rectangle() { System.out.println("Rectangle object initialization"); }
public static void main(String args[]) { Rectangle
rect = new Rectangle(); } } |
Output: Rectangle object initialization
2. Parameterized constructor
A constructor that has parameters is known as parameterized constructor. The parameterized constructor is used to provide different values to distinct objects.
Example :
In this example, we have created the constructor of the Student class that has two parameters. We can have any number of parameters in the constructor.
public class Rectangle {
int length; int breadth;
public Rectangle(int length, int breadth) { System.out.println("This is parameterized constructor"); }
public static void main(String args[]) { Rectangle
rect = new Rectangle(23, 56); } } |
Note: When there is no constructor in the class. JVM provides a default constructor whose syntax is same as a zero constructors.
2.3 Constructor Overloading:
It is one kind of compile-time polymorphism where the same constructor name but different type signatures are there. Constructor overloading is used to initialize the objects in different ways. The call to the matching constructor is decided at compile time.
Example :
public class Rectangle {
int length; int breadth;
public Rectangle() { length = 34; breadth = 35; }
public Rectangle(int length, int breadth) { this.length = length; this.breadth = breadth; }
public static void main(String args[]) { Rectangle
rect=new Rectangle() ; // call will go to zero constructor Rectangle
rect1=new Rectangle(23,56); // call will go to parameterized constructor System.out.println("Zero Parameterized rect object length and breath is:
"+rect.length+" "+rect.breadth); System.out.println("Parameterized rect1 object length and breath is: "+rect1.length+" "+rect1.breadth); } } |
Output:
Zero Parameterized rect object
length and breath is: 34 35 Parameterized rect1 object length
and breath is: 23 56 |
Note:
Java Constructor |
Java Method |
The constructor is used to
initialize the state of an object. |
The method is used to
expose the behavior of an object. |
The constructor must not have a return type. But it by default returns an object |
Method must
have return type or void. |
Constructor is invoked
implicitly, when a new statement is executed |
Method is invoked
explicitly |
The java default constructor if you don't have any constructor. |
Method is not
provided by java in any case. |
Constructor name must
be same as the class name. |
Method name may not be
same as the class name. |
public class Rectangle { int length; int breadth;
public Rectangle() { length = 34; breadth = 35; }
public Rectangle(int length, int breadth) { this.length = length; this.breadth = breadth; }
public int area() { int ar = length * breadth; return ar; }
public static void main(String args[]) { Rectangle
rect = new Rectangle(); // this call will go to zero constructor Rectangle
rect1 = new Rectangle(23, 56); // this call will go to parameterized constructor System.out.println("rect object length and breath is: " + rect.length + " " + rect.breadth); System.out.println("rect1 bject length and breath is: " + rect1.length + " " + rect1.breadth); int area1 = rect.area(); // calling instance method System.out.println("Area of rect :" + area1); int area2 = rect1.area(); // calling instance method System.out.println("Area of rect1 :" + area2); } } |
Output:
rect object length and breath is:
34 35 rect1 bject length and breath is:
23 56 Area of rect :1190 Area of rect1 :1288 |
This is all about Classes, Constructors, and Methods in Java, To want to learn more about core concepts you can prefer the below-recommended topics.
ConversionConversion EmoticonEmoticon