What is the use of constructor in java

Vtech Academy of Computers
2 min readJan 16, 2017

--

Why we use constructor in java

Constructor is a special member function that’s name is same with class name and that is automatically invoked at the time of object creation. Construction has no return type even void. Construction can take argument or can not be. Constructor can not be declared in the private section of the class. But other visibility can be present with constructor. That means, constructor can be public, protected or friendly but can not be private. Constructor is divided in to two categories according to the argument. If the constructor can not take any argument then the constructor is called default constructor. If the constructor can take any argument then it is called parameterized constructor. If a class contains a parameterized constructor then the class should also contains a default constructor. There is no concept of copy constructor in JAVA.

Constructor can not be inherit in the derived classes but can be invoked from the sub-classes using the keyword “super”. If the super class contains a default constructor then it is not necessary to present a constructor in the sub-classes. If the super class contains a parameterized constructor, then in the sub class minimum one constructor must be present, to invoke the super class constructor. A single class can contain more then one constructor. That means constructor can be overloaded.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Vtech Academy of Computers
Vtech Academy of Computers

Written by Vtech Academy of Computers

It is one of the pioneer education institutes in India & has been revolutionizing the IT education with innovative and affordable IT courses.

No responses yet

Write a response