galdikas
galdikas

Reputation: 1669

User defined and default constructor?

Ok I am trying to wrap my head around this:

  1. Write an application that creates a class for the student object with the following attributes: Student Number, Name, Address, Phone No., and Course.

Write a test program that sets and gets each attribute in the class. The test program should also display all of the attributes in the class.

  1. Using the student class and its attributes from the previous question, write an application (extend the previous program) that includes both a user-defined and default constructor.

Write a test program to demonstrate the use of both constructors.

This is a work-sheet from college, for some revision on Objects in Java.

The part that troubles me is the one where it asks to have both user-defined and default constructor?

I was under impression it was impossible to do it? Because if you don't provide a constructor yourself, JVM will provide one (default constructor?). But if you define any constructor, then default one becomes unavailable?

Is this just poorly worded task, or could it mean something else?

Upvotes: 4

Views: 5678

Answers (6)

Patrick Kilula
Patrick Kilula

Reputation: 1

According to Charatan & Kans, Java in Two semesters 3rd Edition, page 196:

Constructor just like any methods can be overloaded, meaning within a single class we can have 2 or more constructors, where one take 1 argument like:("studentName") and within the same class, another constructor might take 2 arguments ("studentName", " studentId"), yet another constructor still within the same class may have 3 arguments ("studentName", "studentId", " studentPhoneNumber").

This is Constructor Overloading. Here's an example:

public class student {

    // attributes
    private String studentName;
    private int studentID;
    private int studentPhoneNumber;
    // constructor with one argument
    public student (String studentNameIn) {
        studentName = studentNameIn;
    }

    // constructor with 2 arguments
    public student (String studentNameIn,  int studentIdIn) {
        studentName = studentNameIn;
        studentID = studentIdIn;
    }

    // constructor with 3 arguments
    public student (String studentNameIn,  int studentIdIn, int studentPhoneNumberIn) {
        studentName = studentNameIn;
        studentID = studentIdIn;
        studentPhoneNumber = studentPhoneNumberIn;
    }

    // default constructor REINSERTED no argument
    public student () {}

    // methods
}

The above is a clear example of constructor overloading. Now during the OBJECT creation, meaning when the student object is being created, it will be up to the programmer to utilise the constructor he/she chooses, with 0 arguments ( default that was REINSERTED) or with 1 argument again he/she may choose the constructor that contains 2 arguments etc. Its a matter of choice and requirement of user.

No superclass or multiple class is needed as constructor can be overloaded as demonstrated above.

Upvotes: 0

J Steven Perry
J Steven Perry

Reputation: 1751

I'm pretty sure whoever created the work-sheet meant "No-arg constructor" instead of "Default constructor".

You can't technically create the Default constructor (since this is done for you by the compiler), but you can explicitly create the no-arg constructor (which functionally is the same).

So, yes, poorly worded task.

Upvotes: 7

stack smasher
stack smasher

Reputation: 463

Above is correct, however, in OO terms, "default constructor" is a constructor that takes in no arguments. The other type of constructor is one where arguments are taken into the constructor to make things custom.

ie:

Student(){     //default constructor
   number = 0;
   name = "bob";
   //etc etc
}

student(int nm, int nm, etc etc){   //parametrized constructor
   number = nm;
   name = nm;
   //etc etc
}

Upvotes: 0

user2707175
user2707175

Reputation: 1137

Default constructor is not created when programmer provides any constructor. But here I'm expecting that the author of this task understands "default" constructor as the one without any parameters.

Concluding you would have two constructors:

public class MyClass {

public MyClass () {
}

public MyClass (long studentNumber, String name, String address....) {
}

Upvotes: 0

RMachnik
RMachnik

Reputation: 3694

Java provide default constructor when you dont implement yourselve one. But when you create customized constructor you have to implement also default if you would like to use constructor with no arguments. Let's asume we have class A{} for java it will looks like that:

public class A{

   public A(){//auto generated constructor

   }
}

but if you provide an customized constructor auto generated constructor dissapear.

Upvotes: 0

morgano
morgano

Reputation: 17422

In java, when you don't explicitly specify a constructor, the compiler will add a "default" constructor: a constructor thar doesn't take parameters. If you specify a constructor, then the compiler doesn't add that constructor.

For instance, this code will compile fine:

class Student {
    String name;
    int age;
    // ...
}

// ...

Student myself = new Student();

But this code wont compile:

class Student {
    String name;
    int age;

    // ...

    public Student(String name) {
        this.name = name;
    }
}

// ...

Student myself = new Student(); // compilation error: use new Student("Jhon Smith");

Because the default constructor is not available any more.

Upvotes: 3

Related Questions