Creating and Using an Object with a Custom Constructor
Write a program that creates a new object of a class Dog
with the attributes name
and age
. The Dog
class has been provided for you. Create a new Dog
object using the custom constructor, set its name to "Rex"
and age to 5
, and print the name and age.
5
5
Instructor solution
Think you've got it?
How do you create a new object using a custom constructor in Java?
- A.
By using the
new
keyword followed by the class name. - B.
By calling the custom constructor without the
new
keyword. - C.
By using the
new
keyword followed by the custom constructor with appropriate arguments. - D.
By declaring a variable of the class type and assigning it the values of the attributes.
You may exit out of this review and return later without penalty.