Showing posts with label constructors. Show all posts
Showing posts with label constructors. Show all posts

Monday, May 15, 2017

Constructors (Initializers) in Python

When you define a class in Python you can include constructor functions (actually initializers in Python) to initialize the instance variables. You can define them to accept arguments when the class is instantiated or to accept no arguments. This accomplishes more or less the same thing as overloaded constructors in Java. In this latter case in Python you would initialize the parameter variables to a desired default value in the constructor declaration. You would not write a separate constructor. Here is the code...

 Expanded image of constructors
Click for enlarged view




Friday, April 14, 2017

Using Java Constructors

Here is the code that instantiates an object form the prior EmployeeDetail2 class. It inputs an employee id. If the id is 0 it invokes the no argument constructor, otherwise it inputs the name and salary and invokes the constructor that takes arguments. On another note I forgot to mention that having multiple constructor with the same name is called overloading. We will see this same term later with methods.

 View expanded image on box.com.



Java constructor

This is the Java EmployeeDetails class with a constructor, Constructors are used to initialize the instance variables in an object when an object is instantiated form a class. If no arguments are passed in to the constructor then a no args constructor is applied. If the programmer does not supply a no args constructor then Java initializes the instance variables to default settings defined by their data type. You can have multiple constructors with the same name but their signatures must differ in the number of arguments passed.

 View expanded image on box.com
Click on the image for an expanded view on box.com.