35
Introduction to Java Programming R.Saravanakumar., MCA Developer, Trainer, Consultant

OOP's Part 1

Embed Size (px)

Citation preview

Page 1: OOP's Part 1

Introduction to Java Programming

R.Saravanakumar., MCADeveloper, Trainer, Consultant

Page 2: OOP's Part 1

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.

Page 3: OOP's Part 1

Types of Constructors

• Default constructor

Page 4: OOP's Part 1

• 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.

Page 5: OOP's Part 1
Page 6: OOP's Part 1

• Parameterized constructor

Page 7: OOP's Part 1

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()

Page 8: OOP's Part 1

Object creation using constructor

• Lets say class name is “Demo”1) I’m declaring a reference variable of class Demo-

Page 9: OOP's Part 1

Object creation using constructor

2) Object creation – Calling default constructor for creating the object of class Demo (new keyword followed by class name)

Page 10: OOP's Part 1

3) Now, I’m assigning the object to the reference –

Page 11: OOP's Part 1

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

Page 12: OOP's Part 1
Page 13: OOP's Part 1

• 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.

Page 14: OOP's Part 1

• If you understood the example then I may need to test your skills – Guess the output of below Java program

Page 15: OOP's Part 1
Page 16: OOP's Part 1
Page 17: OOP's Part 1

• Important Point to Note: If you are defining parameterized constructor then you may ran into trouble. Handle them with care :)

Page 18: OOP's Part 1
Page 19: OOP's Part 1

• 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.

Page 20: OOP's Part 1

• 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.

Page 21: OOP's Part 1

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.

Page 22: OOP's Part 1

• Have a look at the example below:

Page 23: OOP's Part 1
Page 24: OOP's Part 1
Page 25: OOP's Part 1

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.

Page 26: OOP's Part 1

• 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

Page 27: OOP's Part 1

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.

Page 28: OOP's Part 1
Page 29: OOP's Part 1

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.

Page 30: OOP's Part 1

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.

Page 31: OOP's Part 1

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.

Page 32: OOP's Part 1

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.

Page 33: OOP's Part 1

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.

Page 34: OOP's Part 1

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).

Page 35: OOP's Part 1

End