Introduction to Java Programming
R.Saravanakumar., MCA
Developer, Trainer, Consultant
Constructors in Java
• Constructor is a block of code that allows you to create an object of
class. This can also be called creating an instance.
• Constructor looks like a method but it’s not, for example methods
can have any return type or no return type (considered as void) but
constructors don’t have any return type not even void.
• There are several other differences between them, we will discuss
them in detail at the end of this article.
Types of Constructors
• Default constructor
• no-arg constructor
– Important point to note here is that whatever constructor
you write in your class cannot be called default even if you
write public Demo() { } in your class it cannot be called
default since you are writing it. The constructor is called
default only when it has been generated by java.
• Parameterized constructor
How to call a constructor?
• To call a constructor use the keyword new,
followed by the name of class, followed by
parameters if any. For example to create the
object of class Demo, you can call the constructor
like this: new Demo()
Object creation using constructor
• Lets say class name is “Demo”
1) I’m declaring a reference variable of class
Demo-
Object creation using constructor
2) Object creation – Calling default
constructor for creating the object of class
Demo (new keyword followed by class name)
3) Now, I’m assigning the object to the
reference –
What if you don’t write a constructor
in a class?
• As discussed above, if you don’t write a
constructor in your class, java would generate
one for you. Lets take a look at the code below
to understand it litter better
• In public static void main I am creating the
object of class Example and in order to do
that I have invoked the default constructor of
class Example( note the new Example()).
• But where did I write the constructor? No I
didn’t the java did it for me.
• If you understood the example then I may
need to test your skills – Guess the output of
below Java program
• Important Point to Note: If you are defining
parameterized constructor then you may ran
into trouble. Handle them with care :)
• Output:
It will throw a compilation error!!. The reason is when you
don’t define any constructor in your class, compiler defines
default one for you, however when you declare any
constructor (in above example I have already defined a
parameterized constructor), compiler doesn’t do it for you.
• Since I have defined a constructor in above code, compiler didn’t create
default one. While creating object I am invoking default one, which
doesn’t exist in above code. The code gives an compilation error.
• If we remove the parameterized constructor from the code above then the
code would run fine because, java would generate the default constructor
if it doesn’t find any in the code.
Constructor Chaining
• Constructor chaining is nothing but a scenario where in one
constructor calls the constructor of its super class implicitly or
explicitly.
• Suppose there is a class which inherits another class, in this case if
you create the object of child class then first super class(or parent
class) constructor will be invoked and then child class constructor
will be invoked.
• Have a look at the example below:
Explanation of the example:
• Human is a super class of Boy class. In above program I
have created an object of Boy class, As per the rule super
class constructor (Human()) invoked first which set the s1 &
s2 value, later child class constructor(Boy()) gets invoked,
which overridden s2 value.
• Note: Whenever child class constructor gets invoked it
implicitly invokes the constructor of parent class. In
simple terms you can say that compiler puts a super();
statement in the child class constructor. Read more
about super keyword
Question you may have
• In the above example no-arg constructor of super class
invoked, I want to invoke arg constructor(Parameterized).
• Its so simple.
• change the code of public Boy() to something like this –
• Note: super() should be the first statement in the
constructor.
Understood till now – Here are few
more points about constructors
• Every class has a constructor whether it’s normal one
or a abstract class.
• As stated above, constructor are not methods and they
don’t have any return type.
• Constructor name and class name should be the same.
ctd
• Constructor can use any access specifier, they can be declared as
private also. Private constructors are possible in java but there
scope is within the class only.
• Like constructors method can also have name same as class name,
but still they have return type, though which we can identify them
that they are methods not constructors.
ctd
• If you don’t define any constructor within the class,
compiler will do it for you and it will create a constructor
for you.
• this() and super() should be the first statement in the
constructor code. If you don’t mention them, compiler
does it for you accordingly.
ctd
• Constructor overloading is possible but overriding
is not possible. Which means we can have
overloaded constructor in our class but we can’t
override a constructor.
• Constructors can not be inherited.
ctd
• If Super class doesn’t have a no-arg(default)
constructor then compiler would not define a
default one in child class as it does in normal
scenario.
• Interfaces do not have constructors.
ctd
• Abstract can have constructors and these will get invoked
when a class, which implements interface, gets
instantiated. (i.e. object creation of concrete class).
• A constructor can also invoke another constructor of the
same class – By using this(). If you wanna invoke a arg-
constructor then give something like: this(parameter list).
End

OOP's Part 1

  • 1.
    Introduction to JavaProgramming R.Saravanakumar., MCA Developer, Trainer, Consultant
  • 2.
    Constructors in Java •Constructor is a block of code that allows you to create an object of class. This can also be called creating an instance. • Constructor looks like a method but it’s not, for example methods can have any return type or no return type (considered as void) but constructors don’t have any return type not even void. • There are several other differences between them, we will discuss them in detail at the end of this article.
  • 3.
    Types of Constructors •Default constructor
  • 4.
    • no-arg constructor –Important point to note here is that whatever constructor you write in your class cannot be called default even if you write public Demo() { } in your class it cannot be called default since you are writing it. The constructor is called default only when it has been generated by java.
  • 6.
  • 7.
    How to calla constructor? • To call a constructor use the keyword new, followed by the name of class, followed by parameters if any. For example to create the object of class Demo, you can call the constructor like this: new Demo()
  • 8.
    Object creation usingconstructor • Lets say class name is “Demo” 1) I’m declaring a reference variable of class Demo-
  • 9.
    Object creation usingconstructor 2) Object creation – Calling default constructor for creating the object of class Demo (new keyword followed by class name)
  • 10.
    3) Now, I’massigning the object to the reference –
  • 11.
    What if youdon’t write a constructor in a class? • As discussed above, if you don’t write a constructor in your class, java would generate one for you. Lets take a look at the code below to understand it litter better
  • 13.
    • In publicstatic void main I am creating the object of class Example and in order to do that I have invoked the default constructor of class Example( note the new Example()). • But where did I write the constructor? No I didn’t the java did it for me.
  • 14.
    • If youunderstood the example then I may need to test your skills – Guess the output of below Java program
  • 17.
    • Important Pointto Note: If you are defining parameterized constructor then you may ran into trouble. Handle them with care :)
  • 19.
    • Output: It willthrow a compilation error!!. The reason is when you don’t define any constructor in your class, compiler defines default one for you, however when you declare any constructor (in above example I have already defined a parameterized constructor), compiler doesn’t do it for you.
  • 20.
    • Since Ihave defined a constructor in above code, compiler didn’t create default one. While creating object I am invoking default one, which doesn’t exist in above code. The code gives an compilation error. • If we remove the parameterized constructor from the code above then the code would run fine because, java would generate the default constructor if it doesn’t find any in the code.
  • 21.
    Constructor Chaining • Constructorchaining is nothing but a scenario where in one constructor calls the constructor of its super class implicitly or explicitly. • Suppose there is a class which inherits another class, in this case if you create the object of child class then first super class(or parent class) constructor will be invoked and then child class constructor will be invoked.
  • 22.
    • Have alook at the example below:
  • 25.
    Explanation of theexample: • Human is a super class of Boy class. In above program I have created an object of Boy class, As per the rule super class constructor (Human()) invoked first which set the s1 & s2 value, later child class constructor(Boy()) gets invoked, which overridden s2 value.
  • 26.
    • Note: Wheneverchild class constructor gets invoked it implicitly invokes the constructor of parent class. In simple terms you can say that compiler puts a super(); statement in the child class constructor. Read more about super keyword
  • 27.
    Question you mayhave • In the above example no-arg constructor of super class invoked, I want to invoke arg constructor(Parameterized). • Its so simple. • change the code of public Boy() to something like this – • Note: super() should be the first statement in the constructor.
  • 29.
    Understood till now– Here are few more points about constructors • Every class has a constructor whether it’s normal one or a abstract class. • As stated above, constructor are not methods and they don’t have any return type. • Constructor name and class name should be the same.
  • 30.
    ctd • Constructor canuse any access specifier, they can be declared as private also. Private constructors are possible in java but there scope is within the class only. • Like constructors method can also have name same as class name, but still they have return type, though which we can identify them that they are methods not constructors.
  • 31.
    ctd • If youdon’t define any constructor within the class, compiler will do it for you and it will create a constructor for you. • this() and super() should be the first statement in the constructor code. If you don’t mention them, compiler does it for you accordingly.
  • 32.
    ctd • Constructor overloadingis possible but overriding is not possible. Which means we can have overloaded constructor in our class but we can’t override a constructor. • Constructors can not be inherited.
  • 33.
    ctd • If Superclass doesn’t have a no-arg(default) constructor then compiler would not define a default one in child class as it does in normal scenario. • Interfaces do not have constructors.
  • 34.
    ctd • Abstract canhave constructors and these will get invoked when a class, which implements interface, gets instantiated. (i.e. object creation of concrete class). • A constructor can also invoke another constructor of the same class – By using this(). If you wanna invoke a arg- constructor then give something like: this(parameter list).
  • 35.