Topics: Constructors in Java Creating/Instantiating Objects Types of Constructors
Topics: Constructors in Java Creating/Instantiating Objects Types of Constructors
• Constructors in Java
• Creating/Instantiating Objects
• Types of Constructors
• Creating/Instantiating Objects
<Class-Name> object-reference = new Constructor();
new operator used to create
objects at runtime
Examples
Box b1 = new Box();
class BoxTest
{
public static void main (String [ ] args)
{ Constructor
Box b1 = new Box();
Box b2 = new Box(); Provided by
}// End of Method
}// End of class BoxTest JRE
4 Object-Oriented Programming Using Java
Types of Constructors
1. Un-parameterized Constructor
2. Parameterized Constructor
3. Overloaded Constructors
A(int a, int b)
{
this.a = a; Parameterized
this.b = b;
displayValues(); Constructor
} // End of Constructor Method
F:\>javac Demo.java
F:\>java ConstructorDemo
Side 1:10.0
Side 2:10.0
Side 3:10.0
Side 1:20.0
Side 2:20.0
Side 3:30.0
Side 1:10.0
Side 2:6.0
Side 3:8.0