Transcript JAVA

Lecture 06
Class Constructors





A constructor is a special method that is very different from the
basic method types.
A constructor executes as a normal method or function, but it
cannot return any value.
It is generally used to initialize the member variables of class, and
is called whenever an object of the class is create.
Constructor has the same name as class name.
In Java, constructors are of two types:
 Implicit constructor: when programmer do not define a constructor for a class,
the JVM provides a default constructor. This default constructor is called
implicit constructor. Default constructor is a nothing doing constructor with no
arguments.
 Default constructor is created only if there are no constructors. If you define any
constructor for your class, no default constructor is automatically created.
 So, for example, if you have a class B that has a constructor “B (int n)”, you
cannot create a object of B by “new B()” because you didn't define it and Java
didn't create it automatically. It is a compilation error.
Explicit constructors
 those constructor which are define by programmer in the class
definition. While creating an object of class, the values that you pass
and the parameters of constructor should match, in terms of number,
sequence, and data type.
○ There can be more than one explicit constructors in a class, distinguished
by their parameters. When the class is initialized, Java will call the right
constructor with matching arguments and type.
Examples














// a class with 3 constructors, differing by their parameters
class t2 {
t2 () {
System.out.println("empty arg called!");}
t2 (int n) {
System.out.println("int one called!");}
t2 (double n) {
System.out.println("double one called!");}
}
class t1 {
public static void main(String[] arg) {
// creating 3 objects of t2.
// when each object are created, Java automatically calls the right constructor
t2 x = new t2();
t2 y = new t2(3);
t2 z = new t2(3.0); }
}
While in explicit constructors, the first line of a constructor must
either be a call on another constructor in the same class (using this
keyword), or a call on the superclass constructor (using super
keyword).
 this(...) - Calls another constructor in same class. Often a constructor
with few parameters will call a constructor with more parameters, giving
default values for the missing parameters. Use this to call other
constructors in the same class.
 super(...). Use super to call a constructor in a parent class. Calling the
constructor for the superclass must be the first statement in the body of a
constructor.
Call constructor using this keyword













class A
{
int a,b;
A(int x, int y)
{
a=x;
b=y;
System.out.println(a+b);
}
A()
{
this(10,3);
System.out.println("Contructor with no arguments");
System.out.println(this.a);










}
}
public class B
{
public static void main(String args[])
{
A a=new A();
}
}
Output:
Constructor with arguments
13
Constructor with no arguments
10
Call a parent class constuctor using super keyword



























class A
{
int x;
float y;
A(int a, float b)
{
x=a;
y=b;
System.out.println("Class A Constructor");
System.out.println(x+" "+y);
}
}
class B extends A
{
B()
{
super(9,5.6f);
System.out.println("B class Constructor");
}
}
class constuctorsExp
{
public static void main(String args[])
{
B b=new B();
}
}
Output
Class A constructor
9 5.6
B class constructor
Using the Keyword super Accessing Superclass
Members

public class Superclass
{
public void printMethod()
{
System.out.println("Printed in Superclass."); } }

//Here is a subclass, called Subclass, that overrides printMethod():

public class Subclass extends Superclass {






public void printMethod() {

super.printMethod();

System.out.println("Printed in Subclass");
}
public static void main(String[] args) {
Subclass s = new Subclass();
s.printMethod();
}




