Java Programming
Date: / /
Practical No.2
Title:Write a Java Program to define a class, overload the constructors and
instantiate its object.
Government Polytechnic Nashik
Java Programming
Practical No.2
Title:Write a Java Program to define a class, overload the constructors and instantiate its
object.
Class: Class is a collection of objects of similar type. Once a class has been defined, we can
create any number of objects belonging to that class. It is a logical entity. Example: Fruit;
In Java everything is encapsulated under classes. Class is the core of Java language. Class can
be defined as a template/ blueprint that describe the behaviors /states of a particular entity. A
class defines new data type. Once defined this new type can be used to create object of that
type. A class is declared using class keyword. A class contain both data and code that operate
on that data. A class is a group of objects that has common properties. A class in java can
contain: data member, method, constructor, block, class and interface.
Syntax:
class
Data members;
Member method;
Main method;
Rules for Java Class
A class can have only public or default(no modifier) access specifier.
It can be either abstract, final or concrete (normal class).
It must have the class keyword, and class must be followed by a legal identifier.
It may optionally extend one parent class. By default, it will extend java.lang.Object.
It may optionally implement any number of comma-separated interfaces.
The class's variables and methods are declared within a set of curly braces {}.
Each .java source file may contain only one public class. A source file may contain any
number of default visible classes.
Finally, the source file name must match the public class name and it must have a .java
suffix.
Government Polytechnic Nashik
Java Programming
Constructors
A constructor is a special method that is used to initialize an object. Every class has a
constructor, if we don't explicitly declare a constructor for any java class the compiler builds
a default constructor for that class. A constructor does not have any return type. A constructor
has same name as the class in which it resides. Constructor in Java cannot be abstract, static,
final or synchronized. These modifiers are not allowed for constructor. Java constructor is
invoked at the time of object creation. It constructs the values i.e. provides data for the object
that is why it is known as constructor.
Rules for creating java constructor:
There are basically two rules defined for the constructor.
1. Constructor name must be same as its class name.
2. Constructor must have no explicit return type.
1. Default Constructor
A constructor that have no parameter is known as default constructor.
Syntax of default constructor:
<class name> ()
{ //initialization
Example:
class Bike1
Bike1()
System.out.println("Bike is created");
public static void main(String args[])
Bike1 b=new Bike1();
}}
Government Polytechnic Nashik
Java Programming
Parameterized Constructor
A constructor that have parameters is known as parameterized constructor. Why use
parameterized constructor? Parameterized constructor is used to provide different values to
the distinct objects.
Example:
class Student4
int id;
String name;
Student4(int i,String n)
id = i;
name = n;
void display()
System.out.println(id+" "+name);
public static void main(String args[])
Student4 s1 = new Student4(111,"Karan");
Student4 s2 = new Student4(222,"Aryan");
s1.display(); s2.display();
Constructor Overloading
Like methods, a constructor can also be overloaded. Overloaded constructors are
differentiated on the basis of their type of parameters or number of parameters. Constructor
overloading is not much different than method overloading. In case of method overloading
Government Polytechnic Nashik
Java Programming
you have multiple methods with same name but different signature, whereas in Constructor
overloading you have multiple constructor with different signature but only difference is that
Constructor doesn't have return type in Java. Constructor overloading is done to construct
object in different ways.
Example:
class Student5
int id;
String name;
int age;
Student5(int i,String n)
id = i; name = n;
Student5(int i,String n,int a)
id = i; name = n; age=a;
void display()
System.out.println(id+" "+name+" "+age);
public static void main(String args[])
Student5 s1 = new Student5(111,"Karan");
Student5 s2 = new Student5(222,"Aryan",25);
s1.display();
s2.display();
Government Polytechnic Nashik
Java Programming
Program 1:
Government Polytechnic Nashik
Java Programming
Out put:
Government Polytechnic Nashik
Java Programming
Government Polytechnic Nashik