Lecture 10 Objects Initialization Techniques - Part 2
The document discusses three ways to initialize an object in Java: using reference variables, methods, and constructors. It explains the role of constructors in setting initial values for object attributes and outlines the rules and types of constructors, including default, no-parameter, and parameterized constructors. Additionally, it covers the concept of constructor overloading, which allows multiple constructors with different parameter lists to exist within a class.
Lecture 10 Objects Initialization Techniques - Part 2
The document discusses three ways to initialize an object in Java: using reference variables, methods, and constructors. It explains the role of constructors in setting initial values for object attributes and outlines the rules and types of constructors, including default, no-parameter, and parameterized constructors. Additionally, it covers the concept of constructor overloading, which allows multiple constructors with different parameter lists to exist within a class.
National Institute of Technology, Srinagar, Jammu and Kashmir April 03, 2024 How an object can be initialized with Some VALUE in JAVA There are three ways for initializing an object: 1. By Reference Variable 2. By methods 3. By Constructors How an object can be initialized with Some VALUE in JAVA 1. By Reference Variables 2. By Methods Ways to Initialize Objects in Java There are three ways to initialize an object of a class: 1. Using Reference Variables 2. Using Methods 3. Using Constructors Java Constructors In Java, a constructor is a block of codes similar to the method. A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. Every time an object is created using the new() keyword, at least one constructor is called. Constructors can be used to set initial values for object attributes. Need of Constructor Think of a Box Class. If we talk about a box class then it will have some class variables (say length, breadth, and height). But when it comes to creating its object(i.e Box will now exist in computer’s memory), then can a box be there with no value defined for its dimensions. The answer is no.
Constructors are be used to set initial values for object attributes, at
the time of object creation, either explicitly done by the programmer or by Java itself (default constructor).. Rules for creating Java Constructor There are two rules defined for the constructor: Constructor(s) of a class must have same name as the class name in which it resides. A Constructor must have no explicit return type Types of constructors There are two types of constructors in Java: 1.Default constructor (no-arg constructor) 2.No Parameter Constructor 3.Parameterized constructor Types of constructors: Default Constructor If we don’t define a constructor in a class, then compiler creates default constructor(with no arguments) for the class.
Default constructor provides the default values to the object like 0, null, etc. depending on the type.
Default Constructor doesn’t have any arguments.
And if we write a constructor with arguments or no-arguments then the
compiler does not create a default constructor. Output? Default Constructor Types of constructors: No Parameter Constructor As the name specifies the no argument constructors of Java does not accept any parameters. Using this constructor, the instance variables of a method will be initialized with fixed values for all objects. No-Parameter Constructor Types of constructors: Parameterized constructor A constructor which has a specific number of parameters is called a parameterized constructor. Constructors can also take parameters, which is used to initialize attributes. With this type of constructor, we can initialize objects with different values. Parameter Constructor Difference between constructor and method in Java Constructor Overloading in Java In Java, 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. Constructor Overloading in Java