Fundamentals of Software Development 1Slide 1 Recap: Constructors, the new operator and the this object Three ideas:Three ideas: –How to write a constructor.

Similar presentations

Presentation on theme: "Fundamentals of Software Development 1Slide 1 Recap: Constructors, the new operator and the this object Three ideas:Three ideas: –How to write a constructor."— Presentation transcript:

1
Fundamentals of Software Development 1Slide 1 Recap: Constructors, the new operator and the this object Three ideas:Three ideas: –How to write a constructor –How to use a constructor with the new operator And why to use oneAnd why to use one –What the this object is The next several slides review each of these ideas

2
Fundamentals of Software Development 1Slide 2 Writing Constructors public class NameDropper extends StringTransformer implements StringTransformable { extends StringTransformer implements StringTransformable { private String name; private String name; public NameDropper () { public NameDropper () { this.name = "Who knows" ; } public NameDropper(String givenName) { public NameDropper(String givenName) { this.name = givenName; this.name = givenName; } public NameDropper (int givenNumber) { public NameDropper (int givenNumber) { this.name = "Car " + GivenNumber; this.name = "Car " + GivenNumber; } public String transform(String x) { public String transform(String x) { return this.name + " says " + x; }} A class can have many constructors, each with its own footprint (order and types of parameters) This constructor can be used to initialize the name field with the given name This constructor can be used to initialize the name field to the default value of “Who knows” This constructor can be used to initialize the name field as “Car number” Questions?