From the course: Object-Oriented Programming with Java

Unlock this course with a free trial

Join today to access over 22,500 courses taught by industry experts.

Construct a duck

Construct a duck

- [Instructor] I've already created a duck class and I'm going to use it to instantiate some different kinds of ducks. So you can see here, the duck has member variables of name, life expectancy, and favorite food, and two methods. To create different instances of a duck, I need a constructor. I'm going to auto generate a constructor with some help from Eclipse. Okay, Eclipse has made me this constructor and you can see that in the parentheses, it has all of the member variables. Sometimes they're called fields and sometimes they're called instance variables. Now that I have my constructor, it's time to go make some ducks. They're going to live in a pond. You're probably familiar with how to make a variable. It follows this pattern. First you have the type. Then you have a name. And then you set it equal to something. Like that. Well using constructors is very similar. The only difference is we're going to use the…

Contents