Use of Constructor Assignment Help

What is the use of constructor?

In polymorphism where poly means “many” and morphism means “forms” therefore the polymorphism means many forms. It is the skill of an entity to be in many forms.

Polymorphism in object oriented programming follows a rule where a parent class can access an object of a child class. An entity that have more than one method is considered a polymorphic object. Polymorphism works through a reference variable in order to achieve an objects you can communicate the methods through reference variables which is only in one type and cannot be changeable.

It is also be assigned as a class or as an interface. Whereas the constructor is one of the type of polymorphism while the other types of polymorphism are method overloading, method overridden. Inside the constructor you can initialize the data members of the class. Since the constructor is called whenever you create an object, you can define static variables in your class and manipulate those inside the constructor.

Suppose you want to know how many objects have been created in your program. What you can do is keep is declare a static variable as "static int count;" in your class definition. Increment the value of count in the constructor.

Now whenever you create an object, constructor is called and the value of count is incremented. In the end of program you can just print the value of count and you will know how many objects are created

It an object oriented programming constructor is a class or structure of a special method. It is an instant method and has the same class name and used to set the values of the members of an object.

Constructor may be in the form of default constructor and user define constructor. Constructor are not known as explicitly. All classes derived from the hierarchy and inherits from a parent class. The sequence of all the classes constructor call the other constructor in parent class and then derived class.

It does not inherited. It is a built-in method of a class to instantiate a class object and it can be many but different parameters is called constructor overloading.

You can initialize constructor with a same name of a class otherwise your constructor will not be created properly. Public Animal (), Public Animal (int a) is the example of constructor overloading. By using the access modifier constructor can be declared and right access modifier which mandatory have a constructor. Whenever the access modifier is not shown in the class there for it means a compiler supplies default.

Share This