Experiment No: 03
Aim:- To write a program in Java with class Rectangle with the data fields width, length,
area and colour. The length, width and area are of double type and colour is of string type.
The methods are get_length(), get_width(), get_colour() and find_area().
Theory:
Class:
The process of binding data members and associated methods in a
single unit is known as a Class.
A class is a collection of data members and associated methods.
Syntax to declare class
class <class_name>
{
Data member;
method;
}
Class is a template or blueprint from which objects are created.
OBJECT
Definition of object: -
Instance of a class is known as an object (instance is nothing but creating sufficient memory space
for the data members and methods of a class).
Every class variable is known as an object
Creating an object: -
Creating an object is nothing but allocating memory space for the data members and methods
of a class by following Dynamic memory allocation by using new operator.
To create an object we have two approaches they are
Approach 1: -
Sytnax:
<cls name>objname = new <clsname()>;
Ex: -Student s = new Student();
Approach 2: -
Syntax:
<cls name>obj name; ----- >1
Obj name = new <cls name()> ; ---- >2
Using a Class to Make Many Objects:
Class is like cookies cutters and cookies are objects. There is only one cookie cutter, but can be
used to make many cookies. Cookies can be created and cookies can be destroyed A big cookie
(such as a gingerbread house) might be built out of many smaller cookies of several different
types. Different cookies may have different characteristics, even though they follow the same
basic program.
Conclusion:
Program Code:
Take print of code along with output.
Questions-
1) Define package in Java.
2) Differentiate between instance variable and local variable.
3) Does JVM provide any default values to the local variables?
4) What are the default values provided by the JVM to the instance variables?
Experiment No: 04
Aim:- To write a program in JAVA to demonstrate the method and constructor
Overloading.
Theory:
In Java, a constructor is a block of codes similar to the method. It is called when an
instance of the class is created. At the time of calling constructor, memory for the
object is allocated in the memory.
It is a special type of method which is used to initialize the object.
Every time an object is created using the new() keyword, at least one constructor is
called.
A constructor may have or may not have parameters. Parameters are local variables
to receive data.
Rules for creating Java 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
Types of constructors
There are two types of constructors:
• Default constructor (no-argument constructor)
• Parameterized constructor
Default Constructor-
A constructor that have no parameter is known as default constructor.
It calls a default constructor if there is no constructor available in the class.
Java compiler provides a default constructor by default. The default constructor
is used to provide the default values to the object like 0, null, etc., depending on the type.
Syntax of default constructor:
<class_name>()
{
Parameterized Constructor-
A constructor which has a specific number of parameters is called a parameterized
constructor. The parameterized constructor is used to provide different values to
distinct objects. However, you can provide the same values also.
Constructor Overloading
Constructor overloading is a technique in Java in which a class can have any number of
constructors that differ in parameter lists. The compiler differentiates these constructors
bytaking into account the number of parameters in the list and their type
NUTAN MAHARASHTRA INSTITUTE OF ENGINEERING AND TECHNOLOGY, TALEGAON
DEPARTMENT OF ELECTRONICS AND TELECOMMUNIATION
Now let us come up with the syntax for the constructor been invoked at the time of object or
instance creation.
class Geek
{
.......
// A Constructor
new G()
{
// We can create an object of the above class
// using the below statement. This statement
// calls above constructor.
G obj = new g();
Method Overloading
If a class has multiple methods having same name but different in parameters, it is known
as Method Overloading. Method overloading increases the readability of the program.
There are two ways to overload the method in java
1. By changing number of arguments
2. By changing the data type
Conclusion-
Program Code:
Output:
Questions:-
1) What is difference between method overloading and constructor overloading?
2) What are possible access modifiers that can be marked for a constructor?
3) What is the use of constructor in java?