Java Questions 37
- If a superclass defines its own constructor with parameters, what happens when the subclass tries to invoke super(); without using arguments?
Answer:
The subclass will receive an error when it tries to invoke super(); without using arguments.
a) Arguments are used when you call a method and b) parameters are the elements in the method's signature.
- Are constructors inherited?
Answer:
Constructors are never inherited.
- Can constructors be overridden?
Answer:
Constructors cannot be overridden because they are not methods and only instance methods can be overridden.
- What type of polymorphism can occur with constructors?
Answer:
Static polymorphism can occur with constructors.
Static Binding is known as method overloading (in the same class).
Although constructors can not be overridden, constructors can be overloaded.
- What happens if you write your own constructor in a class (i.e. a constructor that takes a parameter)?
Answer:
If you write your own constructor in a class ( a constructor that takes a parameter), the compiler will not supply a default constructor.
- Can you inovke an instance method before the super constructor has run?
Answer:
You cannot inovke an instance method until after the super constructor has run.
- How do you create a String array in Java?
Answer:
String name = new String [] {"Alpha", "Beta", "Tau", "Sigma"};
- What must be the first line in a constructor?
Answer:
The first line in a constructor must be a call to super() or a call to this().
- Can a constructor make both a 1) call to super() and 2) a call to this()?
Answer:
A constructor can never have both a
- call to super and ()
- a call to this().
- Can you legally use both 1) this and 2) super() in the same constructor?
Answer:
You cannot legally use both 1) this and 2) super() in the same constructor. It is a requirements of the language that both appear on the first line of the constructor.